@@ -23,6 +23,8 @@ mod type_pos;
2323mod use_tree;
2424mod visibility;
2525
26+ use std:: ops:: ControlFlow ;
27+
2628use expect_test:: Expect ;
2729use hir:: PrefixKind ;
2830use 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 ( ) ;
0 commit comments