1 //! See `CompletionItem` structure.
5 use hir::{Documentation, Mutability};
7 use text_edit::TextEdit;
9 use crate::config::SnippetCap;
11 /// `CompletionItem` describes a single completion variant in the editor pop-up.
12 /// It is basically a POD with various properties. To construct a
13 /// `CompletionItem`, use `new` method and the `Builder` struct.
14 pub struct CompletionItem {
15 /// Used only internally in tests, to check only specific kind of
16 /// completion (postfix, keyword, reference, etc).
18 pub(crate) completion_kind: CompletionKind,
19 /// Label in the completion pop up which identifies completion.
21 /// Range of identifier that is being completed.
23 /// It should be used primarily for UI, but we also use this to convert
24 /// genetic TextEdit into LSP's completion edit (see conv.rs).
26 /// `source_range` must contain the completion offset. `insert_text` should
27 /// start with what `source_range` points to, or VSCode will filter out the
28 /// completion silently.
29 source_range: TextRange,
30 /// What happens when user selects this item.
32 /// Typically, replaces `source_range` with new identifier.
35 insert_text_format: InsertTextFormat,
37 /// What item (struct, function, etc) are we completing.
38 kind: Option<CompletionItemKind>,
40 /// Lookup is used to check if completion item indeed can complete current
43 /// That is, in `foo.bar<|>` lookup of `abracadabra` will be accepted (it
44 /// contains `bar` sub sequence), and `quux` will rejected.
45 lookup: Option<String>,
47 /// Additional info to show in the UI pop up.
48 detail: Option<String>,
49 documentation: Option<Documentation>,
51 /// Whether this item is marked as deprecated
54 /// If completing a function call, ask the editor to show parameter popup
56 trigger_call_info: bool,
58 /// Score is useful to pre select or display in better order completion items
59 score: Option<CompletionScore>,
61 /// Indicates that a reference or mutable reference to this variable is a
63 ref_match: Option<(Mutability, CompletionScore)>,
66 // We use custom debug for CompletionItem to make snapshot tests more readable.
67 impl fmt::Debug for CompletionItem {
68 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
69 let mut s = f.debug_struct("CompletionItem");
70 s.field("label", &self.label()).field("source_range", &self.source_range());
71 if self.text_edit().len() == 1 {
72 let atom = &self.text_edit().iter().next().unwrap();
73 s.field("delete", &atom.delete);
74 s.field("insert", &atom.insert);
76 s.field("text_edit", &self.text_edit);
78 if let Some(kind) = self.kind().as_ref() {
79 s.field("kind", kind);
81 if self.lookup() != self.label() {
82 s.field("lookup", &self.lookup());
84 if let Some(detail) = self.detail() {
85 s.field("detail", &detail);
87 if let Some(documentation) = self.documentation() {
88 s.field("documentation", &documentation);
91 s.field("deprecated", &true);
93 if let Some(score) = &self.score {
94 s.field("score", score);
96 if self.trigger_call_info {
97 s.field("trigger_call_info", &true);
103 #[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq)]
104 pub enum CompletionScore {
105 /// If only type match
107 /// If type and name match
111 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
112 pub enum CompletionItemKind {
134 impl CompletionItemKind {
136 pub(crate) fn tag(&self) -> &'static str {
138 CompletionItemKind::Attribute => "at",
139 CompletionItemKind::Binding => "bn",
140 CompletionItemKind::BuiltinType => "bt",
141 CompletionItemKind::Const => "ct",
142 CompletionItemKind::Enum => "en",
143 CompletionItemKind::EnumVariant => "ev",
144 CompletionItemKind::Field => "fd",
145 CompletionItemKind::Function => "fn",
146 CompletionItemKind::Keyword => "kw",
147 CompletionItemKind::Macro => "ma",
148 CompletionItemKind::Method => "me",
149 CompletionItemKind::Module => "md",
150 CompletionItemKind::Snippet => "sn",
151 CompletionItemKind::Static => "sc",
152 CompletionItemKind::Struct => "st",
153 CompletionItemKind::Trait => "tt",
154 CompletionItemKind::TypeAlias => "ta",
155 CompletionItemKind::TypeParam => "tp",
156 CompletionItemKind::UnresolvedReference => "??",
161 #[derive(Debug, PartialEq, Eq, Copy, Clone)]
162 pub(crate) enum CompletionKind {
163 /// Parser-based keyword completion.
165 /// Your usual "complete all valid identifiers".
167 /// "Secret sauce" completions.
175 #[derive(Debug, PartialEq, Eq, Copy, Clone)]
176 pub enum InsertTextFormat {
181 impl CompletionItem {
183 completion_kind: CompletionKind,
184 source_range: TextRange,
185 label: impl Into<String>,
187 let label = label.into();
193 insert_text_format: InsertTextFormat::PlainText,
200 trigger_call_info: None,
205 /// What user sees in pop-up in the UI.
206 pub fn label(&self) -> &str {
209 pub fn source_range(&self) -> TextRange {
213 pub fn insert_text_format(&self) -> InsertTextFormat {
214 self.insert_text_format
217 pub fn text_edit(&self) -> &TextEdit {
221 /// Short one-line additional information, like a type
222 pub fn detail(&self) -> Option<&str> {
223 self.detail.as_deref()
226 pub fn documentation(&self) -> Option<Documentation> {
227 self.documentation.clone()
229 /// What string is used for filtering.
230 pub fn lookup(&self) -> &str {
231 self.lookup.as_deref().unwrap_or(&self.label)
234 pub fn kind(&self) -> Option<CompletionItemKind> {
238 pub fn deprecated(&self) -> bool {
242 pub fn score(&self) -> Option<CompletionScore> {
246 pub fn trigger_call_info(&self) -> bool {
247 self.trigger_call_info
250 pub fn ref_match(&self) -> Option<(Mutability, CompletionScore)> {
255 /// A helper to make `CompletionItem`s.
258 pub(crate) struct Builder {
259 source_range: TextRange,
260 completion_kind: CompletionKind,
262 insert_text: Option<String>,
263 insert_text_format: InsertTextFormat,
264 detail: Option<String>,
265 documentation: Option<Documentation>,
266 lookup: Option<String>,
267 kind: Option<CompletionItemKind>,
268 text_edit: Option<TextEdit>,
269 deprecated: Option<bool>,
270 trigger_call_info: Option<bool>,
271 score: Option<CompletionScore>,
272 ref_match: Option<(Mutability, CompletionScore)>,
276 pub(crate) fn build(self) -> CompletionItem {
277 let label = self.label;
278 let text_edit = match self.text_edit {
280 None => TextEdit::replace(
282 self.insert_text.unwrap_or_else(|| label.clone()),
287 source_range: self.source_range,
289 insert_text_format: self.insert_text_format,
292 documentation: self.documentation,
295 completion_kind: self.completion_kind,
296 deprecated: self.deprecated.unwrap_or(false),
297 trigger_call_info: self.trigger_call_info.unwrap_or(false),
299 ref_match: self.ref_match,
302 pub(crate) fn lookup_by(mut self, lookup: impl Into<String>) -> Builder {
303 self.lookup = Some(lookup.into());
306 pub(crate) fn label(mut self, label: impl Into<String>) -> Builder {
307 self.label = label.into();
310 pub(crate) fn insert_text(mut self, insert_text: impl Into<String>) -> Builder {
311 self.insert_text = Some(insert_text.into());
314 pub(crate) fn insert_snippet(
317 snippet: impl Into<String>,
319 self.insert_text_format = InsertTextFormat::Snippet;
320 self.insert_text(snippet)
322 pub(crate) fn kind(mut self, kind: CompletionItemKind) -> Builder {
323 self.kind = Some(kind);
326 pub(crate) fn text_edit(mut self, edit: TextEdit) -> Builder {
327 self.text_edit = Some(edit);
330 pub(crate) fn snippet_edit(mut self, _cap: SnippetCap, edit: TextEdit) -> Builder {
331 self.insert_text_format = InsertTextFormat::Snippet;
335 pub(crate) fn detail(self, detail: impl Into<String>) -> Builder {
336 self.set_detail(Some(detail))
338 pub(crate) fn set_detail(mut self, detail: Option<impl Into<String>>) -> Builder {
339 self.detail = detail.map(Into::into);
343 pub(crate) fn documentation(self, docs: Documentation) -> Builder {
344 self.set_documentation(Some(docs))
346 pub(crate) fn set_documentation(mut self, docs: Option<Documentation>) -> Builder {
347 self.documentation = docs.map(Into::into);
350 pub(crate) fn set_deprecated(mut self, deprecated: bool) -> Builder {
351 self.deprecated = Some(deprecated);
354 pub(crate) fn set_score(mut self, score: CompletionScore) -> Builder {
355 self.score = Some(score);
358 pub(crate) fn trigger_call_info(mut self) -> Builder {
359 self.trigger_call_info = Some(true);
362 pub(crate) fn set_ref_match(
364 ref_match: Option<(Mutability, CompletionScore)>,
366 self.ref_match = ref_match;
371 impl<'a> Into<CompletionItem> for Builder {
372 fn into(self) -> CompletionItem {