diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-12-21 23:28:47 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-12-21 23:28:47 +0000 |
commit | 4e4ca27eabac6a9c97dc07baf9a067efdfc63384 (patch) | |
tree | 83c75bdefa688a220054bf671ffe1692887d6dd4 /crates/ra_analysis/src/completion/completion_item.rs | |
parent | e4d0930d9c6478f7aa069401fb7e28ab7c80fd14 (diff) | |
parent | ea763c73b8d89edbf716805c62cf31b00e2e1a4f (diff) |
Merge #319
319: Completion icons r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_analysis/src/completion/completion_item.rs')
-rw-r--r-- | crates/ra_analysis/src/completion/completion_item.rs | 54 |
1 files changed, 45 insertions, 9 deletions
diff --git a/crates/ra_analysis/src/completion/completion_item.rs b/crates/ra_analysis/src/completion/completion_item.rs index d5d751759..911f08468 100644 --- a/crates/ra_analysis/src/completion/completion_item.rs +++ b/crates/ra_analysis/src/completion/completion_item.rs | |||
@@ -1,13 +1,17 @@ | |||
1 | use crate::db; | ||
2 | |||
1 | /// `CompletionItem` describes a single completion variant in the editor pop-up. | 3 | /// `CompletionItem` describes a single completion variant in the editor pop-up. |
2 | /// It is basically a POD with various properties. To construct a | 4 | /// It is basically a POD with various properties. To construct a |
3 | /// `CompletionItem`, use `new` method and the `Builder` struct. | 5 | /// `CompletionItem`, use `new` method and the `Builder` struct. |
4 | #[derive(Debug)] | 6 | #[derive(Debug)] |
5 | pub struct CompletionItem { | 7 | pub struct CompletionItem { |
8 | /// Used only internally in tests, to check only specific kind of | ||
9 | /// completion. | ||
10 | completion_kind: CompletionKind, | ||
6 | label: String, | 11 | label: String, |
7 | lookup: Option<String>, | 12 | lookup: Option<String>, |
8 | snippet: Option<String>, | 13 | snippet: Option<String>, |
9 | /// Used only internally in test, to check only specific kind of completion. | 14 | kind: Option<CompletionItemKind>, |
10 | kind: CompletionKind, | ||
11 | } | 15 | } |
12 | 16 | ||
13 | pub enum InsertText { | 17 | pub enum InsertText { |
@@ -15,6 +19,15 @@ pub enum InsertText { | |||
15 | Snippet { text: String }, | 19 | Snippet { text: String }, |
16 | } | 20 | } |
17 | 21 | ||
22 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
23 | pub enum CompletionItemKind { | ||
24 | Snippet, | ||
25 | Keyword, | ||
26 | Module, | ||
27 | Function, | ||
28 | Binding, | ||
29 | } | ||
30 | |||
18 | #[derive(Debug, PartialEq, Eq)] | 31 | #[derive(Debug, PartialEq, Eq)] |
19 | pub(crate) enum CompletionKind { | 32 | pub(crate) enum CompletionKind { |
20 | /// Parser-based keyword completion. | 33 | /// Parser-based keyword completion. |
@@ -24,17 +37,17 @@ pub(crate) enum CompletionKind { | |||
24 | /// "Secret sauce" completions. | 37 | /// "Secret sauce" completions. |
25 | Magic, | 38 | Magic, |
26 | Snippet, | 39 | Snippet, |
27 | Unspecified, | ||
28 | } | 40 | } |
29 | 41 | ||
30 | impl CompletionItem { | 42 | impl CompletionItem { |
31 | pub(crate) fn new(label: impl Into<String>) -> Builder { | 43 | pub(crate) fn new(completion_kind: CompletionKind, label: impl Into<String>) -> Builder { |
32 | let label = label.into(); | 44 | let label = label.into(); |
33 | Builder { | 45 | Builder { |
46 | completion_kind, | ||
34 | label, | 47 | label, |
35 | lookup: None, | 48 | lookup: None, |
36 | snippet: None, | 49 | snippet: None, |
37 | kind: CompletionKind::Unspecified, | 50 | kind: None, |
38 | } | 51 | } |
39 | } | 52 | } |
40 | /// What user sees in pop-up in the UI. | 53 | /// What user sees in pop-up in the UI. |
@@ -57,15 +70,20 @@ impl CompletionItem { | |||
57 | Some(it) => InsertText::Snippet { text: it.clone() }, | 70 | Some(it) => InsertText::Snippet { text: it.clone() }, |
58 | } | 71 | } |
59 | } | 72 | } |
73 | |||
74 | pub fn kind(&self) -> Option<CompletionItemKind> { | ||
75 | self.kind | ||
76 | } | ||
60 | } | 77 | } |
61 | 78 | ||
62 | /// A helper to make `CompletionItem`s. | 79 | /// A helper to make `CompletionItem`s. |
63 | #[must_use] | 80 | #[must_use] |
64 | pub(crate) struct Builder { | 81 | pub(crate) struct Builder { |
82 | completion_kind: CompletionKind, | ||
65 | label: String, | 83 | label: String, |
66 | lookup: Option<String>, | 84 | lookup: Option<String>, |
67 | snippet: Option<String>, | 85 | snippet: Option<String>, |
68 | kind: CompletionKind, | 86 | kind: Option<CompletionItemKind>, |
69 | } | 87 | } |
70 | 88 | ||
71 | impl Builder { | 89 | impl Builder { |
@@ -79,6 +97,7 @@ impl Builder { | |||
79 | lookup: self.lookup, | 97 | lookup: self.lookup, |
80 | snippet: self.snippet, | 98 | snippet: self.snippet, |
81 | kind: self.kind, | 99 | kind: self.kind, |
100 | completion_kind: self.completion_kind, | ||
82 | } | 101 | } |
83 | } | 102 | } |
84 | pub(crate) fn lookup_by(mut self, lookup: impl Into<String>) -> Builder { | 103 | pub(crate) fn lookup_by(mut self, lookup: impl Into<String>) -> Builder { |
@@ -89,8 +108,25 @@ impl Builder { | |||
89 | self.snippet = Some(snippet.into()); | 108 | self.snippet = Some(snippet.into()); |
90 | self | 109 | self |
91 | } | 110 | } |
92 | pub(crate) fn kind(mut self, kind: CompletionKind) -> Builder { | 111 | pub(crate) fn kind(mut self, kind: CompletionItemKind) -> Builder { |
93 | self.kind = kind; | 112 | self.kind = Some(kind); |
113 | self | ||
114 | } | ||
115 | pub(crate) fn from_resolution( | ||
116 | mut self, | ||
117 | db: &db::RootDatabase, | ||
118 | resolution: &hir::Resolution, | ||
119 | ) -> Builder { | ||
120 | if let Some(def_id) = resolution.def_id { | ||
121 | if let Ok(def) = def_id.resolve(db) { | ||
122 | let kind = match def { | ||
123 | hir::Def::Module(..) => CompletionItemKind::Module, | ||
124 | hir::Def::Function(..) => CompletionItemKind::Function, | ||
125 | _ => return self, | ||
126 | }; | ||
127 | self.kind = Some(kind); | ||
128 | } | ||
129 | } | ||
94 | self | 130 | self |
95 | } | 131 | } |
96 | } | 132 | } |
@@ -154,7 +190,7 @@ impl Completions { | |||
154 | fn debug_render(&self, kind: CompletionKind) -> String { | 190 | fn debug_render(&self, kind: CompletionKind) -> String { |
155 | let mut res = String::new(); | 191 | let mut res = String::new(); |
156 | for c in self.buf.iter() { | 192 | for c in self.buf.iter() { |
157 | if c.kind == kind { | 193 | if c.completion_kind == kind { |
158 | if let Some(lookup) = &c.lookup { | 194 | if let Some(lookup) = &c.lookup { |
159 | res.push_str(lookup); | 195 | res.push_str(lookup); |
160 | res.push_str(&format!(" {:?}", c.label)); | 196 | res.push_str(&format!(" {:?}", c.label)); |