mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 13:25:09 +00:00
Fix multiple imports acting on files on separately
This commit is contained in:
parent
d0c8777a44
commit
1f7e14e78b
1 changed files with 27 additions and 31 deletions
|
@ -15,11 +15,13 @@ use completions::flyimport::position_for_import;
|
||||||
use ide_db::{
|
use ide_db::{
|
||||||
base_db::FilePosition,
|
base_db::FilePosition,
|
||||||
helpers::{
|
helpers::{
|
||||||
import_assets::{LocatedImport, NameToImport},
|
import_assets::NameToImport,
|
||||||
insert_use::ImportScope,
|
insert_use::{self, ImportScope},
|
||||||
|
mod_path_to_ast,
|
||||||
},
|
},
|
||||||
items_locator, RootDatabase,
|
items_locator, RootDatabase,
|
||||||
};
|
};
|
||||||
|
use syntax::algo;
|
||||||
use text_edit::TextEdit;
|
use text_edit::TextEdit;
|
||||||
|
|
||||||
use crate::{completions::Completions, context::CompletionContext, item::CompletionKind};
|
use crate::{completions::Completions, context::CompletionContext, item::CompletionKind};
|
||||||
|
@ -177,41 +179,35 @@ pub fn resolve_completion_edits(
|
||||||
position: FilePosition,
|
position: FilePosition,
|
||||||
imports: impl IntoIterator<Item = (String, String)>,
|
imports: impl IntoIterator<Item = (String, String)>,
|
||||||
) -> Option<Vec<TextEdit>> {
|
) -> Option<Vec<TextEdit>> {
|
||||||
|
let _p = profile::span("resolve_completion_edits");
|
||||||
let ctx = CompletionContext::new(db, position, config)?;
|
let ctx = CompletionContext::new(db, position, config)?;
|
||||||
let position_for_import = position_for_import(&ctx, None)?;
|
let position_for_import = position_for_import(&ctx, None)?;
|
||||||
let scope = ImportScope::find_insert_use_container_with_macros(position_for_import, &ctx.sema)?;
|
let scope = ImportScope::find_insert_use_container_with_macros(position_for_import, &ctx.sema)?;
|
||||||
|
|
||||||
let current_module = ctx.sema.scope(position_for_import).module()?;
|
let current_module = ctx.sema.scope(position_for_import).module()?;
|
||||||
let current_crate = current_module.krate();
|
let current_crate = current_module.krate();
|
||||||
|
let new_ast = scope.clone_for_update();
|
||||||
|
let mut import_insert = TextEdit::builder();
|
||||||
|
|
||||||
Some(
|
// FIXME: lift out and make some tests here, this is ImportEdit::to_text_edit but changed to work with multiple edits
|
||||||
imports
|
imports.into_iter().for_each(|(full_import_path, imported_name)| {
|
||||||
.into_iter()
|
let items_with_name = items_locator::items_with_name(
|
||||||
.filter_map(|(full_import_path, imported_name)| {
|
&ctx.sema,
|
||||||
let (import_path, item_to_import) = items_locator::items_with_name(
|
current_crate,
|
||||||
&ctx.sema,
|
NameToImport::Exact(imported_name),
|
||||||
current_crate,
|
items_locator::AssocItemSearch::Include,
|
||||||
NameToImport::Exact(imported_name),
|
Some(items_locator::DEFAULT_QUERY_SEARCH_LIMIT.inner()),
|
||||||
items_locator::AssocItemSearch::Include,
|
);
|
||||||
Some(items_locator::DEFAULT_QUERY_SEARCH_LIMIT.inner()),
|
let import = items_with_name
|
||||||
)
|
.filter_map(|candidate| {
|
||||||
.filter_map(|candidate| {
|
current_module.find_use_path_prefixed(db, candidate, config.insert_use.prefix_kind)
|
||||||
current_module
|
|
||||||
.find_use_path_prefixed(db, candidate, config.insert_use.prefix_kind)
|
|
||||||
.zip(Some(candidate))
|
|
||||||
})
|
|
||||||
.find(|(mod_path, _)| mod_path.to_string() == full_import_path)?;
|
|
||||||
let import = LocatedImport::new(
|
|
||||||
import_path.clone(),
|
|
||||||
item_to_import,
|
|
||||||
item_to_import,
|
|
||||||
Some(import_path),
|
|
||||||
);
|
|
||||||
|
|
||||||
ImportEdit { import, scope: scope.clone() }
|
|
||||||
.to_text_edit(config.insert_use)
|
|
||||||
.map(|edit| edit)
|
|
||||||
})
|
})
|
||||||
.collect(),
|
.find(|mod_path| mod_path.to_string() == full_import_path);
|
||||||
)
|
if let Some(import_path) = import {
|
||||||
|
insert_use::insert_use(&new_ast, mod_path_to_ast(&import_path), &config.insert_use);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
algo::diff(scope.as_syntax_node(), new_ast.as_syntax_node()).into_text_edit(&mut import_insert);
|
||||||
|
Some(vec![import_insert.finish()])
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue