aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-08-31 13:53:52 +0100
committerAleksey Kladov <[email protected]>2018-08-31 13:53:52 +0100
commitcdb9b4cbf417976739de5147938e2c3080e497b9 (patch)
tree2a23465e2a00ab487daea4ebc2b5e993a76f59b6
parent78d60a549d0711ffea10e2898bff46ebc739f637 (diff)
handle shadowing
-rw-r--r--crates/libeditor/src/completion.rs22
-rw-r--r--crates/libsyntax2/Cargo.toml2
2 files changed, 21 insertions, 3 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 @@
1use std::collections::HashSet;
2
1use libsyntax2::{ 3use 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
132fn complete_fn(name_ref: ast::NameRef, scopes: &FnScopes, acc: &mut Vec<CompletionItem>) { 134fn 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() {
diff --git a/crates/libsyntax2/Cargo.toml b/crates/libsyntax2/Cargo.toml
index 3e5a83290..abb711feb 100644
--- a/crates/libsyntax2/Cargo.toml
+++ b/crates/libsyntax2/Cargo.toml
@@ -10,7 +10,7 @@ text_unit = "0.1.4"
10itertools = "0.7.8" 10itertools = "0.7.8"
11drop_bomb = "0.1.4" 11drop_bomb = "0.1.4"
12parking_lot = "0.6.0" 12parking_lot = "0.6.0"
13smol_str = "0.1.0" 13smol_str = "0.1.4"
14 14
15[dev-dependencies] 15[dev-dependencies]
16test_utils = { path = "../test_utils" } 16test_utils = { path = "../test_utils" }