1 //! See `CompletionItem` structure.
5 use hir::{Documentation, Mutability};
8 import_assets::LocatedImport,
9 insert_use::{self, ImportScope, InsertUseConfig},
10 mod_path_to_ast, SnippetCap,
14 use stdx::{format_to, impl_from, never};
15 use syntax::{algo, TextRange};
16 use text_edit::TextEdit;
18 /// `CompletionItem` describes a single completion variant in the editor pop-up.
19 /// It is basically a POD with various properties. To construct a
20 /// `CompletionItem`, use `new` method and the `Builder` struct.
22 pub struct CompletionItem {
23 /// Used only internally in tests, to check only specific kind of
24 /// completion (postfix, keyword, reference, etc).
26 pub(crate) completion_kind: CompletionKind,
27 /// Label in the completion pop up which identifies completion.
29 /// Range of identifier that is being completed.
31 /// It should be used primarily for UI, but we also use this to convert
32 /// generic TextEdit into LSP's completion edit (see conv.rs).
34 /// `source_range` must contain the completion offset. `insert_text` should
35 /// start with what `source_range` points to, or VSCode will filter out the
36 /// completion silently.
37 source_range: TextRange,
38 /// What happens when user selects this item.
40 /// Typically, replaces `source_range` with new identifier.
44 /// What item (struct, function, etc) are we completing.
45 kind: Option<CompletionItemKind>,
47 /// Lookup is used to check if completion item indeed can complete current
50 /// That is, in `foo.bar$0` lookup of `abracadabra` will be accepted (it
51 /// contains `bar` sub sequence), and `quux` will rejected.
52 lookup: Option<String>,
54 /// Additional info to show in the UI pop up.
55 detail: Option<String>,
56 documentation: Option<Documentation>,
58 /// Whether this item is marked as deprecated
61 /// If completing a function call, ask the editor to show parameter popup
63 trigger_call_info: bool,
65 /// We use this to sort completion. Relevance records facts like "do the
66 /// types align precisely?". We can't sort by relevances directly, they are
67 /// only partially ordered.
69 /// Note that Relevance ignores fuzzy match score. We compute Relevance for
70 /// all possible items, and then separately build an ordered completion list
71 /// based on relevance and fuzzy matching with the already typed identifier.
72 relevance: CompletionRelevance,
74 /// Indicates that a reference or mutable reference to this variable is a
76 ref_match: Option<Mutability>,
78 /// The import data to add to completion's edits.
79 import_to_add: Option<ImportEdit>,
82 // We use custom debug for CompletionItem to make snapshot tests more readable.
83 impl fmt::Debug for CompletionItem {
84 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
85 let mut s = f.debug_struct("CompletionItem");
86 s.field("label", &self.label()).field("source_range", &self.source_range());
87 if self.text_edit().len() == 1 {
88 let atom = &self.text_edit().iter().next().unwrap();
89 s.field("delete", &atom.delete);
90 s.field("insert", &atom.insert);
92 s.field("text_edit", &self.text_edit);
94 if let Some(kind) = self.kind().as_ref() {
95 s.field("kind", kind);
97 if self.lookup() != self.label() {
98 s.field("lookup", &self.lookup());
100 if let Some(detail) = self.detail() {
101 s.field("detail", &detail);
103 if let Some(documentation) = self.documentation() {
104 s.field("documentation", &documentation);
107 s.field("deprecated", &true);
110 if self.relevance != CompletionRelevance::default() {
111 s.field("relevance", &self.relevance);
114 if let Some(mutability) = &self.ref_match {
115 s.field("ref_match", &format!("&{}", mutability.as_keyword_for_ref()));
117 if self.trigger_call_info {
118 s.field("trigger_call_info", &true);
124 #[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
125 pub struct CompletionRelevance {
126 /// This is set in cases like these:
129 /// fn f(spam: String) {}
132 /// f($0) // name of local matches the name of param
135 pub exact_name_match: bool,
136 /// See CompletionRelevanceTypeMatch doc comments for cases where this is set.
137 pub type_match: Option<CompletionRelevanceTypeMatch>,
138 /// This is set in cases like these:
143 /// $0 // `a` and `b` are local
147 /// This is set in cases like these:
153 /// Basically, we want to guarantee that postfix snippets always takes
154 /// precedence over everything else.
155 pub exact_postfix_snippet_match: bool,
158 #[derive(Debug, Clone, Copy, Eq, PartialEq)]
159 pub enum CompletionRelevanceTypeMatch {
160 /// This is set in cases like these:
163 /// enum Option<T> { Some(T), None }
164 /// fn f(a: Option<u32>) {}
166 /// f(Option::N$0) // type `Option<T>` could unify with `Option<u32>`
170 /// This is set in cases like these:
173 /// fn f(spam: String) {}
175 /// let foo = String::new();
176 /// f($0) // type of local matches the type of param
182 impl CompletionRelevance {
183 /// Provides a relevance score. Higher values are more relevant.
185 /// The absolute value of the relevance score is not meaningful, for
186 /// example a value of 0 doesn't mean "not relevant", rather
187 /// it means "least relevant". The score value should only be used
188 /// for relative ordering.
190 /// See is_relevant if you need to make some judgement about score
191 /// in an absolute sense.
192 pub fn score(&self) -> u32 {
195 if self.exact_name_match {
198 score += match self.type_match {
199 Some(CompletionRelevanceTypeMatch::Exact) => 4,
200 Some(CompletionRelevanceTypeMatch::CouldUnify) => 3,
206 if self.exact_postfix_snippet_match {
212 /// Returns true when the score for this threshold is above
213 /// some threshold such that we think it is especially likely
215 pub fn is_relevant(&self) -> bool {
220 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
221 pub enum CompletionItemKind {
222 SymbolKind(SymbolKind),
232 impl_from!(SymbolKind for CompletionItemKind);
234 impl CompletionItemKind {
236 pub(crate) fn tag(&self) -> &'static str {
238 CompletionItemKind::SymbolKind(kind) => match kind {
239 SymbolKind::Const => "ct",
240 SymbolKind::ConstParam => "cp",
241 SymbolKind::Enum => "en",
242 SymbolKind::Field => "fd",
243 SymbolKind::Function => "fn",
244 SymbolKind::Impl => "im",
245 SymbolKind::Label => "lb",
246 SymbolKind::LifetimeParam => "lt",
247 SymbolKind::Local => "lc",
248 SymbolKind::Macro => "ma",
249 SymbolKind::Module => "md",
250 SymbolKind::SelfParam => "sp",
251 SymbolKind::Static => "sc",
252 SymbolKind::Struct => "st",
253 SymbolKind::Trait => "tt",
254 SymbolKind::TypeAlias => "ta",
255 SymbolKind::TypeParam => "tp",
256 SymbolKind::Union => "un",
257 SymbolKind::ValueParam => "vp",
258 SymbolKind::Variant => "ev",
260 CompletionItemKind::Attribute => "at",
261 CompletionItemKind::Binding => "bn",
262 CompletionItemKind::BuiltinType => "bt",
263 CompletionItemKind::Keyword => "kw",
264 CompletionItemKind::Method => "me",
265 CompletionItemKind::Snippet => "sn",
266 CompletionItemKind::UnresolvedReference => "??",
271 #[derive(Debug, PartialEq, Eq, Copy, Clone)]
272 pub(crate) enum CompletionKind {
273 /// Parser-based keyword completion.
275 /// Your usual "complete all valid identifiers".
277 /// "Secret sauce" completions.
285 impl CompletionItem {
287 completion_kind: CompletionKind,
288 source_range: TextRange,
289 label: impl Into<String>,
291 let label = label.into();
305 trigger_call_info: None,
306 relevance: CompletionRelevance::default(),
312 /// What user sees in pop-up in the UI.
313 pub fn label(&self) -> &str {
316 pub fn source_range(&self) -> TextRange {
320 pub fn text_edit(&self) -> &TextEdit {
323 /// Whether `text_edit` is a snippet (contains `$0` markers).
324 pub fn is_snippet(&self) -> bool {
328 /// Short one-line additional information, like a type
329 pub fn detail(&self) -> Option<&str> {
330 self.detail.as_deref()
333 pub fn documentation(&self) -> Option<Documentation> {
334 self.documentation.clone()
336 /// What string is used for filtering.
337 pub fn lookup(&self) -> &str {
338 self.lookup.as_deref().unwrap_or(&self.label)
341 pub fn kind(&self) -> Option<CompletionItemKind> {
345 pub fn deprecated(&self) -> bool {
349 pub fn relevance(&self) -> CompletionRelevance {
353 pub fn trigger_call_info(&self) -> bool {
354 self.trigger_call_info
357 pub fn ref_match(&self) -> Option<(Mutability, CompletionRelevance)> {
358 // Relevance of the ref match should be the same as the original
359 // match, but with exact type match set because self.ref_match
360 // is only set if there is an exact type match.
361 let mut relevance = self.relevance;
362 relevance.type_match = Some(CompletionRelevanceTypeMatch::Exact);
364 self.ref_match.map(|mutability| (mutability, relevance))
367 pub fn import_to_add(&self) -> Option<&ImportEdit> {
368 self.import_to_add.as_ref()
372 /// An extra import to add after the completion is applied.
373 #[derive(Debug, Clone)]
374 pub struct ImportEdit {
375 pub import: LocatedImport,
376 pub scope: ImportScope,
380 /// Attempts to insert the import to the given scope, producing a text edit.
381 /// May return no edit in edge cases, such as scope already containing the import.
382 pub fn to_text_edit(&self, cfg: InsertUseConfig) -> Option<TextEdit> {
383 let _p = profile::span("ImportEdit::to_text_edit");
385 let new_ast = self.scope.clone_for_update();
386 insert_use::insert_use(&new_ast, mod_path_to_ast(&self.import.import_path), &cfg);
387 let mut import_insert = TextEdit::builder();
388 algo::diff(self.scope.as_syntax_node(), new_ast.as_syntax_node())
389 .into_text_edit(&mut import_insert);
391 Some(import_insert.finish())
395 /// A helper to make `CompletionItem`s.
398 pub(crate) struct Builder {
399 source_range: TextRange,
400 completion_kind: CompletionKind,
401 import_to_add: Option<ImportEdit>,
402 trait_name: Option<String>,
404 insert_text: Option<String>,
406 detail: Option<String>,
407 documentation: Option<Documentation>,
408 lookup: Option<String>,
409 kind: Option<CompletionItemKind>,
410 text_edit: Option<TextEdit>,
412 trigger_call_info: Option<bool>,
413 relevance: CompletionRelevance,
414 ref_match: Option<Mutability>,
418 pub(crate) fn build(self) -> CompletionItem {
419 let _p = profile::span("item::Builder::build");
421 let mut label = self.label;
422 let mut lookup = self.lookup;
423 let mut insert_text = self.insert_text;
425 if let Some(original_path) = self
428 .and_then(|import_edit| import_edit.import.original_path.as_ref())
430 lookup = lookup.or_else(|| Some(label.clone()));
431 insert_text = insert_text.or_else(|| Some(label.clone()));
432 format_to!(label, " (use {})", original_path)
433 } else if let Some(trait_name) = self.trait_name {
434 insert_text = insert_text.or_else(|| Some(label.clone()));
435 format_to!(label, " (as {})", trait_name)
438 let text_edit = match self.text_edit {
441 TextEdit::replace(self.source_range, insert_text.unwrap_or_else(|| label.clone()))
446 source_range: self.source_range,
449 is_snippet: self.is_snippet,
451 documentation: self.documentation,
454 completion_kind: self.completion_kind,
455 deprecated: self.deprecated,
456 trigger_call_info: self.trigger_call_info.unwrap_or(false),
457 relevance: self.relevance,
458 ref_match: self.ref_match,
459 import_to_add: self.import_to_add,
462 pub(crate) fn lookup_by(&mut self, lookup: impl Into<String>) -> &mut Builder {
463 self.lookup = Some(lookup.into());
466 pub(crate) fn label(&mut self, label: impl Into<String>) -> &mut Builder {
467 self.label = label.into();
470 pub(crate) fn trait_name(&mut self, trait_name: impl Into<String>) -> &mut Builder {
471 self.trait_name = Some(trait_name.into());
474 pub(crate) fn insert_text(&mut self, insert_text: impl Into<String>) -> &mut Builder {
475 self.insert_text = Some(insert_text.into());
478 pub(crate) fn insert_snippet(
481 snippet: impl Into<String>,
483 self.is_snippet = true;
484 self.insert_text(snippet)
486 pub(crate) fn kind(&mut self, kind: impl Into<CompletionItemKind>) -> &mut Builder {
487 self.kind = Some(kind.into());
490 pub(crate) fn text_edit(&mut self, edit: TextEdit) -> &mut Builder {
491 self.text_edit = Some(edit);
494 pub(crate) fn snippet_edit(&mut self, _cap: SnippetCap, edit: TextEdit) -> &mut Builder {
495 self.is_snippet = true;
498 pub(crate) fn detail(&mut self, detail: impl Into<String>) -> &mut Builder {
499 self.set_detail(Some(detail))
501 pub(crate) fn set_detail(&mut self, detail: Option<impl Into<String>>) -> &mut Builder {
502 self.detail = detail.map(Into::into);
503 if let Some(detail) = &self.detail {
504 if never!(detail.contains('\n'), "multiline detail:\n{}", detail) {
505 self.detail = Some(detail.splitn(2, '\n').next().unwrap().to_string());
511 pub(crate) fn documentation(&mut self, docs: Documentation) -> &mut Builder {
512 self.set_documentation(Some(docs))
514 pub(crate) fn set_documentation(&mut self, docs: Option<Documentation>) -> &mut Builder {
515 self.documentation = docs.map(Into::into);
518 pub(crate) fn set_deprecated(&mut self, deprecated: bool) -> &mut Builder {
519 self.deprecated = deprecated;
522 pub(crate) fn set_relevance(&mut self, relevance: CompletionRelevance) -> &mut Builder {
523 self.relevance = relevance;
526 pub(crate) fn trigger_call_info(&mut self) -> &mut Builder {
527 self.trigger_call_info = Some(true);
530 pub(crate) fn add_import(&mut self, import_to_add: Option<ImportEdit>) -> &mut Builder {
531 self.import_to_add = import_to_add;
534 pub(crate) fn ref_match(&mut self, mutability: Mutability) -> &mut Builder {
535 self.ref_match = Some(mutability);
542 use itertools::Itertools;
543 use test_utils::assert_eq_text;
545 use super::{CompletionRelevance, CompletionRelevanceTypeMatch};
547 /// Check that these are CompletionRelevance are sorted in ascending order
548 /// by their relevance score.
550 /// We want to avoid making assertions about the absolute score of any
551 /// item, but we do want to assert whether each is >, <, or == to the
554 /// If provided vec![vec![a], vec![b, c], vec![d]], then this will assert:
555 /// a.score < b.score == c.score < d.score
556 fn check_relevance_score_ordered(expected_relevance_order: Vec<Vec<CompletionRelevance>>) {
557 let expected = format!("{:#?}", &expected_relevance_order);
559 let actual_relevance_order = expected_relevance_order
562 .map(|r| (r.score(), r))
563 .sorted_by_key(|(score, _r)| *score)
565 (u32::MIN, vec![vec![]]),
566 |(mut currently_collecting_score, mut out), (score, r)| {
567 if currently_collecting_score == score {
568 out.last_mut().unwrap().push(r);
570 currently_collecting_score = score;
573 (currently_collecting_score, out)
578 let actual = format!("{:#?}", &actual_relevance_order);
580 assert_eq_text!(&expected, &actual);
584 fn relevance_score() {
585 // This test asserts that the relevance score for these items is ascending, and
586 // that any items in the same vec have the same score.
587 let expected_relevance_order = vec![
588 vec![CompletionRelevance::default()],
590 CompletionRelevance { exact_name_match: true, ..CompletionRelevance::default() },
591 CompletionRelevance { is_local: true, ..CompletionRelevance::default() },
593 vec![CompletionRelevance {
594 exact_name_match: true,
596 ..CompletionRelevance::default()
598 vec![CompletionRelevance {
599 type_match: Some(CompletionRelevanceTypeMatch::CouldUnify),
600 ..CompletionRelevance::default()
602 vec![CompletionRelevance {
603 type_match: Some(CompletionRelevanceTypeMatch::Exact),
604 ..CompletionRelevance::default()
606 vec![CompletionRelevance {
607 exact_name_match: true,
608 type_match: Some(CompletionRelevanceTypeMatch::Exact),
609 ..CompletionRelevance::default()
611 vec![CompletionRelevance {
612 exact_name_match: true,
613 type_match: Some(CompletionRelevanceTypeMatch::Exact),
615 ..CompletionRelevance::default()
617 vec![CompletionRelevance {
618 exact_name_match: false,
621 exact_postfix_snippet_match: true,
625 check_relevance_score_ordered(expected_relevance_order);