rust-analyzer/crates/completion/src/item.rs

441 lines
13 KiB
Rust
Raw Normal View History

//! See `CompletionItem` structure.
use std::fmt;
use hir::{Documentation, ModPath, Mutability};
2020-12-02 21:55:35 +00:00
use ide_db::helpers::{
2020-12-10 14:41:57 +00:00
insert_use::{self, ImportScope, MergeBehavior},
2020-12-02 21:55:35 +00:00
mod_path_to_ast,
};
2020-12-04 08:02:22 +00:00
use syntax::{algo, TextRange};
2020-08-12 15:03:06 +00:00
use text_edit::TextEdit;
2020-10-25 07:59:15 +00:00
use crate::config::SnippetCap;
2020-05-06 14:05:43 +00:00
2019-01-08 19:33:36 +00:00
/// `CompletionItem` describes a single completion variant in the editor pop-up.
/// It is basically a POD with various properties. To construct a
/// `CompletionItem`, use `new` method and the `Builder` struct.
#[derive(Clone)]
2019-01-08 19:33:36 +00:00
pub struct CompletionItem {
/// Used only internally in tests, to check only specific kind of
/// completion (postfix, keyword, reference, etc).
#[allow(unused)]
2020-03-11 09:46:43 +00:00
pub(crate) completion_kind: CompletionKind,
/// Label in the completion pop up which identifies completion.
2019-01-08 19:33:36 +00:00
label: String,
/// Range of identifier that is being completed.
///
/// It should be used primarily for UI, but we also use this to convert
/// genetic TextEdit into LSP's completion edit (see conv.rs).
///
/// `source_range` must contain the completion offset. `insert_text` should
/// start with what `source_range` points to, or VSCode will filter out the
/// completion silently.
source_range: TextRange,
/// What happens when user selects this item.
///
/// Typically, replaces `source_range` with new identifier.
text_edit: TextEdit,
2020-09-18 20:40:11 +00:00
insert_text_format: InsertTextFormat,
/// What item (struct, function, etc) are we completing.
kind: Option<CompletionItemKind>,
/// Lookup is used to check if completion item indeed can complete current
/// ident.
///
/// That is, in `foo.bar<|>` lookup of `abracadabra` will be accepted (it
/// contains `bar` sub sequence), and `quux` will rejected.
lookup: Option<String>,
/// Additional info to show in the UI pop up.
2019-01-09 15:09:49 +00:00
detail: Option<String>,
documentation: Option<Documentation>,
/// Whether this item is marked as deprecated
deprecated: bool,
/// If completing a function call, ask the editor to show parameter popup
/// after completion.
trigger_call_info: bool,
2020-04-23 22:11:33 +00:00
/// Score is useful to pre select or display in better order completion items
score: Option<CompletionScore>,
2020-09-29 20:24:56 +00:00
/// Indicates that a reference or mutable reference to this variable is a
/// possible match.
ref_match: Option<(Mutability, CompletionScore)>,
2020-11-28 14:26:30 +00:00
2020-12-02 22:13:32 +00:00
/// The import data to add to completion's edits.
2020-12-03 09:13:28 +00:00
import_to_add: Option<ImportEdit>,
2019-01-08 19:33:36 +00:00
}
// We use custom debug for CompletionItem to make snapshot tests more readable.
impl fmt::Debug for CompletionItem {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut s = f.debug_struct("CompletionItem");
2019-02-18 09:23:31 +00:00
s.field("label", &self.label()).field("source_range", &self.source_range());
2020-05-21 13:56:18 +00:00
if self.text_edit().len() == 1 {
let atom = &self.text_edit().iter().next().unwrap();
2019-02-18 09:23:31 +00:00
s.field("delete", &atom.delete);
s.field("insert", &atom.insert);
} else {
s.field("text_edit", &self.text_edit);
}
if let Some(kind) = self.kind().as_ref() {
s.field("kind", kind);
}
if self.lookup() != self.label() {
s.field("lookup", &self.lookup());
}
if let Some(detail) = self.detail() {
s.field("detail", &detail);
}
if let Some(documentation) = self.documentation() {
s.field("documentation", &documentation);
}
if self.deprecated {
s.field("deprecated", &true);
}
if let Some(score) = &self.score {
s.field("score", score);
}
2020-03-12 18:02:55 +00:00
if self.trigger_call_info {
s.field("trigger_call_info", &true);
}
s.finish()
}
}
2020-07-04 07:53:54 +00:00
#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq)]
2020-04-23 22:11:33 +00:00
pub enum CompletionScore {
/// If only type match
TypeMatch,
/// If type and name match
TypeAndNameMatch,
}
2019-01-08 19:33:36 +00:00
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompletionItemKind {
Snippet,
Keyword,
Module,
Function,
2019-05-30 13:10:07 +00:00
BuiltinType,
2019-01-08 19:33:36 +00:00
Struct,
Enum,
EnumVariant,
Binding,
Field,
2019-01-11 18:02:12 +00:00
Static,
Const,
Trait,
TypeAlias,
2019-01-07 18:12:19 +00:00
Method,
2019-01-27 19:50:57 +00:00
TypeParam,
2019-04-24 20:16:50 +00:00
Macro,
2020-04-23 16:22:33 +00:00
Attribute,
UnresolvedReference,
2019-01-08 19:33:36 +00:00
}
2020-06-12 18:30:57 +00:00
impl CompletionItemKind {
2020-06-13 11:47:30 +00:00
#[cfg(test)]
pub(crate) fn tag(&self) -> &'static str {
match self {
2020-07-03 09:48:48 +00:00
CompletionItemKind::Attribute => "at",
CompletionItemKind::Binding => "bn",
2020-06-12 18:30:57 +00:00
CompletionItemKind::BuiltinType => "bt",
2020-07-03 09:48:48 +00:00
CompletionItemKind::Const => "ct",
2020-06-12 18:30:57 +00:00
CompletionItemKind::Enum => "en",
CompletionItemKind::EnumVariant => "ev",
CompletionItemKind::Field => "fd",
2020-07-03 09:48:48 +00:00
CompletionItemKind::Function => "fn",
CompletionItemKind::Keyword => "kw",
CompletionItemKind::Macro => "ma",
CompletionItemKind::Method => "me",
CompletionItemKind::Module => "md",
CompletionItemKind::Snippet => "sn",
2020-06-12 18:30:57 +00:00
CompletionItemKind::Static => "sc",
2020-07-03 09:48:48 +00:00
CompletionItemKind::Struct => "st",
2020-06-12 18:30:57 +00:00
CompletionItemKind::Trait => "tt",
CompletionItemKind::TypeAlias => "ta",
CompletionItemKind::TypeParam => "tp",
CompletionItemKind::UnresolvedReference => "??",
2020-06-13 11:47:30 +00:00
}
2020-06-12 18:30:57 +00:00
}
}
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
2019-01-08 19:33:36 +00:00
pub(crate) enum CompletionKind {
/// Parser-based keyword completion.
Keyword,
/// Your usual "complete all valid identifiers".
Reference,
/// "Secret sauce" completions.
Magic,
Snippet,
2019-01-21 05:19:51 +00:00
Postfix,
2019-05-30 13:10:07 +00:00
BuiltinType,
2020-04-23 16:22:33 +00:00
Attribute,
2019-01-08 19:33:36 +00:00
}
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum InsertTextFormat {
PlainText,
Snippet,
}
2019-01-08 19:33:36 +00:00
impl CompletionItem {
pub(crate) fn new(
completion_kind: CompletionKind,
source_range: TextRange,
label: impl Into<String>,
) -> Builder {
2019-01-08 19:33:36 +00:00
let label = label.into();
Builder {
source_range,
2019-01-08 19:33:36 +00:00
completion_kind,
label,
insert_text: None,
insert_text_format: InsertTextFormat::PlainText,
2019-01-09 15:09:49 +00:00
detail: None,
documentation: None,
2019-01-08 19:33:36 +00:00
lookup: None,
kind: None,
text_edit: None,
deprecated: None,
trigger_call_info: None,
score: None,
2020-09-29 20:24:56 +00:00
ref_match: None,
2020-11-27 10:22:10 +00:00
import_to_add: None,
}
}
2019-01-08 19:33:36 +00:00
/// What user sees in pop-up in the UI.
pub fn label(&self) -> &str {
&self.label
}
pub fn source_range(&self) -> TextRange {
self.source_range
}
pub fn insert_text_format(&self) -> InsertTextFormat {
self.insert_text_format
}
pub fn text_edit(&self) -> &TextEdit {
&self.text_edit
}
2019-01-09 15:09:49 +00:00
/// Short one-line additional information, like a type
pub fn detail(&self) -> Option<&str> {
2020-02-18 13:32:19 +00:00
self.detail.as_deref()
2019-01-09 15:09:49 +00:00
}
/// A doc-comment
pub fn documentation(&self) -> Option<Documentation> {
self.documentation.clone()
}
2019-01-08 19:33:36 +00:00
/// What string is used for filtering.
pub fn lookup(&self) -> &str {
2020-04-23 23:17:33 +00:00
self.lookup.as_deref().unwrap_or(&self.label)
2019-01-08 19:33:36 +00:00
}
2019-01-08 19:33:36 +00:00
pub fn kind(&self) -> Option<CompletionItemKind> {
self.kind
}
pub fn deprecated(&self) -> bool {
self.deprecated
}
pub fn score(&self) -> Option<CompletionScore> {
2020-04-23 23:08:27 +00:00
self.score
}
pub fn trigger_call_info(&self) -> bool {
self.trigger_call_info
}
2020-09-29 20:24:56 +00:00
pub fn ref_match(&self) -> Option<(Mutability, CompletionScore)> {
self.ref_match
}
2020-12-03 09:13:28 +00:00
pub fn import_to_add(&self) -> Option<&ImportEdit> {
self.import_to_add.as_ref()
}
2019-01-08 19:33:36 +00:00
}
2020-11-27 10:22:10 +00:00
/// An extra import to add after the completion is applied.
#[derive(Debug, Clone)]
2020-12-03 09:13:28 +00:00
pub struct ImportEdit {
pub import_path: ModPath,
pub import_scope: ImportScope,
2020-11-27 10:22:10 +00:00
}
2020-12-03 09:13:28 +00:00
impl ImportEdit {
/// Attempts to insert the import to the given scope, producing a text edit.
/// May return no edit in edge cases, such as scope already containing the import.
pub fn to_text_edit(&self, merge_behavior: Option<MergeBehavior>) -> Option<TextEdit> {
2020-12-04 18:02:42 +00:00
let _p = profile::span("ImportEdit::to_text_edit");
2020-12-03 09:13:28 +00:00
let rewriter = insert_use::insert_use(
&self.import_scope,
mod_path_to_ast(&self.import_path),
merge_behavior,
2020-12-03 09:13:28 +00:00
);
let old_ast = rewriter.rewrite_root()?;
let mut import_insert = TextEdit::builder();
algo::diff(&old_ast, &rewriter.rewrite(&old_ast)).into_text_edit(&mut import_insert);
Some(import_insert.finish())
}
}
2019-01-08 19:33:36 +00:00
/// A helper to make `CompletionItem`s.
#[must_use]
2020-09-29 20:24:56 +00:00
#[derive(Clone)]
pub(crate) struct Builder {
source_range: TextRange,
2019-01-08 19:33:36 +00:00
completion_kind: CompletionKind,
2020-12-03 09:13:28 +00:00
import_to_add: Option<ImportEdit>,
2019-01-08 19:33:36 +00:00
label: String,
insert_text: Option<String>,
insert_text_format: InsertTextFormat,
2019-01-09 15:09:49 +00:00
detail: Option<String>,
documentation: Option<Documentation>,
2019-01-08 19:33:36 +00:00
lookup: Option<String>,
kind: Option<CompletionItemKind>,
text_edit: Option<TextEdit>,
deprecated: Option<bool>,
trigger_call_info: Option<bool>,
score: Option<CompletionScore>,
2020-09-29 20:24:56 +00:00
ref_match: Option<(Mutability, CompletionScore)>,
2019-01-08 19:33:36 +00:00
}
impl Builder {
2019-01-08 19:33:36 +00:00
pub(crate) fn build(self) -> CompletionItem {
2020-11-27 16:00:03 +00:00
let _p = profile::span("item::Builder::build");
let mut label = self.label;
let mut lookup = self.lookup;
let mut insert_text = self.insert_text;
2020-11-28 14:26:30 +00:00
if let Some(import_to_add) = self.import_to_add.as_ref() {
let mut import_path_without_last_segment = import_to_add.import_path.to_owned();
let _ = import_path_without_last_segment.segments.pop();
if !import_path_without_last_segment.segments.is_empty() {
if lookup.is_none() {
lookup = Some(label.clone());
}
if insert_text.is_none() {
insert_text = Some(label.clone());
}
label = format!("{}::{}", import_path_without_last_segment, label);
}
}
let text_edit = match self.text_edit {
Some(it) => it,
None => {
TextEdit::replace(self.source_range, insert_text.unwrap_or_else(|| label.clone()))
}
};
2019-01-08 19:33:36 +00:00
CompletionItem {
source_range: self.source_range,
label,
insert_text_format: self.insert_text_format,
2020-12-03 09:13:28 +00:00
text_edit,
2019-01-09 15:09:49 +00:00
detail: self.detail,
documentation: self.documentation,
lookup,
2019-01-08 19:33:36 +00:00
kind: self.kind,
completion_kind: self.completion_kind,
deprecated: self.deprecated.unwrap_or(false),
trigger_call_info: self.trigger_call_info.unwrap_or(false),
score: self.score,
2020-09-29 20:24:56 +00:00
ref_match: self.ref_match,
import_to_add: self.import_to_add,
2019-01-08 19:33:36 +00:00
}
}
pub(crate) fn lookup_by(mut self, lookup: impl Into<String>) -> Builder {
2019-01-08 19:33:36 +00:00
self.lookup = Some(lookup.into());
self
}
pub(crate) fn label(mut self, label: impl Into<String>) -> Builder {
self.label = label.into();
self
}
pub(crate) fn insert_text(mut self, insert_text: impl Into<String>) -> Builder {
self.insert_text = Some(insert_text.into());
self
}
pub(crate) fn insert_snippet(
mut self,
_cap: SnippetCap,
snippet: impl Into<String>,
) -> Builder {
self.insert_text_format = InsertTextFormat::Snippet;
self.insert_text(snippet)
}
pub(crate) fn kind(mut self, kind: CompletionItemKind) -> Builder {
2019-01-08 19:33:36 +00:00
self.kind = Some(kind);
self
}
pub(crate) fn text_edit(mut self, edit: TextEdit) -> Builder {
self.text_edit = Some(edit);
self
}
pub(crate) fn snippet_edit(mut self, _cap: SnippetCap, edit: TextEdit) -> Builder {
self.insert_text_format = InsertTextFormat::Snippet;
self.text_edit(edit)
}
#[allow(unused)]
pub(crate) fn detail(self, detail: impl Into<String>) -> Builder {
2019-01-09 15:46:02 +00:00
self.set_detail(Some(detail))
}
pub(crate) fn set_detail(mut self, detail: Option<impl Into<String>>) -> Builder {
2019-01-09 15:46:02 +00:00
self.detail = detail.map(Into::into);
2019-01-09 15:09:49 +00:00
self
}
#[allow(unused)]
pub(crate) fn documentation(self, docs: Documentation) -> Builder {
self.set_documentation(Some(docs))
}
pub(crate) fn set_documentation(mut self, docs: Option<Documentation>) -> Builder {
self.documentation = docs.map(Into::into);
self
}
pub(crate) fn set_deprecated(mut self, deprecated: bool) -> Builder {
self.deprecated = Some(deprecated);
self
}
pub(crate) fn set_score(mut self, score: CompletionScore) -> Builder {
self.score = Some(score);
self
}
pub(crate) fn trigger_call_info(mut self) -> Builder {
self.trigger_call_info = Some(true);
self
}
pub(crate) fn add_import(mut self, import_to_add: Option<ImportEdit>) -> Builder {
2020-11-27 10:22:10 +00:00
self.import_to_add = import_to_add;
self
}
2020-09-29 20:24:56 +00:00
pub(crate) fn set_ref_match(
mut self,
ref_match: Option<(Mutability, CompletionScore)>,
) -> Builder {
self.ref_match = ref_match;
self
}
2019-01-08 19:33:36 +00:00
}
impl<'a> Into<CompletionItem> for Builder {
2019-01-08 19:33:36 +00:00
fn into(self) -> CompletionItem {
self.build()
}
}