mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-13 05:38:46 +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::{
|
use libsyntax2::{
|
||||||
File, TextUnit, AstNode, SyntaxKind::*,
|
File, TextUnit, AstNode, SyntaxKind::*,
|
||||||
ast::{self, LoopBodyOwner},
|
ast::{self, LoopBodyOwner},
|
||||||
|
@ -44,7 +46,7 @@ pub fn scope_completion(file: &File, offset: TextUnit) -> Option<Vec<CompletionI
|
||||||
name: entry.name().to_string(),
|
name: entry.name().to_string(),
|
||||||
snippet: None,
|
snippet: None,
|
||||||
})
|
})
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
Some(res)
|
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>) {
|
fn complete_fn(name_ref: ast::NameRef, scopes: &FnScopes, acc: &mut Vec<CompletionItem>) {
|
||||||
|
let mut shadowed = HashSet::new();
|
||||||
acc.extend(
|
acc.extend(
|
||||||
scopes.scope_chain(name_ref.syntax())
|
scopes.scope_chain(name_ref.syntax())
|
||||||
.flat_map(|scope| scopes.entries(scope).iter())
|
.flat_map(|scope| scopes.entries(scope).iter())
|
||||||
|
.filter(|entry| shadowed.insert(entry.name()))
|
||||||
.map(|entry| CompletionItem {
|
.map(|entry| CompletionItem {
|
||||||
name: entry.name().to_string(),
|
name: entry.name().to_string(),
|
||||||
snippet: None,
|
snippet: None,
|
||||||
})
|
})
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -231,6 +235,20 @@ mod tests {
|
||||||
CompletionItem { name: "x", snippet: None }]"#)
|
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]
|
#[test]
|
||||||
fn test_completion_kewords() {
|
fn test_completion_kewords() {
|
||||||
check_snippet_completion(r"
|
check_snippet_completion(r"
|
||||||
|
|
|
@ -10,7 +10,7 @@ text_unit = "0.1.4"
|
||||||
itertools = "0.7.8"
|
itertools = "0.7.8"
|
||||||
drop_bomb = "0.1.4"
|
drop_bomb = "0.1.4"
|
||||||
parking_lot = "0.6.0"
|
parking_lot = "0.6.0"
|
||||||
smol_str = "0.1.0"
|
smol_str = "0.1.4"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
test_utils = { path = "../test_utils" }
|
test_utils = { path = "../test_utils" }
|
||||||
|
|
Loading…
Reference in a new issue