aboutsummaryrefslogtreecommitdiff
path: root/crates/completion/src
diff options
context:
space:
mode:
authorKirill Bulatov <[email protected]>2020-12-04 14:03:22 +0000
committerKirill Bulatov <[email protected]>2020-12-07 21:41:08 +0000
commitdeda74edd89affb3f77d274776d2a672bc11db90 (patch)
treee9c7dac6df4fd06012ca6b6e628223e925998f1e /crates/completion/src
parent93bc009a5968c964693299263689b50b2efe9abc (diff)
Use stateless completion resolve
Diffstat (limited to 'crates/completion/src')
-rw-r--r--crates/completion/src/completions/unqualified_path.rs10
-rw-r--r--crates/completion/src/item.rs1
-rw-r--r--crates/completion/src/lib.rs32
-rw-r--r--crates/completion/src/render.rs12
4 files changed, 40 insertions, 15 deletions
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;
9 9
10use crate::{ 10use crate::{
11 render::{render_resolution_with_import, RenderContext}, 11 render::{render_resolution_with_import, RenderContext},
12 CompletionContext, Completions, 12 CompletionContext, Completions, ImportEdit,
13}; 13};
14 14
15pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) { 15pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) {
@@ -103,9 +103,11 @@ fn fuzzy_completion(acc: &mut Completions, ctx: &CompletionContext) -> Option<()
103 .filter_map(|(import_path, definition)| { 103 .filter_map(|(import_path, definition)| {
104 render_resolution_with_import( 104 render_resolution_with_import(
105 RenderContext::new(ctx), 105 RenderContext::new(ctx),
106 import_path.clone(), 106 ImportEdit {
107 import_scope.clone(), 107 import_path: import_path.clone(),
108 ctx.config.merge, 108 import_scope: import_scope.clone(),
109 merge_behaviour: ctx.config.merge,
110 },
109 &definition, 111 &definition,
110 ) 112 )
111 }); 113 });
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 {
276} 276}
277 277
278impl ImportEdit { 278impl ImportEdit {
279 // TODO kb remove this at all now, since it's used only once?
280 /// Attempts to insert the import to the given scope, producing a text edit. 279 /// Attempts to insert the import to the given scope, producing a text edit.
281 /// May return no edit in edge cases, such as scope already containing the import. 280 /// May return no edit in edge cases, such as scope already containing the import.
282 pub fn to_text_edit(&self) -> Option<TextEdit> { 281 pub fn to_text_edit(&self) -> Option<TextEdit> {
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;
11 11
12mod completions; 12mod completions;
13 13
14use ide_db::base_db::FilePosition; 14use ide_db::{
15use ide_db::RootDatabase; 15 base_db::FilePosition, helpers::insert_use::ImportScope, imports_locator, RootDatabase,
16};
17use syntax::AstNode;
18use text_edit::TextEdit;
16 19
17use crate::{completions::Completions, context::CompletionContext, item::CompletionKind}; 20use crate::{completions::Completions, context::CompletionContext, item::CompletionKind};
18 21
@@ -131,6 +134,31 @@ pub fn completions(
131 Some(acc) 134 Some(acc)
132} 135}
133 136
137/// Resolves additional completion data at the position given.
138pub fn resolve_completion_edits(
139 db: &RootDatabase,
140 config: &CompletionConfig,
141 position: FilePosition,
142 full_import_path: &str,
143 imported_name: &str,
144) -> Option<TextEdit> {
145 let ctx = CompletionContext::new(db, position, config)?;
146 let anchor = ctx.name_ref_syntax.as_ref()?;
147 let import_scope = ImportScope::find_insert_use_container(anchor.syntax(), &ctx.sema)?;
148
149 let current_module = ctx.sema.scope(anchor.syntax()).module()?;
150 let current_crate = current_module.krate();
151
152 let import_path = imports_locator::find_exact_imports(&ctx.sema, current_crate, imported_name)
153 .filter_map(|candidate| {
154 let item: hir::ItemInNs = candidate.either(Into::into, Into::into);
155 current_module.find_use_path(db, item)
156 })
157 .find(|mod_path| mod_path.to_string() == full_import_path)?;
158
159 ImportEdit { import_path, import_scope, merge_behaviour: config.merge }.to_text_edit()
160}
161
134#[cfg(test)] 162#[cfg(test)]
135mod tests { 163mod tests {
136 use crate::config::CompletionConfig; 164 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;
9 9
10mod builder_ext; 10mod builder_ext;
11 11
12use hir::{Documentation, HasAttrs, HirDisplay, ModPath, Mutability, ScopeDef, Type}; 12use hir::{Documentation, HasAttrs, HirDisplay, Mutability, ScopeDef, Type};
13use ide_db::helpers::insert_use::{ImportScope, MergeBehaviour};
14use ide_db::RootDatabase; 13use ide_db::RootDatabase;
15use syntax::TextRange; 14use syntax::TextRange;
16use test_utils::mark; 15use test_utils::mark;
@@ -48,15 +47,12 @@ pub(crate) fn render_resolution<'a>(
48 47
49pub(crate) fn render_resolution_with_import<'a>( 48pub(crate) fn render_resolution_with_import<'a>(
50 ctx: RenderContext<'a>, 49 ctx: RenderContext<'a>,
51 import_path: ModPath, 50 import_edit: ImportEdit,
52 import_scope: ImportScope,
53 merge_behaviour: Option<MergeBehaviour>,
54 resolution: &ScopeDef, 51 resolution: &ScopeDef,
55) -> Option<CompletionItem> { 52) -> Option<CompletionItem> {
56 let local_name = import_path.segments.last()?.to_string();
57 Render::new(ctx).render_resolution( 53 Render::new(ctx).render_resolution(
58 local_name, 54 import_edit.import_path.segments.last()?.to_string(),
59 Some(ImportEdit { import_path, import_scope, merge_behaviour }), 55 Some(import_edit),
60 resolution, 56 resolution,
61 ) 57 )
62} 58}