Skip to content

Commit 143a30a

Browse files
committed
Show case-insensitive exact matches instead of fuzzy flyimport for short paths
1 parent c469f8a commit 143a30a

File tree

6 files changed

+55
-33
lines changed

6 files changed

+55
-33
lines changed

crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub(crate) fn replace_derive_with_manual_impl(
6565
let found_traits = items_locator::items_with_name(
6666
&ctx.sema,
6767
current_crate,
68-
NameToImport::Exact(trait_path.segments().last()?.to_string()),
68+
NameToImport::exact_case_sensitive(trait_path.segments().last()?.to_string()),
6969
items_locator::AssocItemSearch::Exclude,
7070
Some(items_locator::DEFAULT_QUERY_SEARCH_LIMIT.inner()),
7171
)

crates/ide_completion/src/completions/flyimport.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -227,22 +227,18 @@ fn import_assets(ctx: &CompletionContext, fuzzy_name: String) -> Option<ImportAs
227227
)
228228
} else {
229229
let fuzzy_name_length = fuzzy_name.len();
230-
let assets_for_path = ImportAssets::for_fuzzy_path(
230+
let mut assets_for_path = ImportAssets::for_fuzzy_path(
231231
current_module,
232232
ctx.path_qual().cloned(),
233233
fuzzy_name,
234234
&ctx.sema,
235235
ctx.token.parent()?,
236236
)?;
237-
238-
if matches!(assets_for_path.import_candidate(), ImportCandidate::Path(_))
239-
&& fuzzy_name_length < 2
240-
{
241-
cov_mark::hit!(ignore_short_input_for_path);
242-
None
243-
} else {
244-
Some(assets_for_path)
237+
if fuzzy_name_length < 3 {
238+
cov_mark::hit!(flyimport_exact_on_short_path);
239+
assets_for_path.path_fuzzy_name_to_exact(false);
245240
}
241+
Some(assets_for_path)
246242
}
247243
}
248244

crates/ide_completion/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ pub fn resolve_completion_edits(
195195
let items_with_name = items_locator::items_with_name(
196196
&ctx.sema,
197197
current_crate,
198-
NameToImport::Exact(imported_name),
198+
NameToImport::exact_case_sensitive(imported_name),
199199
items_locator::AssocItemSearch::Include,
200200
Some(items_locator::DEFAULT_QUERY_SEARCH_LIMIT.inner()),
201201
);

crates/ide_completion/src/tests/flyimport.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,25 +96,29 @@ fn main() {
9696

9797
#[test]
9898
fn short_paths_are_ignored() {
99-
cov_mark::check!(ignore_short_input_for_path);
99+
cov_mark::check!(flyimport_exact_on_short_path);
100100

101101
check(
102102
r#"
103103
//- /lib.rs crate:dep
104-
pub struct FirstStruct;
104+
pub struct Bar;
105+
pub struct Rcar;
106+
pub struct Rc;
105107
pub mod some_module {
106-
pub struct SecondStruct;
107-
pub struct ThirdStruct;
108+
pub struct Bar;
109+
pub struct Rcar;
110+
pub struct Rc;
108111
}
109112
110113
//- /main.rs crate:main deps:dep
111-
use dep::{FirstStruct, some_module::SecondStruct};
112-
113114
fn main() {
114-
t$0
115+
rc$0
115116
}
116117
"#,
117-
expect![[r#""#]],
118+
expect![[r#"
119+
st Rc (use dep::Rc)
120+
st Rc (use dep::some_module::Rc)
121+
"#]],
118122
);
119123
}
120124

@@ -772,7 +776,7 @@ mod foo {
772776
}
773777
774778
fn main() {
775-
TE$0
779+
TES$0
776780
}"#,
777781
expect![[r#"
778782
ct TEST_CONST (use foo::TEST_CONST)
@@ -789,7 +793,7 @@ mod foo {
789793
}
790794
791795
fn main() {
792-
te$0
796+
tes$0
793797
}"#,
794798
expect![[r#"
795799
ct TEST_CONST (use foo::TEST_CONST)
@@ -846,7 +850,7 @@ struct Foo {
846850
some_field: i32,
847851
}
848852
fn main() {
849-
let _ = Foo { some_field: so$0 };
853+
let _ = Foo { some_field: som$0 };
850854
}
851855
"#,
852856
expect![[r#"

crates/ide_db/src/helpers/import_assets.rs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,23 @@ pub struct FirstSegmentUnresolved {
6868
/// A name that will be used during item lookups.
6969
#[derive(Debug, Clone)]
7070
pub enum NameToImport {
71-
/// Requires items with names that exactly match the given string, case-sensitive.
72-
Exact(String),
71+
/// Requires items with names that exactly match the given string, bool indicatse case-sensitivity.
72+
Exact(String, bool),
7373
/// Requires items with names that case-insensitively contain all letters from the string,
7474
/// in the same order, but not necessary adjacent.
7575
Fuzzy(String),
7676
}
7777

78+
impl NameToImport {
79+
pub fn exact_case_sensitive(s: String) -> NameToImport {
80+
NameToImport::Exact(s, true)
81+
}
82+
}
83+
7884
impl NameToImport {
7985
pub fn text(&self) -> &str {
8086
match self {
81-
NameToImport::Exact(text) => text.as_str(),
87+
NameToImport::Exact(text, _) => text.as_str(),
8288
NameToImport::Fuzzy(text) => text.as_str(),
8389
}
8490
}
@@ -140,7 +146,7 @@ impl ImportAssets {
140146
if let Some(_) = path.qualifier() {
141147
return None;
142148
}
143-
let name = NameToImport::Exact(path.segment()?.name_ref()?.to_string());
149+
let name = NameToImport::exact_case_sensitive(path.segment()?.name_ref()?.to_string());
144150
let candidate_node = attr.syntax().clone();
145151
Some(Self {
146152
import_candidate: ImportCandidate::Path(PathImportCandidate { qualifier: None, name }),
@@ -230,6 +236,18 @@ impl ImportAssets {
230236
self.search_for(sema, None)
231237
}
232238

239+
pub fn path_fuzzy_name_to_exact(&mut self, case_sensitive: bool) {
240+
if let ImportCandidate::Path(PathImportCandidate { name: to_import, .. }) =
241+
&mut self.import_candidate
242+
{
243+
let name = match to_import {
244+
NameToImport::Fuzzy(name) => std::mem::take(name),
245+
_ => return,
246+
};
247+
*to_import = NameToImport::Exact(name, case_sensitive);
248+
}
249+
}
250+
233251
fn search_for(
234252
&self,
235253
sema: &Semantics<RootDatabase>,
@@ -561,7 +579,9 @@ impl ImportCandidate {
561579
Some(_) => None,
562580
None => Some(Self::TraitMethod(TraitImportCandidate {
563581
receiver_ty: sema.type_of_expr(&method_call.receiver()?)?.adjusted(),
564-
assoc_item_name: NameToImport::Exact(method_call.name_ref()?.to_string()),
582+
assoc_item_name: NameToImport::exact_case_sensitive(
583+
method_call.name_ref()?.to_string(),
584+
),
565585
})),
566586
}
567587
}
@@ -573,7 +593,7 @@ impl ImportCandidate {
573593
path_import_candidate(
574594
sema,
575595
path.qualifier(),
576-
NameToImport::Exact(path.segment()?.name_ref()?.to_string()),
596+
NameToImport::exact_case_sensitive(path.segment()?.name_ref()?.to_string()),
577597
)
578598
}
579599

@@ -587,7 +607,7 @@ impl ImportCandidate {
587607
}
588608
Some(ImportCandidate::Path(PathImportCandidate {
589609
qualifier: None,
590-
name: NameToImport::Exact(name.to_string()),
610+
name: NameToImport::exact_case_sensitive(name.to_string()),
591611
}))
592612
}
593613

crates/ide_db/src/items_locator.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,18 @@ pub fn items_with_name<'a>(
5050
});
5151

5252
let (mut local_query, mut external_query) = match name {
53-
NameToImport::Exact(exact_name) => {
53+
NameToImport::Exact(exact_name, case_sensitive) => {
5454
let mut local_query = symbol_index::Query::new(exact_name.clone());
5555
local_query.exact();
5656

5757
let external_query = import_map::Query::new(exact_name)
5858
.name_only()
59-
.search_mode(import_map::SearchMode::Equals)
60-
.case_sensitive();
59+
.search_mode(import_map::SearchMode::Equals);
6160

62-
(local_query, external_query)
61+
(
62+
local_query,
63+
if case_sensitive { external_query.case_sensitive() } else { external_query },
64+
)
6365
}
6466
NameToImport::Fuzzy(fuzzy_search_string) => {
6567
let mut local_query = symbol_index::Query::new(fuzzy_search_string.clone());

0 commit comments

Comments
 (0)