11use crate :: { MatchFinder , SsrRule } ;
22use expect:: { expect, Expect } ;
3- use ra_db:: { salsa:: Durability , FileId , SourceDatabaseExt } ;
3+ use ra_db:: { salsa:: Durability , FileId , FilePosition , SourceDatabaseExt } ;
44use rustc_hash:: FxHashSet ;
55use std:: sync:: Arc ;
66use test_utils:: mark;
@@ -59,24 +59,30 @@ fn parser_undefined_placeholder_in_replacement() {
5959 ) ;
6060}
6161
62- pub ( crate ) fn single_file ( code : & str ) -> ( ra_ide_db:: RootDatabase , FileId ) {
62+ /// `code` may optionally contain a cursor marker `<|>`. If it doesn't, then the position will be
63+ /// the start of the file.
64+ pub ( crate ) fn single_file ( code : & str ) -> ( ra_ide_db:: RootDatabase , FilePosition ) {
6365 use ra_db:: fixture:: WithFixture ;
6466 use ra_ide_db:: symbol_index:: SymbolsDatabase ;
65- let ( db, file_id) = ra_ide_db:: RootDatabase :: with_single_file ( code) ;
66- let mut db = db;
67+ let ( mut db, position) = if code. contains ( test_utils:: CURSOR_MARKER ) {
68+ ra_ide_db:: RootDatabase :: with_position ( code)
69+ } else {
70+ let ( db, file_id) = ra_ide_db:: RootDatabase :: with_single_file ( code) ;
71+ ( db, FilePosition { file_id, offset : 0 . into ( ) } )
72+ } ;
6773 let mut local_roots = FxHashSet :: default ( ) ;
6874 local_roots. insert ( ra_db:: fixture:: WORKSPACE ) ;
6975 db. set_local_roots_with_durability ( Arc :: new ( local_roots) , Durability :: HIGH ) ;
70- ( db, file_id )
76+ ( db, position )
7177}
7278
7379fn assert_ssr_transform ( rule : & str , input : & str , expected : Expect ) {
7480 assert_ssr_transforms ( & [ rule] , input, expected) ;
7581}
7682
7783fn assert_ssr_transforms ( rules : & [ & str ] , input : & str , expected : Expect ) {
78- let ( db, file_id ) = single_file ( input) ;
79- let mut match_finder = MatchFinder :: new ( & db) ;
84+ let ( db, position ) = single_file ( input) ;
85+ let mut match_finder = MatchFinder :: in_context ( & db, position ) ;
8086 for rule in rules {
8187 let rule: SsrRule = rule. parse ( ) . unwrap ( ) ;
8288 match_finder. add_rule ( rule) ;
@@ -85,10 +91,10 @@ fn assert_ssr_transforms(rules: &[&str], input: &str, expected: Expect) {
8591 if edits. is_empty ( ) {
8692 panic ! ( "No edits were made" ) ;
8793 }
88- assert_eq ! ( edits[ 0 ] . file_id, file_id) ;
94+ assert_eq ! ( edits[ 0 ] . file_id, position . file_id) ;
8995 // Note, db.file_text is not necessarily the same as `input`, since fixture parsing alters
9096 // stuff.
91- let mut actual = db. file_text ( file_id) . to_string ( ) ;
97+ let mut actual = db. file_text ( position . file_id ) . to_string ( ) ;
9298 edits[ 0 ] . edit . apply ( & mut actual) ;
9399 expected. assert_eq ( & actual) ;
94100}
@@ -106,34 +112,34 @@ fn print_match_debug_info(match_finder: &MatchFinder, file_id: FileId, snippet:
106112}
107113
108114fn assert_matches ( pattern : & str , code : & str , expected : & [ & str ] ) {
109- let ( db, file_id ) = single_file ( code) ;
110- let mut match_finder = MatchFinder :: new ( & db) ;
115+ let ( db, position ) = single_file ( code) ;
116+ let mut match_finder = MatchFinder :: in_context ( & db, position ) ;
111117 match_finder. add_search_pattern ( pattern. parse ( ) . unwrap ( ) ) ;
112118 let matched_strings: Vec < String > =
113119 match_finder. matches ( ) . flattened ( ) . matches . iter ( ) . map ( |m| m. matched_text ( ) ) . collect ( ) ;
114120 if matched_strings != expected && !expected. is_empty ( ) {
115- print_match_debug_info ( & match_finder, file_id, & expected[ 0 ] ) ;
121+ print_match_debug_info ( & match_finder, position . file_id , & expected[ 0 ] ) ;
116122 }
117123 assert_eq ! ( matched_strings, expected) ;
118124}
119125
120126fn assert_no_match ( pattern : & str , code : & str ) {
121- let ( db, file_id ) = single_file ( code) ;
122- let mut match_finder = MatchFinder :: new ( & db) ;
127+ let ( db, position ) = single_file ( code) ;
128+ let mut match_finder = MatchFinder :: in_context ( & db, position ) ;
123129 match_finder. add_search_pattern ( pattern. parse ( ) . unwrap ( ) ) ;
124130 let matches = match_finder. matches ( ) . flattened ( ) . matches ;
125131 if !matches. is_empty ( ) {
126- print_match_debug_info ( & match_finder, file_id, & matches[ 0 ] . matched_text ( ) ) ;
132+ print_match_debug_info ( & match_finder, position . file_id , & matches[ 0 ] . matched_text ( ) ) ;
127133 panic ! ( "Got {} matches when we expected none: {:#?}" , matches. len( ) , matches) ;
128134 }
129135}
130136
131137fn assert_match_failure_reason ( pattern : & str , code : & str , snippet : & str , expected_reason : & str ) {
132- let ( db, file_id ) = single_file ( code) ;
133- let mut match_finder = MatchFinder :: new ( & db) ;
138+ let ( db, position ) = single_file ( code) ;
139+ let mut match_finder = MatchFinder :: in_context ( & db, position ) ;
134140 match_finder. add_search_pattern ( pattern. parse ( ) . unwrap ( ) ) ;
135141 let mut reasons = Vec :: new ( ) ;
136- for d in match_finder. debug_where_text_equal ( file_id, snippet) {
142+ for d in match_finder. debug_where_text_equal ( position . file_id , snippet) {
137143 if let Some ( reason) = d. match_failure_reason ( ) {
138144 reasons. push ( reason. to_owned ( ) ) ;
139145 }
0 commit comments