mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-12 21:28:51 +00:00
handle shadowing
This commit is contained in:
parent
78d60a549d
commit
cdb9b4cbf4
2 changed files with 21 additions and 3 deletions
|
@ -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<Vec<CompletionI
|
|||
name: entry.name().to_string(),
|
||||
snippet: None,
|
||||
})
|
||||
)
|
||||
);
|
||||
}
|
||||
Some(res)
|
||||
}
|
||||
|
@ -130,14 +132,16 @@ fn keyword(kw: &str, snip: &str) -> CompletionItem {
|
|||
}
|
||||
|
||||
fn complete_fn(name_ref: ast::NameRef, scopes: &FnScopes, acc: &mut Vec<CompletionItem>) {
|
||||
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"
|
||||
|
|
|
@ -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" }
|
||||
|
|
Loading…
Reference in a new issue