@@ -161,84 +161,86 @@ const convert_code_block_to_new_file_diff = (params: {
161161 let path_line_index = - 1
162162 let content_lines : string [ ] = [ ]
163163
164- if ( ! file_path ) {
165- // Check for XML path format
166- const first_line = params . lines . length > 0 ? params . lines [ 0 ] . trim ( ) : ''
167- const xml_match = first_line . match ( xml_path_regex )
164+ // Check for XML path format
165+ const first_line = params . lines . length > 0 ? params . lines [ 0 ] . trim ( ) : ''
166+ const xml_match = first_line . match ( xml_path_regex )
168167
169- if ( xml_match && xml_match [ 1 ] ) {
170- file_path = xml_match [ 1 ] . replace ( / \\ / g, '/' )
168+ if ( xml_match && xml_match [ 1 ] ) {
169+ file_path = xml_match [ 1 ] . replace ( / \\ / g, '/' )
171170
172- const file_tag_start = params . lines . findIndex ( ( l ) =>
173- l . trim ( ) . startsWith ( '<file' )
174- )
175- let file_tag_end = - 1
176- for ( let i = params . lines . length - 1 ; i >= 0 ; i -- ) {
177- if ( params . lines [ i ] . trim ( ) . startsWith ( '</file>' ) ) {
178- file_tag_end = i
179- break
180- }
171+ const file_tag_start = params . lines . findIndex ( ( l ) =>
172+ l . trim ( ) . startsWith ( '<file' )
173+ )
174+ let file_tag_end = - 1
175+ for ( let i = params . lines . length - 1 ; i >= 0 ; i -- ) {
176+ if ( params . lines [ i ] . trim ( ) . startsWith ( '</file>' ) ) {
177+ file_tag_end = i
178+ break
181179 }
180+ }
182181
183- if (
184- file_tag_start != - 1 &&
185- file_tag_end != - 1 &&
186- file_tag_end > file_tag_start
187- ) {
188- const inner_lines = params . lines . slice ( file_tag_start + 1 , file_tag_end )
182+ if (
183+ file_tag_start != - 1 &&
184+ file_tag_end != - 1 &&
185+ file_tag_end > file_tag_start
186+ ) {
187+ const inner_lines = params . lines . slice ( file_tag_start + 1 , file_tag_end )
189188
190- const cdata_start = inner_lines . findIndex ( ( l ) =>
191- l . trim ( ) . startsWith ( '<![CDATA[' )
192- )
193- let cdata_end = - 1
194- for ( let i = inner_lines . length - 1 ; i >= 0 ; i -- ) {
195- if ( inner_lines [ i ] . trim ( ) . endsWith ( ']]>' ) ) {
196- cdata_end = i
197- break
198- }
189+ const cdata_start = inner_lines . findIndex ( ( l ) =>
190+ l . trim ( ) . startsWith ( '<![CDATA[' )
191+ )
192+ let cdata_end = - 1
193+ for ( let i = inner_lines . length - 1 ; i >= 0 ; i -- ) {
194+ if ( inner_lines [ i ] . trim ( ) . endsWith ( ']]>' ) ) {
195+ cdata_end = i
196+ break
199197 }
198+ }
200199
201- if ( cdata_start != - 1 && cdata_end != - 1 && cdata_end > cdata_start ) {
202- content_lines = inner_lines . slice ( cdata_start + 1 , cdata_end )
203- } else {
204- content_lines = inner_lines
205- }
200+ if ( cdata_start != - 1 && cdata_end != - 1 && cdata_end > cdata_start ) {
201+ content_lines = inner_lines . slice ( cdata_start + 1 , cdata_end )
206202 } else {
207- // malformed, maybe path is on first line, and content follows
208- content_lines = params . lines . slice ( 1 )
203+ content_lines = inner_lines
209204 }
210205 } else {
211- // Look for a file path in the first few lines of the code block
212- for ( let i = 0 ; i < Math . min ( params . lines . length , 5 ) ; i ++ ) {
213- const line = params . lines [ i ] . trim ( )
214- const match = line . match ( path_regex )
215-
216- if ( match && match [ 1 ] ) {
217- const potential_path = match [ 1 ]
218-
219- if (
220- ( potential_path . includes ( '.' ) || potential_path . includes ( '/' ) ) &&
221- ! potential_path . includes ( ' ' )
222- ) {
223- const rest_of_line = line
224- . substring ( line . indexOf ( potential_path ) + potential_path . length )
225- . trim ( )
226- // e.g. `25:5` for line and column, or `-->` for html comments
227- const is_just_path_and_location = / ^ (?: \d + : \d + ) ? \s * ( - - > ) ? \s * $ / . test (
228- rest_of_line
229- )
206+ // malformed, maybe path is on first line, and content follows
207+ content_lines = params . lines . slice ( 1 )
208+ }
209+ } else {
210+ // Look for a file path in the first few lines of the code block
211+ for ( let i = 0 ; i < Math . min ( params . lines . length , 5 ) ; i ++ ) {
212+ const line = params . lines [ i ] . trim ( )
213+ const match = line . match ( path_regex )
214+
215+ if ( match && match [ 1 ] ) {
216+ const potential_path = match [ 1 ]
217+
218+ if (
219+ ( potential_path . includes ( '.' ) || potential_path . includes ( '/' ) ) &&
220+ ! potential_path . includes ( ' ' )
221+ ) {
222+ const rest_of_line = line
223+ . substring ( line . indexOf ( potential_path ) + potential_path . length )
224+ . trim ( )
225+ // e.g. `25:5` for line and column, or `-->` for html comments
226+ const is_just_path_and_location = / ^ (?: \d + : \d + ) ? \s * ( - - > ) ? \s * $ / . test (
227+ rest_of_line
228+ )
230229
231- if ( is_just_path_and_location ) {
232- file_path = potential_path . replace ( / \\ / g, '/' ) // normalize backslashes
233- path_line_index = i
234- break // Found it, stop searching.
235- }
230+ if ( is_just_path_and_location ) {
231+ file_path = potential_path . replace ( / \\ / g, '/' ) // normalize backslashes
232+ path_line_index = i
233+ break // Found it, stop searching.
236234 }
237235 }
238236 }
239237 }
240238 }
241239
240+ if ( ! file_path ) {
241+ file_path = params . file_path_hint
242+ }
243+
242244 if ( path_line_index != - 1 ) {
243245 content_lines = params . lines . filter ( ( _ , index ) => index != path_line_index )
244246 } else if ( file_path && content_lines . length == 0 ) {
0 commit comments