@@ -2,6 +2,7 @@ mod generated;
22
33use hir:: Semantics ;
44use ide_db:: base_db:: { fixture:: WithFixture , FileId , FileRange , SourceDatabaseExt } ;
5+ use ide_db:: source_change:: FileSystemEdit ;
56use ide_db:: RootDatabase ;
67use syntax:: TextRange ;
78use test_utils:: { assert_eq_text, extract_offset, extract_range} ;
@@ -47,7 +48,7 @@ fn check_doc_test(assist_id: &str, before: &str, after: &str) {
4748 let before = db. file_text ( file_id) . to_string ( ) ;
4849 let frange = FileRange { file_id, range : selection. into ( ) } ;
4950
50- let mut assist = Assist :: resolved ( & db, & AssistConfig :: default ( ) , frange)
51+ let assist = Assist :: resolved ( & db, & AssistConfig :: default ( ) , frange)
5152 . into_iter ( )
5253 . find ( |assist| assist. assist . id . 0 == assist_id)
5354 . unwrap_or_else ( || {
@@ -63,9 +64,12 @@ fn check_doc_test(assist_id: &str, before: &str, after: &str) {
6364 } ) ;
6465
6566 let actual = {
66- let change = assist. source_change . source_file_edits . pop ( ) . unwrap ( ) ;
6767 let mut actual = before;
68- change. edit . apply ( & mut actual) ;
68+ for source_file_edit in assist. source_change . source_file_edits {
69+ if source_file_edit. file_id == file_id {
70+ source_file_edit. edit . apply ( & mut actual)
71+ }
72+ }
6973 actual
7074 } ;
7175 assert_eq_text ! ( & after, & actual) ;
@@ -99,20 +103,52 @@ fn check(handler: Handler, before: &str, expected: ExpectedResult, assist_label:
99103 ( Some ( assist) , ExpectedResult :: After ( after) ) => {
100104 let mut source_change = assist. source_change ;
101105 assert ! ( !source_change. source_file_edits. is_empty( ) ) ;
102- let skip_header = source_change. source_file_edits . len ( ) == 1 ;
106+ let skip_header = source_change. source_file_edits . len ( ) == 1
107+ && source_change. file_system_edits . len ( ) == 0 ;
103108 source_change. source_file_edits . sort_by_key ( |it| it. file_id ) ;
104109
110+ let mut created_file_ids = Vec :: new ( ) ;
105111 let mut buf = String :: new ( ) ;
112+ for file_system_edit in source_change. file_system_edits . clone ( ) {
113+ match file_system_edit {
114+ FileSystemEdit :: CreateFile { dst } => {
115+ created_file_ids. push ( dst. anchor ) ;
116+ }
117+ _ => ( ) ,
118+ }
119+ }
120+
106121 for source_file_edit in source_change. source_file_edits {
107- let mut text = db. file_text ( source_file_edit. file_id ) . as_ref ( ) . to_owned ( ) ;
108- source_file_edit. edit . apply ( & mut text) ;
109- if !skip_header {
110- let sr = db. file_source_root ( source_file_edit. file_id ) ;
111- let sr = db. source_root ( sr) ;
112- let path = sr. path_for_file ( & source_file_edit. file_id ) . unwrap ( ) ;
113- format_to ! ( buf, "//- {}\n " , path)
122+ if created_file_ids. contains ( & source_file_edit. file_id ) {
123+ let target_dst = source_change
124+ . file_system_edits
125+ . iter ( )
126+ . find_map ( |f| match f {
127+ FileSystemEdit :: CreateFile { dst } => {
128+ if dst. anchor == source_file_edit. file_id {
129+ Some ( & dst. path )
130+ } else {
131+ None
132+ }
133+ }
134+ _ => None ,
135+ } )
136+ . unwrap ( ) ;
137+ format_to ! ( buf, "//- {}\n " , target_dst) ;
138+ let mut text = String :: new ( ) ;
139+ source_file_edit. edit . apply ( & mut text) ;
140+ buf. push_str ( & text) ;
141+ } else {
142+ let mut text = db. file_text ( source_file_edit. file_id ) . as_ref ( ) . to_owned ( ) ;
143+ source_file_edit. edit . apply ( & mut text) ;
144+ if !skip_header {
145+ let sr = db. file_source_root ( source_file_edit. file_id ) ;
146+ let sr = db. source_root ( sr) ;
147+ let path = sr. path_for_file ( & source_file_edit. file_id ) . unwrap ( ) ;
148+ format_to ! ( buf, "//- {}\n " , path)
149+ }
150+ buf. push_str ( & text) ;
114151 }
115- buf. push_str ( & text) ;
116152 }
117153
118154 assert_eq_text ! ( after, & buf) ;
0 commit comments