diff options
author | Aleksey Kladov <[email protected]> | 2018-08-31 13:53:52 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-08-31 13:53:52 +0100 |
commit | cdb9b4cbf417976739de5147938e2c3080e497b9 (patch) | |
tree | 2a23465e2a00ab487daea4ebc2b5e993a76f59b6 /crates/libeditor/src | |
parent | 78d60a549d0711ffea10e2898bff46ebc739f637 (diff) |
handle shadowing
Diffstat (limited to 'crates/libeditor/src')
-rw-r--r-- | crates/libeditor/src/completion.rs | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/crates/libeditor/src/completion.rs b/crates/libeditor/src/completion.rs index 42cc656ee..06a607265 100644 --- a/crates/libeditor/src/completion.rs +++ b/crates/libeditor/src/completion.rs | |||
@@ -1,3 +1,5 @@ | |||
1 | use std::collections::HashSet; | ||
2 | |||
1 | use libsyntax2::{ | 3 | use libsyntax2::{ |
2 | File, TextUnit, AstNode, SyntaxKind::*, | 4 | File, TextUnit, AstNode, SyntaxKind::*, |
3 | ast::{self, LoopBodyOwner}, | 5 | ast::{self, LoopBodyOwner}, |
@@ -44,7 +46,7 @@ pub fn scope_completion(file: &File, offset: TextUnit) -> Option<Vec<CompletionI | |||
44 | name: entry.name().to_string(), | 46 | name: entry.name().to_string(), |
45 | snippet: None, | 47 | snippet: None, |
46 | }) | 48 | }) |
47 | ) | 49 | ); |
48 | } | 50 | } |
49 | Some(res) | 51 | Some(res) |
50 | } | 52 | } |
@@ -130,14 +132,16 @@ fn keyword(kw: &str, snip: &str) -> CompletionItem { | |||
130 | } | 132 | } |
131 | 133 | ||
132 | fn complete_fn(name_ref: ast::NameRef, scopes: &FnScopes, acc: &mut Vec<CompletionItem>) { | 134 | fn complete_fn(name_ref: ast::NameRef, scopes: &FnScopes, acc: &mut Vec<CompletionItem>) { |
135 | let mut shadowed = HashSet::new(); | ||
133 | acc.extend( | 136 | acc.extend( |
134 | scopes.scope_chain(name_ref.syntax()) | 137 | scopes.scope_chain(name_ref.syntax()) |
135 | .flat_map(|scope| scopes.entries(scope).iter()) | 138 | .flat_map(|scope| scopes.entries(scope).iter()) |
139 | .filter(|entry| shadowed.insert(entry.name())) | ||
136 | .map(|entry| CompletionItem { | 140 | .map(|entry| CompletionItem { |
137 | name: entry.name().to_string(), | 141 | name: entry.name().to_string(), |
138 | snippet: None, | 142 | snippet: None, |
139 | }) | 143 | }) |
140 | ) | 144 | ); |
141 | } | 145 | } |
142 | 146 | ||
143 | #[cfg(test)] | 147 | #[cfg(test)] |
@@ -232,6 +236,20 @@ mod tests { | |||
232 | } | 236 | } |
233 | 237 | ||
234 | #[test] | 238 | #[test] |
239 | fn test_complete_shadowing() { | ||
240 | check_scope_completion(r" | ||
241 | fn foo() -> { | ||
242 | let bar = 92; | ||
243 | { | ||
244 | let bar = 62; | ||
245 | <|> | ||
246 | } | ||
247 | } | ||
248 | ", r#"[CompletionItem { name: "bar", snippet: None }, | ||
249 | CompletionItem { name: "foo", snippet: None }]"#) | ||
250 | } | ||
251 | |||
252 | #[test] | ||
235 | fn test_completion_kewords() { | 253 | fn test_completion_kewords() { |
236 | check_snippet_completion(r" | 254 | check_snippet_completion(r" |
237 | fn quux() { | 255 | fn quux() { |