From bc1fc6239d67708112f4f3997b104934dd11b7fd Mon Sep 17 00:00:00 2001 From: Kevin DeLorey <2295721+kdelorey@users.noreply.github.com> Date: Wed, 22 Jan 2020 22:38:03 -0600 Subject: Renamed the trait completion mod. --- crates/ra_ide/src/completion.rs | 4 +- crates/ra_ide/src/completion/complete_impl_fn.rs | 80 ---------------------- .../ra_ide/src/completion/complete_trait_impl.rs | 80 ++++++++++++++++++++++ 3 files changed, 82 insertions(+), 82 deletions(-) delete mode 100644 crates/ra_ide/src/completion/complete_impl_fn.rs create mode 100644 crates/ra_ide/src/completion/complete_trait_impl.rs diff --git a/crates/ra_ide/src/completion.rs b/crates/ra_ide/src/completion.rs index f31f2593c..ad7f6ef26 100644 --- a/crates/ra_ide/src/completion.rs +++ b/crates/ra_ide/src/completion.rs @@ -15,7 +15,7 @@ mod complete_path; mod complete_scope; mod complete_postfix; mod complete_macro_in_item_position; -mod complete_impl_fn; +mod complete_trait_impl; use ra_db::SourceDatabase; @@ -74,7 +74,7 @@ pub(crate) fn completions(db: &db::RootDatabase, position: FilePosition) -> Opti complete_pattern::complete_pattern(&mut acc, &ctx); complete_postfix::complete_postfix(&mut acc, &ctx); complete_macro_in_item_position::complete_macro_in_item_position(&mut acc, &ctx); - complete_impl_fn::complete_impl_fn(&mut acc, &ctx); + complete_trait_impl::complete_trait_impl(&mut acc, &ctx); Some(acc) } diff --git a/crates/ra_ide/src/completion/complete_impl_fn.rs b/crates/ra_ide/src/completion/complete_impl_fn.rs deleted file mode 100644 index 6d464cc1f..000000000 --- a/crates/ra_ide/src/completion/complete_impl_fn.rs +++ /dev/null @@ -1,80 +0,0 @@ - -use crate::completion::{CompletionContext, Completions}; - -use hir::{ self, db::HirDatabase, HasSource }; - -use ra_syntax::{ ast, ast::AstNode }; - -pub(crate) fn complete_impl_fn(acc: &mut Completions, ctx: &CompletionContext) { - let impl_trait = ast::ItemList::cast(ctx.token.parent()) - .and_then(|item_list| item_list.syntax().parent()) - .and_then(|item_list_parent| ast::ImplBlock::cast(item_list_parent)) - .and_then(|impl_block| resolve_target_trait(ctx.db, &ctx.analyzer, &impl_block)); - - if let Some(x) = &impl_trait { - for trait_item in x.0.items(ctx.db) { - match trait_item { - hir::AssocItem::Function(f) => acc.add_function_impl(ctx, f), - _ => {} - } - } - } -} - -fn resolve_target_trait( - db: &impl HirDatabase, - analyzer: &hir::SourceAnalyzer, - impl_block: &ast::ImplBlock -) -> Option<(hir::Trait, ast::TraitDef)> { - let ast_path = impl_block - .target_trait() - .map(|it| it.syntax().clone()) - .and_then(ast::PathType::cast)? - .path()?; - - match analyzer.resolve_path(db, &ast_path) { - Some(hir::PathResolution::Def(hir::ModuleDef::Trait(def))) => { - Some((def, def.source(db).value)) - } - _ => None, - } -} - -#[cfg(test)] -mod tests { - use crate::completion::{do_completion, CompletionItem, CompletionKind}; - use insta::assert_debug_snapshot; - - fn complete(code: &str) -> Vec { - do_completion(code, CompletionKind::Reference) - } - - #[test] - fn single_function() { - let completions = complete( - r" - trait Test { - fn foo(); - } - - struct T1; - - impl Test for T1 { - <|> - } - ", - ); - assert_debug_snapshot!(completions, @r###" - [ - CompletionItem { - label: "fn foo()", - source_range: [138; 138), - delete: [138; 138), - insert: "fn foo() { $0 }", - kind: Function, - lookup: "foo", - }, - ] - "###); - } -} \ No newline at end of file diff --git a/crates/ra_ide/src/completion/complete_trait_impl.rs b/crates/ra_ide/src/completion/complete_trait_impl.rs new file mode 100644 index 000000000..3bec57426 --- /dev/null +++ b/crates/ra_ide/src/completion/complete_trait_impl.rs @@ -0,0 +1,80 @@ + +use crate::completion::{CompletionContext, Completions}; + +use hir::{ self, db::HirDatabase, HasSource }; + +use ra_syntax::{ ast, ast::AstNode }; + +pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext) { + let impl_trait = ast::ItemList::cast(ctx.token.parent()) + .and_then(|item_list| item_list.syntax().parent()) + .and_then(|item_list_parent| ast::ImplBlock::cast(item_list_parent)) + .and_then(|impl_block| resolve_target_trait(ctx.db, &ctx.analyzer, &impl_block)); + + if let Some(x) = &impl_trait { + for trait_item in x.0.items(ctx.db) { + match trait_item { + hir::AssocItem::Function(f) => acc.add_function_impl(ctx, f), + _ => {} + } + } + } +} + +fn resolve_target_trait( + db: &impl HirDatabase, + analyzer: &hir::SourceAnalyzer, + impl_block: &ast::ImplBlock +) -> Option<(hir::Trait, ast::TraitDef)> { + let ast_path = impl_block + .target_trait() + .map(|it| it.syntax().clone()) + .and_then(ast::PathType::cast)? + .path()?; + + match analyzer.resolve_path(db, &ast_path) { + Some(hir::PathResolution::Def(hir::ModuleDef::Trait(def))) => { + Some((def, def.source(db).value)) + } + _ => None, + } +} + +#[cfg(test)] +mod tests { + use crate::completion::{do_completion, CompletionItem, CompletionKind}; + use insta::assert_debug_snapshot; + + fn complete(code: &str) -> Vec { + do_completion(code, CompletionKind::Reference) + } + + #[test] + fn single_function() { + let completions = complete( + r" + trait Test { + fn foo(); + } + + struct T1; + + impl Test for T1 { + <|> + } + ", + ); + assert_debug_snapshot!(completions, @r###" + [ + CompletionItem { + label: "fn foo()", + source_range: [138; 138), + delete: [138; 138), + insert: "fn foo() { $0 }", + kind: Function, + lookup: "foo", + }, + ] + "###); + } +} \ No newline at end of file -- cgit v1.2.3