Skip to content

Commit b7a3d60

Browse files
authored
Merge pull request #19029 from Veykril/push-wklzwzqvkrou
internal: Skip redundant path search in `resolve_completion_edits`
2 parents 5df0b59 + ae74cc3 commit b7a3d60

File tree

9 files changed

+27
-50
lines changed

9 files changed

+27
-50
lines changed

Diff for: crates/ide-completion/src/item.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ pub struct CompletionItem {
8282
pub ref_match: Option<(CompletionItemRefMode, TextSize)>,
8383

8484
/// The import data to add to completion's edits.
85-
/// (ImportPath, LastSegment)
86-
pub import_to_add: SmallVec<[(String, String); 1]>,
85+
pub import_to_add: SmallVec<[String; 1]>,
8786
}
8887

8988
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
@@ -570,12 +569,7 @@ impl Builder {
570569
let import_to_add = self
571570
.imports_to_add
572571
.into_iter()
573-
.filter_map(|import| {
574-
Some((
575-
import.import_path.display(db, self.edition).to_string(),
576-
import.import_path.segments().last()?.display(db, self.edition).to_string(),
577-
))
578-
})
572+
.map(|import| import.import_path.display(db, self.edition).to_string())
579573
.collect();
580574

581575
CompletionItem {

Diff for: crates/ide-completion/src/lib.rs

+9-28
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,13 @@ mod snippet;
1010
#[cfg(test)]
1111
mod tests;
1212

13-
use ide_db::text_edit::TextEdit;
1413
use ide_db::{
15-
helpers::mod_path_to_ast,
16-
imports::{
17-
import_assets::NameToImport,
18-
insert_use::{self, ImportScope},
19-
},
20-
items_locator,
14+
imports::insert_use::{self, ImportScope},
2115
syntax_helpers::tree_diff::diff,
16+
text_edit::TextEdit,
2217
FilePosition, FxHashSet, RootDatabase,
2318
};
19+
use syntax::ast::make;
2420

2521
use crate::{
2622
completions::Completions,
@@ -272,7 +268,7 @@ pub fn resolve_completion_edits(
272268
db: &RootDatabase,
273269
config: &CompletionConfig<'_>,
274270
FilePosition { file_id, offset }: FilePosition,
275-
imports: impl IntoIterator<Item = (String, String)>,
271+
imports: impl IntoIterator<Item = String>,
276272
) -> Option<Vec<TextEdit>> {
277273
let _p = tracing::info_span!("resolve_completion_edits").entered();
278274
let sema = hir::Semantics::new(db);
@@ -289,27 +285,12 @@ pub fn resolve_completion_edits(
289285
let new_ast = scope.clone_for_update();
290286
let mut import_insert = TextEdit::builder();
291287

292-
let cfg = config.import_path_config(true);
293-
294-
imports.into_iter().for_each(|(full_import_path, imported_name)| {
295-
let items_with_name = items_locator::items_with_name(
296-
&sema,
297-
current_crate,
298-
NameToImport::exact_case_sensitive(imported_name),
299-
items_locator::AssocSearchMode::Include,
288+
imports.into_iter().for_each(|full_import_path| {
289+
insert_use::insert_use(
290+
&new_ast,
291+
make::path_from_text_with_edition(&full_import_path, current_edition),
292+
&config.insert_use,
300293
);
301-
let import = items_with_name
302-
.filter_map(|candidate| {
303-
current_module.find_use_path(db, candidate, config.insert_use.prefix_kind, cfg)
304-
})
305-
.find(|mod_path| mod_path.display(db, current_edition).to_string() == full_import_path);
306-
if let Some(import_path) = import {
307-
insert_use::insert_use(
308-
&new_ast,
309-
mod_path_to_ast(&import_path, current_edition),
310-
&config.insert_use,
311-
);
312-
}
313294
});
314295

315296
diff(scope.as_syntax_node(), new_ast.as_syntax_node()).into_text_edit(&mut import_insert);

Diff for: crates/ide/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ impl Analysis {
672672
&self,
673673
config: &CompletionConfig<'_>,
674674
position: FilePosition,
675-
imports: impl IntoIterator<Item = (String, String)> + std::panic::UnwindSafe,
675+
imports: impl IntoIterator<Item = String> + std::panic::UnwindSafe,
676676
) -> Cancellable<Vec<TextEdit>> {
677677
Ok(self
678678
.with_db(|db| ide_completion::resolve_completion_edits(db, config, position, imports))?

Diff for: crates/rust-analyzer/src/handlers/request.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1154,10 +1154,7 @@ pub(crate) fn handle_completion_resolve(
11541154
.resolve_completion_edits(
11551155
&forced_resolve_completions_config,
11561156
position,
1157-
resolve_data
1158-
.imports
1159-
.into_iter()
1160-
.map(|import| (import.full_import_path, import.imported_name)),
1157+
resolve_data.imports.into_iter().map(|import| import.full_import_path),
11611158
)?
11621159
.into_iter()
11631160
.flat_map(|edit| edit.into_iter().map(|indel| to_proto::text_edit(&line_index, indel)))

Diff for: crates/rust-analyzer/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,8 @@ fn completion_item_hash(item: &CompletionItem, is_ref_completion: bool) -> [u8;
142142
hasher.update(prefix);
143143
hasher.update(u32::from(*text_size).to_le_bytes());
144144
}
145-
for (import_path, import_name) in &item.import_to_add {
145+
for import_path in &item.import_to_add {
146146
hasher.update(import_path);
147-
hasher.update(import_name);
148147
}
149148
hasher.finalize()
150149
}

Diff for: crates/rust-analyzer/src/lsp/ext.rs

-1
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,6 @@ pub struct InlayHintResolveData {
850850
#[derive(Debug, Serialize, Deserialize)]
851851
pub struct CompletionImport {
852852
pub full_import_path: String,
853-
pub imported_name: String,
854853
}
855854

856855
#[derive(Debug, Deserialize, Default)]

Diff for: crates/rust-analyzer/src/lsp/to_proto.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,7 @@ fn completion_item(
394394
item.import_to_add
395395
.clone()
396396
.into_iter()
397-
.map(|(import_path, import_name)| lsp_ext::CompletionImport {
398-
full_import_path: import_path,
399-
imported_name: import_name,
400-
})
397+
.map(|import_path| lsp_ext::CompletionImport { full_import_path: import_path })
401398
.collect()
402399
} else {
403400
Vec::new()

Diff for: crates/syntax/src/ast/make.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,11 @@ pub fn path_from_text(text: &str) -> ast::Path {
411411
ast_from_text(&format!("fn main() {{ let test: {text}; }}"))
412412
}
413413

414+
// FIXME: should not be pub
415+
pub fn path_from_text_with_edition(text: &str, edition: Edition) -> ast::Path {
416+
ast_from_text_with_edition(&format!("fn main() {{ let test: {text}; }}"), edition)
417+
}
418+
414419
pub fn use_tree_glob() -> ast::UseTree {
415420
ast_from_text("use *;")
416421
}
@@ -1230,7 +1235,12 @@ pub fn token_tree(
12301235

12311236
#[track_caller]
12321237
fn ast_from_text<N: AstNode>(text: &str) -> N {
1233-
let parse = SourceFile::parse(text, Edition::CURRENT);
1238+
ast_from_text_with_edition(text, Edition::CURRENT)
1239+
}
1240+
1241+
#[track_caller]
1242+
fn ast_from_text_with_edition<N: AstNode>(text: &str, edition: Edition) -> N {
1243+
let parse = SourceFile::parse(text, edition);
12341244
let node = match parse.tree().syntax().descendants().find_map(N::cast) {
12351245
Some(it) => it,
12361246
None => {

Diff for: docs/dev/lsp-extensions.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!---
2-
lsp/ext.rs hash: 2d8604825c458288
2+
lsp/ext.rs hash: af70cce5d6905e39
33
44
If you need to change the above hash to make the test pass, please check if you
55
need to adjust this doc as well and ping this issue:

0 commit comments

Comments
 (0)