diff options
author | Aleksey Kladov <[email protected]> | 2018-12-21 11:38:41 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-12-21 11:38:41 +0000 |
commit | b5c5995bf13da31bb97113a7eea5c138555c2b1b (patch) | |
tree | 81f8eee77b832c49a3c47f5b4fc4738cd83f2284 /crates/ra_analysis/src | |
parent | b0ff6176ed0401251ae9f84d115a888fa4baee89 (diff) |
use builder interface for completion item
Diffstat (limited to 'crates/ra_analysis/src')
-rw-r--r-- | crates/ra_analysis/src/completion.rs | 2 | ||||
-rw-r--r-- | crates/ra_analysis/src/completion/completion_item.rs | 5 | ||||
-rw-r--r-- | crates/ra_analysis/src/completion/reference_completion.rs | 84 |
3 files changed, 36 insertions, 55 deletions
diff --git a/crates/ra_analysis/src/completion.rs b/crates/ra_analysis/src/completion.rs index 6d84558d1..fd7b78c2a 100644 --- a/crates/ra_analysis/src/completion.rs +++ b/crates/ra_analysis/src/completion.rs | |||
@@ -428,7 +428,7 @@ mod tests { | |||
428 | <|> | 428 | <|> |
429 | } | 429 | } |
430 | ", | 430 | ", |
431 | r##"[CompletionItem { label: "Test function", lookup: Some("tfn"), snippet: Some("#[test]\nfn ${1:feature}() {\n$0\n}") }, | 431 | r##"[CompletionItem { label: "Test function", lookup: Some("tfn"), snippet: Some("#[test]\nfn ${1:feature}() {\n $0\n}") }, |
432 | CompletionItem { label: "pub(crate)", lookup: None, snippet: Some("pub(crate) $0") }]"##, | 432 | CompletionItem { label: "pub(crate)", lookup: None, snippet: Some("pub(crate) $0") }]"##, |
433 | ); | 433 | ); |
434 | } | 434 | } |
diff --git a/crates/ra_analysis/src/completion/completion_item.rs b/crates/ra_analysis/src/completion/completion_item.rs index 309b0108d..7edb86436 100644 --- a/crates/ra_analysis/src/completion/completion_item.rs +++ b/crates/ra_analysis/src/completion/completion_item.rs | |||
@@ -19,6 +19,7 @@ impl CompletionItem { | |||
19 | } | 19 | } |
20 | } | 20 | } |
21 | 21 | ||
22 | #[must_use] | ||
22 | pub(crate) struct Builder { | 23 | pub(crate) struct Builder { |
23 | label: String, | 24 | label: String, |
24 | lookup: Option<String>, | 25 | lookup: Option<String>, |
@@ -41,4 +42,8 @@ impl Builder { | |||
41 | self.lookup = Some(lookup.into()); | 42 | self.lookup = Some(lookup.into()); |
42 | self | 43 | self |
43 | } | 44 | } |
45 | pub fn snippet(mut self, snippet: impl Into<String>) -> Builder { | ||
46 | self.snippet = Some(snippet.into()); | ||
47 | self | ||
48 | } | ||
44 | } | 49 | } |
diff --git a/crates/ra_analysis/src/completion/reference_completion.rs b/crates/ra_analysis/src/completion/reference_completion.rs index 457ca13cc..23052295c 100644 --- a/crates/ra_analysis/src/completion/reference_completion.rs +++ b/crates/ra_analysis/src/completion/reference_completion.rs | |||
@@ -125,23 +125,13 @@ fn classify_name_ref(name_ref: ast::NameRef) -> Option<NameRefKind> { | |||
125 | 125 | ||
126 | fn complete_fn(name_ref: ast::NameRef, scopes: &FnScopes, acc: &mut Vec<CompletionItem>) { | 126 | fn complete_fn(name_ref: ast::NameRef, scopes: &FnScopes, acc: &mut Vec<CompletionItem>) { |
127 | let mut shadowed = FxHashSet::default(); | 127 | let mut shadowed = FxHashSet::default(); |
128 | acc.extend( | 128 | scopes |
129 | scopes | 129 | .scope_chain(name_ref.syntax()) |
130 | .scope_chain(name_ref.syntax()) | 130 | .flat_map(|scope| scopes.entries(scope).iter()) |
131 | .flat_map(|scope| scopes.entries(scope).iter()) | 131 | .filter(|entry| shadowed.insert(entry.name())) |
132 | .filter(|entry| shadowed.insert(entry.name())) | 132 | .for_each(|entry| CompletionItem::new(entry.name().to_string()).add_to(acc)); |
133 | .map(|entry| CompletionItem { | ||
134 | label: entry.name().to_string(), | ||
135 | lookup: None, | ||
136 | snippet: None, | ||
137 | }), | ||
138 | ); | ||
139 | if scopes.self_param.is_some() { | 133 | if scopes.self_param.is_some() { |
140 | acc.push(CompletionItem { | 134 | CompletionItem::new("self").add_to(acc); |
141 | label: "self".to_string(), | ||
142 | lookup: None, | ||
143 | snippet: None, | ||
144 | }) | ||
145 | } | 135 | } |
146 | } | 136 | } |
147 | 137 | ||
@@ -164,32 +154,26 @@ fn complete_path( | |||
164 | _ => return Ok(()), | 154 | _ => return Ok(()), |
165 | }; | 155 | }; |
166 | let module_scope = target_module.scope(db)?; | 156 | let module_scope = target_module.scope(db)?; |
167 | let completions = module_scope.entries().map(|(name, _res)| CompletionItem { | 157 | module_scope |
168 | label: name.to_string(), | 158 | .entries() |
169 | lookup: None, | 159 | .for_each(|(name, _res)| CompletionItem::new(name.to_string()).add_to(acc)); |
170 | snippet: None, | ||
171 | }); | ||
172 | acc.extend(completions); | ||
173 | Ok(()) | 160 | Ok(()) |
174 | } | 161 | } |
175 | 162 | ||
176 | fn complete_mod_item_snippets(acc: &mut Vec<CompletionItem>) { | 163 | fn complete_mod_item_snippets(acc: &mut Vec<CompletionItem>) { |
177 | acc.push(CompletionItem { | 164 | CompletionItem::new("Test function") |
178 | label: "Test function".to_string(), | 165 | .lookup_by("tfn") |
179 | lookup: Some("tfn".to_string()), | 166 | .snippet( |
180 | snippet: Some( | 167 | "\ |
181 | "#[test]\n\ | 168 | #[test] |
182 | fn ${1:feature}() {\n\ | 169 | fn ${1:feature}() { |
183 | $0\n\ | 170 | $0 |
184 | }" | 171 | }", |
185 | .to_string(), | 172 | ) |
186 | ), | 173 | .add_to(acc); |
187 | }); | 174 | CompletionItem::new("pub(crate)") |
188 | acc.push(CompletionItem { | 175 | .snippet("pub(crate) $0") |
189 | label: "pub(crate)".to_string(), | 176 | .add_to(acc); |
190 | lookup: None, | ||
191 | snippet: Some("pub(crate) $0".to_string()), | ||
192 | }) | ||
193 | } | 177 | } |
194 | 178 | ||
195 | fn complete_expr_keywords( | 179 | fn complete_expr_keywords( |
@@ -270,23 +254,15 @@ fn complete_return(fn_def: ast::FnDef, name_ref: ast::NameRef) -> Option<Complet | |||
270 | Some(keyword("return", snip)) | 254 | Some(keyword("return", snip)) |
271 | } | 255 | } |
272 | 256 | ||
273 | fn keyword(kw: &str, snip: &str) -> CompletionItem { | 257 | fn keyword(kw: &str, snippet: &str) -> CompletionItem { |
274 | CompletionItem { | 258 | CompletionItem::new(kw).snippet(snippet).build() |
275 | label: kw.to_string(), | ||
276 | lookup: None, | ||
277 | snippet: Some(snip.to_string()), | ||
278 | } | ||
279 | } | 259 | } |
280 | 260 | ||
281 | fn complete_expr_snippets(acc: &mut Vec<CompletionItem>) { | 261 | fn complete_expr_snippets(acc: &mut Vec<CompletionItem>) { |
282 | acc.push(CompletionItem { | 262 | CompletionItem::new("pd") |
283 | label: "pd".to_string(), | 263 | .snippet("eprintln!(\"$0 = {:?}\", $0);") |
284 | lookup: None, | 264 | .add_to(acc); |
285 | snippet: Some("eprintln!(\"$0 = {:?}\", $0);".to_string()), | 265 | CompletionItem::new("ppd") |
286 | }); | 266 | .snippet("eprintln!(\"$0 = {:#?}\", $0);") |
287 | acc.push(CompletionItem { | 267 | .add_to(acc); |
288 | label: "ppd".to_string(), | ||
289 | lookup: None, | ||
290 | snippet: Some("eprintln!(\"$0 = {:#?}\", $0);".to_string()), | ||
291 | }); | ||
292 | } | 268 | } |