From 127814d9a7f62c834c0893ff05e933aac4be89e9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 8 Sep 2018 01:35:20 +0300 Subject: nested mod completion --- crates/libeditor/src/completion.rs | 58 +++++++++++++++++++++++---------- crates/libeditor/src/scope/mod_scope.rs | 11 ++++--- 2 files changed, 47 insertions(+), 22 deletions(-) (limited to 'crates/libeditor/src') diff --git a/crates/libeditor/src/completion.rs b/crates/libeditor/src/completion.rs index c25b4c217..52df6fd10 100644 --- a/crates/libeditor/src/completion.rs +++ b/crates/libeditor/src/completion.rs @@ -2,7 +2,7 @@ use std::collections::{HashSet, HashMap}; use libsyntax2::{ File, TextUnit, AstNode, SyntaxNodeRef, SyntaxKind::*, - ast::{self, LoopBodyOwner}, + ast::{self, LoopBodyOwner, ModuleItemOwner}, algo::{ ancestors, visit::{visitor, Visitor, visitor_ctx, VisitorCtx}, @@ -58,22 +58,34 @@ fn complete_name_ref(file: &File, name_ref: ast::NameRef, acc: &mut Vec(name_ref.syntax()) { return; } - if let Some(fn_def) = ancestors(name_ref.syntax()).filter_map(ast::FnDef::cast).next() { - complete_expr_keywords(&file, fn_def, name_ref, acc); - let scopes = FnScopes::new(fn_def); - complete_fn(name_ref, &scopes, acc); - } - if let Some(root) = ancestors(name_ref.syntax()).filter_map(ast::Root::cast).next() { - let scope = ModuleScope::new(root); - acc.extend( - scope.entries().iter() - .filter(|entry| entry.syntax() != name_ref.syntax()) - .map(|entry| CompletionItem { - label: entry.name().to_string(), - lookup: None, - snippet: None, - }) - ); + let mut visited_fn = false; + for node in ancestors(name_ref.syntax()) { + if let Some(items) = visitor() + .visit::(|it| Some(it.items())) + .visit::(|it| Some(it.item_list()?.items())) + .accept(node) { + if let Some(items) = items { + let scope = ModuleScope::new(items); + acc.extend( + scope.entries().iter() + .filter(|entry| entry.syntax() != name_ref.syntax()) + .map(|entry| CompletionItem { + label: entry.name().to_string(), + lookup: None, + snippet: None, + }) + ); + } + break; + + } else if !visited_fn { + if let Some(fn_def) = ast::FnDef::cast(node) { + visited_fn = true; + complete_expr_keywords(&file, fn_def, name_ref, acc); + let scopes = FnScopes::new(fn_def); + complete_fn(name_ref, &scopes, acc); + } + } } } @@ -299,6 +311,18 @@ mod tests { ", r#"[]"#); } + #[test] + fn test_completion_mod_scope_nested() { + check_scope_completion(r" + struct Foo; + mod m { + struct Bar; + fn quux() { <|> } + } + ", r#"[CompletionItem { label: "Bar", lookup: None, snippet: None }, + CompletionItem { label: "quux", lookup: None, snippet: None }]"#); + } + #[test] fn test_complete_type() { check_scope_completion(r" diff --git a/crates/libeditor/src/scope/mod_scope.rs b/crates/libeditor/src/scope/mod_scope.rs index 67baa8678..0ec56a206 100644 --- a/crates/libeditor/src/scope/mod_scope.rs +++ b/crates/libeditor/src/scope/mod_scope.rs @@ -1,5 +1,6 @@ use libsyntax2::{ - AstNode, SyntaxNode, SyntaxNodeRef, SmolStr, ast + AstNode, SyntaxNode, SyntaxNodeRef, SmolStr, + ast::{self, AstChildren}, }; pub struct ModuleScope { @@ -16,9 +17,9 @@ enum EntryKind { } impl ModuleScope { - pub fn new(m: ast::Root) -> ModuleScope { + pub fn new(items: AstChildren) -> ModuleScope { let mut entries = Vec::new(); - for item in m.items() { + for item in items { let entry = match item { ast::ModuleItem::StructDef(item) => Entry::new(item), ast::ModuleItem::EnumDef(item) => Entry::new(item), @@ -85,11 +86,11 @@ fn collect_imports(tree: ast::UseTree, acc: &mut Vec) { #[cfg(test)] mod tests { use super::*; - use libsyntax2::File; + use libsyntax2::{File, ast::ModuleItemOwner}; fn do_check(code: &str, expected: &[&str]) { let file = File::parse(&code); - let scope = ModuleScope::new(file.ast()); + let scope = ModuleScope::new(file.ast().items()); let actual = scope.entries .iter() .map(|it| it.name()) -- cgit v1.2.3