@@ -23,6 +23,8 @@ mod type_pos;
23
23
mod use_tree;
24
24
mod visibility;
25
25
26
+ use std:: ops:: ControlFlow ;
27
+
26
28
use expect_test:: Expect ;
27
29
use hir:: PrefixKind ;
28
30
use ide_db:: {
@@ -185,11 +187,29 @@ pub(crate) fn check_edit_with_config(
185
187
let ( db, position) = position ( ra_fixture_before) ;
186
188
let completions: Vec < CompletionItem > =
187
189
crate :: completions ( & db, & config, position, None ) . unwrap ( ) ;
188
- let ( completion , ) = completions
190
+ let matching = completions
189
191
. 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
+
193
213
let mut actual = db. file_text ( position. file_id ) . to_string ( ) ;
194
214
195
215
let mut combined_edit = completion. text_edit . clone ( ) ;
0 commit comments