1+ const SEARCH_BLOCK_START = "------- SEARCH"
2+ const SEARCH_BLOCK_END = "======="
3+ const REPLACE_BLOCK_END = "+++++++ REPLACE"
4+
5+ const SEARCH_BLOCK_CHAR = "-"
6+ const REPLACE_BLOCK_CHAR = "+"
7+
18/**
29 * Attempts a line-trimmed fallback match for the given search content in the original content.
310 * It tries to match `searchContent` lines against a block of lines in `originalContent` starting
@@ -150,11 +157,11 @@ function blockAnchorFallbackMatch(originalContent: string, searchContent: string
150157 *
151158 * The diff format is a custom structure that uses three markers to define changes:
152159 *
153- * <<<<<<< SEARCH
160+ * ------- SEARCH
154161 * [Exact content to find in the original file]
155162 * =======
156163 * [Content to replace with]
157- * >>>>>>> REPLACE
164+ * +++++++ REPLACE
158165 *
159166 * Behavior and Assumptions:
160167 * 1. The file is processed chunk-by-chunk. Each chunk of `diffContent` may contain
@@ -243,23 +250,23 @@ async function constructNewFileContentV1(diffContent: string, originalContent: s
243250 const lastLine = lines [ lines . length - 1 ]
244251 if (
245252 lines . length > 0 &&
246- ( lastLine . startsWith ( "<" ) || lastLine . startsWith ( "=" ) || lastLine . startsWith ( ">" ) ) &&
247- lastLine !== "<<<<<<< SEARCH" &&
248- lastLine !== "=======" &&
249- lastLine !== ">>>>>>> REPLACE"
253+ ( lastLine . startsWith ( SEARCH_BLOCK_CHAR ) || lastLine . startsWith ( "=" ) || lastLine . startsWith ( REPLACE_BLOCK_CHAR ) ) &&
254+ lastLine !== SEARCH_BLOCK_START &&
255+ lastLine !== SEARCH_BLOCK_END &&
256+ lastLine !== REPLACE_BLOCK_END
250257 ) {
251258 lines . pop ( )
252259 }
253260
254261 for ( const line of lines ) {
255- if ( line === "<<<<<<< SEARCH" ) {
262+ if ( line === SEARCH_BLOCK_START ) {
256263 inSearch = true
257264 currentSearchContent = ""
258265 currentReplaceContent = ""
259266 continue
260267 }
261268
262- if ( line === "=======" ) {
269+ if ( line === SEARCH_BLOCK_END ) {
263270 inSearch = false
264271 inReplace = true
265272
@@ -320,7 +327,7 @@ async function constructNewFileContentV1(diffContent: string, originalContent: s
320327 continue
321328 }
322329
323- if ( line === ">>>>>>> REPLACE" ) {
330+ if ( line === REPLACE_BLOCK_END ) {
324331 // Finished one replace block
325332
326333 // // Remove the artificially added linebreak in the last line of the REPLACE block
@@ -480,7 +487,7 @@ class NewFileContentConstructor {
480487 pendingNonStandardLineLimit : number ,
481488 ) : number {
482489 let removeLineCount = 0
483- if ( line === "<<<<<<< SEARCH" ) {
490+ if ( line === SEARCH_BLOCK_START ) {
484491 removeLineCount = this . trimPendingNonStandardTrailingEmptyLines ( pendingNonStandardLineLimit )
485492 if ( removeLineCount > 0 ) {
486493 pendingNonStandardLineLimit = pendingNonStandardLineLimit - removeLineCount
@@ -490,15 +497,15 @@ class NewFileContentConstructor {
490497 canWritependingNonStandardLines && ( this . pendingNonStandardLines . length = 0 )
491498 }
492499 this . activateSearchState ( )
493- } else if ( line === "=======" ) {
500+ } else if ( line === SEARCH_BLOCK_END ) {
494501 // 校验非标内容
495502 if ( ! this . isSearchingActive ( ) ) {
496503 this . tryFixSearchBlock ( pendingNonStandardLineLimit )
497504 canWritependingNonStandardLines && ( this . pendingNonStandardLines . length = 0 )
498505 }
499506 this . activateReplaceState ( )
500507 this . beforeReplace ( )
501- } else if ( line === ">>>>>>> REPLACE" ) {
508+ } else if ( line === REPLACE_BLOCK_END ) {
502509 if ( ! this . isReplacingActive ( ) ) {
503510 this . tryFixReplaceBlock ( pendingNonStandardLineLimit )
504511 canWritependingNonStandardLines && ( this . pendingNonStandardLines . length = 0 )
@@ -606,11 +613,11 @@ class NewFileContentConstructor {
606613 if ( ! lineLimit ) {
607614 throw new Error ( "Invalid SEARCH/REPLACE block structure - no lines available to process" )
608615 }
609- let searchTagRegexp = / ^ [ < ] { 3 , } S E A R C H $ /
616+ let searchTagRegexp = / ^ [ - ] { 3 , } S E A R C H $ /
610617 const searchTagIndex = this . findLastMatchingLineIndex ( searchTagRegexp , lineLimit )
611618 if ( searchTagIndex !== - 1 ) {
612619 let fixLines = this . pendingNonStandardLines . slice ( searchTagIndex , lineLimit )
613- fixLines [ 0 ] = "<<<<<<< SEARCH"
620+ fixLines [ 0 ] = SEARCH_BLOCK_START
614621 for ( const line of fixLines ) {
615622 removeLineCount += this . internalProcessLine ( line , false , searchTagIndex )
616623 }
@@ -638,7 +645,7 @@ class NewFileContentConstructor {
638645 // removeLineCount += this.tryFixSearchBlock(replaceBeginTagIndex)
639646 // }
640647 let fixLines = this . pendingNonStandardLines . slice ( replaceBeginTagIndex - removeLineCount , lineLimit - removeLineCount )
641- fixLines [ 0 ] = "======="
648+ fixLines [ 0 ] = SEARCH_BLOCK_END
642649 for ( const line of fixLines ) {
643650 removeLineCount += this . internalProcessLine ( line , false , replaceBeginTagIndex - removeLineCount )
644651 }
@@ -657,7 +664,7 @@ class NewFileContentConstructor {
657664 throw new Error ( )
658665 }
659666
660- let replaceEndTagRegexp = / ^ [ > ] { 3 , } R E P L A C E $ /
667+ let replaceEndTagRegexp = / ^ [ + ] { 3 , } R E P L A C E $ /
661668 const replaceEndTagIndex = this . findLastMatchingLineIndex ( replaceEndTagRegexp , lineLimit )
662669 const likeReplaceEndTag = replaceEndTagIndex === lineLimit - 1
663670 if ( likeReplaceEndTag ) {
@@ -666,7 +673,7 @@ class NewFileContentConstructor {
666673 // removeLineCount += this.tryFixReplaceBlock(replaceEndTagIndex)
667674 // }
668675 let fixLines = this . pendingNonStandardLines . slice ( replaceEndTagIndex - removeLineCount , lineLimit - removeLineCount )
669- fixLines [ fixLines . length - 1 ] = ">>>>>>> REPLACE"
676+ fixLines [ fixLines . length - 1 ] = REPLACE_BLOCK_END
670677 for ( const line of fixLines ) {
671678 removeLineCount += this . internalProcessLine ( line , false , replaceEndTagIndex - removeLineCount )
672679 }
@@ -706,10 +713,10 @@ export async function constructNewFileContentV2(diffContent: string, originalCon
706713 const lastLine = lines [ lines . length - 1 ]
707714 if (
708715 lines . length > 0 &&
709- ( lastLine . startsWith ( "<" ) || lastLine . startsWith ( "=" ) || lastLine . startsWith ( ">" ) ) &&
710- lastLine !== "<<<<<<< SEARCH" &&
711- lastLine !== "=======" &&
712- lastLine !== ">>>>>>> REPLACE"
716+ ( lastLine . startsWith ( SEARCH_BLOCK_CHAR ) || lastLine . startsWith ( "=" ) || lastLine . startsWith ( REPLACE_BLOCK_CHAR ) ) &&
717+ lastLine !== SEARCH_BLOCK_START &&
718+ lastLine !== SEARCH_BLOCK_END &&
719+ lastLine !== REPLACE_BLOCK_END
713720 ) {
714721 lines . pop ( )
715722 }
0 commit comments