aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/completion/completion_item.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide/src/completion/completion_item.rs')
-rw-r--r--crates/ra_ide/src/completion/completion_item.rs38
1 files changed, 35 insertions, 3 deletions
diff --git a/crates/ra_ide/src/completion/completion_item.rs b/crates/ra_ide/src/completion/completion_item.rs
index bc0f1aff5..5936fb8f7 100644
--- a/crates/ra_ide/src/completion/completion_item.rs
+++ b/crates/ra_ide/src/completion/completion_item.rs
@@ -2,6 +2,7 @@
2 2
3use std::fmt; 3use std::fmt;
4 4
5use super::completion_config::SnippetCap;
5use hir::Documentation; 6use hir::Documentation;
6use ra_syntax::TextRange; 7use ra_syntax::TextRange;
7use ra_text_edit::TextEdit; 8use ra_text_edit::TextEdit;
@@ -51,6 +52,9 @@ pub struct CompletionItem {
51 /// If completing a function call, ask the editor to show parameter popup 52 /// If completing a function call, ask the editor to show parameter popup
52 /// after completion. 53 /// after completion.
53 trigger_call_info: bool, 54 trigger_call_info: bool,
55
56 /// Score is useful to pre select or display in better order completion items
57 score: Option<CompletionScore>,
54} 58}
55 59
56// We use custom debug for CompletionItem to make `insta`'s diffs more readable. 60// We use custom debug for CompletionItem to make `insta`'s diffs more readable.
@@ -80,6 +84,9 @@ impl fmt::Debug for CompletionItem {
80 if self.deprecated { 84 if self.deprecated {
81 s.field("deprecated", &true); 85 s.field("deprecated", &true);
82 } 86 }
87 if let Some(score) = &self.score {
88 s.field("score", score);
89 }
83 if self.trigger_call_info { 90 if self.trigger_call_info {
84 s.field("trigger_call_info", &true); 91 s.field("trigger_call_info", &true);
85 } 92 }
@@ -87,6 +94,14 @@ impl fmt::Debug for CompletionItem {
87 } 94 }
88} 95}
89 96
97#[derive(Debug, Clone, Copy)]
98pub enum CompletionScore {
99 /// If only type match
100 TypeMatch,
101 /// If type and name match
102 TypeAndNameMatch,
103}
104
90#[derive(Debug, Clone, Copy, PartialEq, Eq)] 105#[derive(Debug, Clone, Copy, PartialEq, Eq)]
91pub enum CompletionItemKind { 106pub enum CompletionItemKind {
92 Snippet, 107 Snippet,
@@ -106,6 +121,7 @@ pub enum CompletionItemKind {
106 Method, 121 Method,
107 TypeParam, 122 TypeParam,
108 Macro, 123 Macro,
124 Attribute,
109} 125}
110 126
111#[derive(Debug, PartialEq, Eq, Copy, Clone)] 127#[derive(Debug, PartialEq, Eq, Copy, Clone)]
@@ -119,6 +135,7 @@ pub(crate) enum CompletionKind {
119 Snippet, 135 Snippet,
120 Postfix, 136 Postfix,
121 BuiltinType, 137 BuiltinType,
138 Attribute,
122} 139}
123 140
124#[derive(Debug, PartialEq, Eq, Copy, Clone)] 141#[derive(Debug, PartialEq, Eq, Copy, Clone)]
@@ -147,6 +164,7 @@ impl CompletionItem {
147 text_edit: None, 164 text_edit: None,
148 deprecated: None, 165 deprecated: None,
149 trigger_call_info: None, 166 trigger_call_info: None,
167 score: None,
150 } 168 }
151 } 169 }
152 /// What user sees in pop-up in the UI. 170 /// What user sees in pop-up in the UI.
@@ -175,7 +193,7 @@ impl CompletionItem {
175 } 193 }
176 /// What string is used for filtering. 194 /// What string is used for filtering.
177 pub fn lookup(&self) -> &str { 195 pub fn lookup(&self) -> &str {
178 self.lookup.as_deref().unwrap_or_else(|| self.label()) 196 self.lookup.as_deref().unwrap_or(&self.label)
179 } 197 }
180 198
181 pub fn kind(&self) -> Option<CompletionItemKind> { 199 pub fn kind(&self) -> Option<CompletionItemKind> {
@@ -186,6 +204,10 @@ impl CompletionItem {
186 self.deprecated 204 self.deprecated
187 } 205 }
188 206
207 pub fn score(&self) -> Option<CompletionScore> {
208 self.score
209 }
210
189 pub fn trigger_call_info(&self) -> bool { 211 pub fn trigger_call_info(&self) -> bool {
190 self.trigger_call_info 212 self.trigger_call_info
191 } 213 }
@@ -206,6 +228,7 @@ pub(crate) struct Builder {
206 text_edit: Option<TextEdit>, 228 text_edit: Option<TextEdit>,
207 deprecated: Option<bool>, 229 deprecated: Option<bool>,
208 trigger_call_info: Option<bool>, 230 trigger_call_info: Option<bool>,
231 score: Option<CompletionScore>,
209} 232}
210 233
211impl Builder { 234impl Builder {
@@ -235,6 +258,7 @@ impl Builder {
235 completion_kind: self.completion_kind, 258 completion_kind: self.completion_kind,
236 deprecated: self.deprecated.unwrap_or(false), 259 deprecated: self.deprecated.unwrap_or(false),
237 trigger_call_info: self.trigger_call_info.unwrap_or(false), 260 trigger_call_info: self.trigger_call_info.unwrap_or(false),
261 score: self.score,
238 } 262 }
239 } 263 }
240 pub(crate) fn lookup_by(mut self, lookup: impl Into<String>) -> Builder { 264 pub(crate) fn lookup_by(mut self, lookup: impl Into<String>) -> Builder {
@@ -249,7 +273,11 @@ impl Builder {
249 self.insert_text = Some(insert_text.into()); 273 self.insert_text = Some(insert_text.into());
250 self 274 self
251 } 275 }
252 pub(crate) fn insert_snippet(mut self, snippet: impl Into<String>) -> Builder { 276 pub(crate) fn insert_snippet(
277 mut self,
278 _cap: SnippetCap,
279 snippet: impl Into<String>,
280 ) -> Builder {
253 self.insert_text_format = InsertTextFormat::Snippet; 281 self.insert_text_format = InsertTextFormat::Snippet;
254 self.insert_text(snippet) 282 self.insert_text(snippet)
255 } 283 }
@@ -261,7 +289,7 @@ impl Builder {
261 self.text_edit = Some(edit); 289 self.text_edit = Some(edit);
262 self 290 self
263 } 291 }
264 pub(crate) fn snippet_edit(mut self, edit: TextEdit) -> Builder { 292 pub(crate) fn snippet_edit(mut self, _cap: SnippetCap, edit: TextEdit) -> Builder {
265 self.insert_text_format = InsertTextFormat::Snippet; 293 self.insert_text_format = InsertTextFormat::Snippet;
266 self.text_edit(edit) 294 self.text_edit(edit)
267 } 295 }
@@ -285,6 +313,10 @@ impl Builder {
285 self.deprecated = Some(deprecated); 313 self.deprecated = Some(deprecated);
286 self 314 self
287 } 315 }
316 pub(crate) fn set_score(mut self, score: CompletionScore) -> Builder {
317 self.score = Some(score);
318 self
319 }
288 pub(crate) fn trigger_call_info(mut self) -> Builder { 320 pub(crate) fn trigger_call_info(mut self) -> Builder {
289 self.trigger_call_info = Some(true); 321 self.trigger_call_info = Some(true);
290 self 322 self