From cdb9b4cbf417976739de5147938e2c3080e497b9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 31 Aug 2018 15:53:52 +0300 Subject: handle shadowing --- crates/libeditor/src/completion.rs | 22 ++++++++++++++++++++-- crates/libsyntax2/Cargo.toml | 2 +- 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 @@ +use std::collections::HashSet; + use libsyntax2::{ File, TextUnit, AstNode, SyntaxKind::*, ast::{self, LoopBodyOwner}, @@ -44,7 +46,7 @@ pub fn scope_completion(file: &File, offset: TextUnit) -> Option CompletionItem { } fn complete_fn(name_ref: ast::NameRef, scopes: &FnScopes, acc: &mut Vec) { + let mut shadowed = HashSet::new(); acc.extend( scopes.scope_chain(name_ref.syntax()) .flat_map(|scope| scopes.entries(scope).iter()) + .filter(|entry| shadowed.insert(entry.name())) .map(|entry| CompletionItem { name: entry.name().to_string(), snippet: None, }) - ) + ); } #[cfg(test)] @@ -231,6 +235,20 @@ mod tests { CompletionItem { name: "x", snippet: None }]"#) } + #[test] + fn test_complete_shadowing() { + check_scope_completion(r" + fn foo() -> { + let bar = 92; + { + let bar = 62; + <|> + } + } + ", r#"[CompletionItem { name: "bar", snippet: None }, + CompletionItem { name: "foo", snippet: None }]"#) + } + #[test] fn test_completion_kewords() { check_snippet_completion(r" 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" itertools = "0.7.8" drop_bomb = "0.1.4" parking_lot = "0.6.0" -smol_str = "0.1.0" +smol_str = "0.1.4" [dev-dependencies] test_utils = { path = "../test_utils" } -- cgit v1.2.3