Skip to content

Commit 4bb7702

Browse files
committed
check_edit test
1 parent bc2b70d commit 4bb7702

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

crates/ide-completion/src/tests.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ mod type_pos;
2323
mod use_tree;
2424
mod visibility;
2525

26+
use std::ops::ControlFlow;
27+
2628
use expect_test::Expect;
2729
use hir::PrefixKind;
2830
use ide_db::{
@@ -185,11 +187,29 @@ pub(crate) fn check_edit_with_config(
185187
let (db, position) = position(ra_fixture_before);
186188
let completions: Vec<CompletionItem> =
187189
crate::completions(&db, &config, position, None).unwrap();
188-
let (completion,) = completions
190+
let matching = completions
189191
.iter()
190-
.filter(|it| it.lookup() == what)
191-
.collect_tuple()
192-
.unwrap_or_else(|| panic!("can't find {what:?} completion in {completions:#?}"));
192+
// Match IDE behavior by considering completions as matching if `what` is a subsequence
193+
// of the completion's lookup text.
194+
.filter(|it| {
195+
let mut lookup = it.lookup().chars();
196+
what.chars().all(|c| lookup.contains(&c))
197+
})
198+
// Select the first exact match if one exists, or the first subsequence match if not
199+
.try_fold(None, |first_match, completion| {
200+
let exact_match = completion.lookup() == what;
201+
if exact_match {
202+
ControlFlow::Break(completion)
203+
} else {
204+
ControlFlow::Continue(first_match.or(Some(completion)))
205+
}
206+
});
207+
let completion = match matching {
208+
ControlFlow::Continue(first_match) => first_match
209+
.unwrap_or_else(|| panic!("can't find {what:?} completion in {completions:#?}")),
210+
ControlFlow::Break(exact_match) => exact_match,
211+
};
212+
193213
let mut actual = db.file_text(position.file_id).to_string();
194214

195215
let mut combined_edit = completion.text_edit.clone();

crates/ide-completion/src/tests/special.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,3 +1280,28 @@ fn here_we_go() {
12801280
"#]],
12811281
);
12821282
}
1283+
1284+
#[test]
1285+
fn completion_filtering_excludes_non_identifier_aliases() {
1286+
// Catch panic instead of using `#[should_panic]` as style check bans
1287+
// `#[should_panic]`. Making `check_edit` return a result would require
1288+
// a lot of test changes.
1289+
std::panic::catch_unwind(|| {
1290+
check_edit(
1291+
"Partial>",
1292+
r#"
1293+
#[doc(alias = ">")]
1294+
trait PartialOrd {}
1295+
1296+
struct Foo<T: Partial$0
1297+
"#,
1298+
r#"
1299+
#[doc(alias = ">")]
1300+
trait PartialOrd {}
1301+
1302+
struct Foo<T: PartialOrd
1303+
"#,
1304+
)
1305+
})
1306+
.unwrap_err();
1307+
}

0 commit comments

Comments
 (0)