Use stateless completion resolve

This commit is contained in:
Kirill Bulatov 2020-12-04 16:03:22 +02:00
parent 93bc009a59
commit deda74edd8
6 changed files with 123 additions and 70 deletions

View file

@ -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,
)
});

View file

@ -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<TextEdit> {

View file

@ -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<TextEdit> {
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;

View file

@ -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<MergeBehaviour>,
import_edit: ImportEdit,
resolution: &ScopeDef,
) -> Option<CompletionItem> {
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,
)
}