From 49e14a99ed4d0baf849bbd5766f6c16e7d37930c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Aug 2018 20:03:18 +0300 Subject: Complete types --- crates/libeditor/src/completion.rs | 22 +++++++++++++++------- crates/libsyntax2/src/ast/generated.rs | 18 ++++++++++++++++++ crates/libsyntax2/src/grammar.ron | 3 ++- 3 files changed, 35 insertions(+), 8 deletions(-) (limited to 'crates') diff --git a/crates/libeditor/src/completion.rs b/crates/libeditor/src/completion.rs index d95c40773..f733cd2b2 100644 --- a/crates/libeditor/src/completion.rs +++ b/crates/libeditor/src/completion.rs @@ -21,11 +21,10 @@ pub fn scope_completion(file: &File, offset: TextUnit) -> Option(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 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" diff --git a/crates/libsyntax2/src/ast/generated.rs b/crates/libsyntax2/src/ast/generated.rs index d72e2091a..c2a22c8fc 100644 --- a/crates/libsyntax2/src/ast/generated.rs +++ b/crates/libsyntax2/src/ast/generated.rs @@ -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 { + 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> { diff --git a/crates/libsyntax2/src/grammar.ron b/crates/libsyntax2/src/grammar.ron index 3c293d3e4..0bb40f5ab 100644 --- a/crates/libsyntax2/src/grammar.ron +++ b/crates/libsyntax2/src/grammar.ron @@ -513,6 +513,7 @@ Grammar( collections: [ ["args", "Expr"] ] - ) + ), + "Path": (), }, ) -- cgit v1.2.3