From deda74edd89affb3f77d274776d2a672bc11db90 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Fri, 4 Dec 2020 16:03:22 +0200 Subject: Use stateless completion resolve --- .../completion/src/completions/unqualified_path.rs | 10 ++++--- crates/completion/src/item.rs | 1 - crates/completion/src/lib.rs | 32 ++++++++++++++++++++-- crates/completion/src/render.rs | 12 +++----- 4 files changed, 40 insertions(+), 15 deletions(-) (limited to 'crates/completion') diff --git a/crates/completion/src/completions/unqualified_path.rs b/crates/completion/src/completions/unqualified_path.rs index 81691cd7f..26a2b7a1b 100644 --- a/crates/completion/src/completions/unqualified_path.rs +++ b/crates/completion/src/completions/unqualified_path.rs @@ -9,7 +9,7 @@ use test_utils::mark; use crate::{ render::{render_resolution_with_import, RenderContext}, - CompletionContext, Completions, + CompletionContext, Completions, ImportEdit, }; pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) { @@ -103,9 +103,11 @@ fn fuzzy_completion(acc: &mut Completions, ctx: &CompletionContext) -> Option<() .filter_map(|(import_path, definition)| { render_resolution_with_import( RenderContext::new(ctx), - import_path.clone(), - import_scope.clone(), - ctx.config.merge, + ImportEdit { + import_path: import_path.clone(), + import_scope: import_scope.clone(), + merge_behaviour: ctx.config.merge, + }, &definition, ) }); diff --git a/crates/completion/src/item.rs b/crates/completion/src/item.rs index 2dadf7e5b..4e56f28f3 100644 --- a/crates/completion/src/item.rs +++ b/crates/completion/src/item.rs @@ -276,7 +276,6 @@ pub struct ImportEdit { } impl ImportEdit { - // TODO kb remove this at all now, since it's used only once? /// Attempts to insert the import to the given scope, producing a text edit. /// May return no edit in edge cases, such as scope already containing the import. pub fn to_text_edit(&self) -> Option { diff --git a/crates/completion/src/lib.rs b/crates/completion/src/lib.rs index c57203c80..938c92dbb 100644 --- a/crates/completion/src/lib.rs +++ b/crates/completion/src/lib.rs @@ -11,8 +11,11 @@ mod render; mod completions; -use ide_db::base_db::FilePosition; -use ide_db::RootDatabase; +use ide_db::{ + base_db::FilePosition, helpers::insert_use::ImportScope, imports_locator, RootDatabase, +}; +use syntax::AstNode; +use text_edit::TextEdit; use crate::{completions::Completions, context::CompletionContext, item::CompletionKind}; @@ -131,6 +134,31 @@ pub fn completions( Some(acc) } +/// Resolves additional completion data at the position given. +pub fn resolve_completion_edits( + db: &RootDatabase, + config: &CompletionConfig, + position: FilePosition, + full_import_path: &str, + imported_name: &str, +) -> Option { + let ctx = CompletionContext::new(db, position, config)?; + let anchor = ctx.name_ref_syntax.as_ref()?; + let import_scope = ImportScope::find_insert_use_container(anchor.syntax(), &ctx.sema)?; + + let current_module = ctx.sema.scope(anchor.syntax()).module()?; + let current_crate = current_module.krate(); + + let import_path = imports_locator::find_exact_imports(&ctx.sema, current_crate, imported_name) + .filter_map(|candidate| { + let item: hir::ItemInNs = candidate.either(Into::into, Into::into); + current_module.find_use_path(db, item) + }) + .find(|mod_path| mod_path.to_string() == full_import_path)?; + + ImportEdit { import_path, import_scope, merge_behaviour: config.merge }.to_text_edit() +} + #[cfg(test)] mod tests { use crate::config::CompletionConfig; diff --git a/crates/completion/src/render.rs b/crates/completion/src/render.rs index a6faedb18..9a43480e1 100644 --- a/crates/completion/src/render.rs +++ b/crates/completion/src/render.rs @@ -9,8 +9,7 @@ pub(crate) mod type_alias; mod builder_ext; -use hir::{Documentation, HasAttrs, HirDisplay, ModPath, Mutability, ScopeDef, Type}; -use ide_db::helpers::insert_use::{ImportScope, MergeBehaviour}; +use hir::{Documentation, HasAttrs, HirDisplay, Mutability, ScopeDef, Type}; use ide_db::RootDatabase; use syntax::TextRange; use test_utils::mark; @@ -48,15 +47,12 @@ pub(crate) fn render_resolution<'a>( pub(crate) fn render_resolution_with_import<'a>( ctx: RenderContext<'a>, - import_path: ModPath, - import_scope: ImportScope, - merge_behaviour: Option, + import_edit: ImportEdit, resolution: &ScopeDef, ) -> Option { - let local_name = import_path.segments.last()?.to_string(); Render::new(ctx).render_resolution( - local_name, - Some(ImportEdit { import_path, import_scope, merge_behaviour }), + import_edit.import_path.segments.last()?.to_string(), + Some(import_edit), resolution, ) } -- cgit v1.2.3