Complete types

This commit is contained in:
Aleksey Kladov 2018-08-30 20:03:18 +03:00
parent 9fcebbc512
commit 49e14a99ed
3 changed files with 35 additions and 8 deletions

View file

@ -21,11 +21,10 @@ pub fn scope_completion(file: &File, offset: TextUnit) -> Option<Vec<CompletionI
// Insert a fake ident to get a valid parse tree
let file = {
let edit = AtomEdit::insert(offset, "intellijRulezz".to_string());
// Don't bother with completion if incremental reparse fails
file.incremental_reparse(&edit)?
file.reparse(&edit)
};
let name_ref = find_node_at_offset::<ast::NameRef>(file.syntax(), offset)?;
if !is_ident_expr(name_ref) {
if !is_single_segment(name_ref) {
return None;
}
@ -50,11 +49,11 @@ pub fn scope_completion(file: &File, offset: TextUnit) -> Option<Vec<CompletionI
Some(res)
}
fn is_ident_expr(name_ref: ast::NameRef) -> bool {
match ancestors(name_ref.syntax()).filter_map(ast::Expr::cast).next() {
fn is_single_segment(name_ref: ast::NameRef) -> bool {
match ancestors(name_ref.syntax()).filter_map(ast::Path::cast).next() {
None => false,
Some(expr) => {
expr.syntax().range() == name_ref.syntax().range()
Some(path) => {
path.syntax().range() == name_ref.syntax().range()
}
}
}
@ -203,6 +202,15 @@ mod tests {
CompletionItem { name: "quux", snippet: None }]"#);
}
#[test]
fn test_complete_type() {
check_scope_completion(r"
struct Foo;
fn x() -> <|>
", r#"[CompletionItem { name: "Foo", snippet: None },
CompletionItem { name: "x", snippet: None }]"#)
}
#[test]
fn test_completion_kewords() {
check_snippet_completion(r"