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"

View file

@ -1278,6 +1278,24 @@ impl<'a> AstNode<'a> for Pat<'a> {
impl<'a> Pat<'a> {}
// Path
#[derive(Debug, Clone, Copy)]
pub struct Path<'a> {
syntax: SyntaxNodeRef<'a>,
}
impl<'a> AstNode<'a> for Path<'a> {
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() {
PATH => Some(Path { syntax }),
_ => None,
}
}
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
}
impl<'a> Path<'a> {}
// PathExpr
#[derive(Debug, Clone, Copy)]
pub struct PathExpr<'a> {

View file

@ -513,6 +513,7 @@ Grammar(
collections: [
["args", "Expr"]
]
)
),
"Path": (),
},
)