| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827 |
- const SEARCH_BLOCK_START = "------- SEARCH"
- const SEARCH_BLOCK_END = "======="
- const REPLACE_BLOCK_END = "+++++++ REPLACE"
- const SEARCH_BLOCK_CHAR = "-"
- const REPLACE_BLOCK_CHAR = "+"
- const LEGACY_SEARCH_BLOCK_CHAR = "<"
- const LEGACY_REPLACE_BLOCK_CHAR = ">"
- // Replace the exact string constants with flexible regex patterns
- const SEARCH_BLOCK_START_REGEX = /^[-]{3,} SEARCH$/
- const SEARCH_BLOCK_END_REGEX = /^[=]{3,}$/
- const REPLACE_BLOCK_END_REGEX = /^[+]{3,} REPLACE$/
- const LEGACY_SEARCH_BLOCK_START_REGEX = /^[<]{3,} SEARCH$/
- const LEGACY_REPLACE_BLOCK_END_REGEX = /^[>]{3,} REPLACE$/
- // Helper functions to check if a line matches the flexible patterns
- function isSearchBlockStart(line: string): boolean {
- return SEARCH_BLOCK_START_REGEX.test(line) || LEGACY_SEARCH_BLOCK_START_REGEX.test(line)
- }
- function isSearchBlockEnd(line: string): boolean {
- return SEARCH_BLOCK_END_REGEX.test(line)
- }
- function isReplaceBlockEnd(line: string): boolean {
- return REPLACE_BLOCK_END_REGEX.test(line) || LEGACY_REPLACE_BLOCK_END_REGEX.test(line)
- }
- /**
- * Attempts a line-trimmed fallback match for the given search content in the original content.
- * It tries to match `searchContent` lines against a block of lines in `originalContent` starting
- * from `lastProcessedIndex`. Lines are matched by trimming leading/trailing whitespace and ensuring
- * they are identical afterwards.
- *
- * Returns [matchIndexStart, matchIndexEnd] if found, or false if not found.
- */
- function lineTrimmedFallbackMatch(originalContent: string, searchContent: string, startIndex: number): [number, number] | false {
- // Split both contents into lines
- const originalLines = originalContent.split("\n")
- const searchLines = searchContent.split("\n")
- // Trim trailing empty line if exists (from the trailing \n in searchContent)
- if (searchLines[searchLines.length - 1] === "") {
- searchLines.pop()
- }
- // Find the line number where startIndex falls
- let startLineNum = 0
- let currentIndex = 0
- while (currentIndex < startIndex && startLineNum < originalLines.length) {
- currentIndex += originalLines[startLineNum].length + 1 // +1 for \n
- startLineNum++
- }
- // For each possible starting position in original content
- for (let i = startLineNum; i <= originalLines.length - searchLines.length; i++) {
- let matches = true
- // Try to match all search lines from this position
- for (let j = 0; j < searchLines.length; j++) {
- const originalTrimmed = originalLines[i + j].trim()
- const searchTrimmed = searchLines[j].trim()
- if (originalTrimmed !== searchTrimmed) {
- matches = false
- break
- }
- }
- // If we found a match, calculate the exact character positions
- if (matches) {
- // Find start character index
- let matchStartIndex = 0
- for (let k = 0; k < i; k++) {
- matchStartIndex += originalLines[k].length + 1 // +1 for \n
- }
- // Find end character index
- let matchEndIndex = matchStartIndex
- for (let k = 0; k < searchLines.length; k++) {
- matchEndIndex += originalLines[i + k].length + 1 // +1 for \n
- }
- return [matchStartIndex, matchEndIndex]
- }
- }
- return false
- }
- /**
- * Attempts to match blocks of code by using the first and last lines as anchors.
- * This is a third-tier fallback strategy that helps match blocks where we can identify
- * the correct location by matching the beginning and end, even if the exact content
- * differs slightly.
- *
- * The matching strategy:
- * 1. Only attempts to match blocks of 3 or more lines to avoid false positives
- * 2. Extracts from the search content:
- * - First line as the "start anchor"
- * - Last line as the "end anchor"
- * 3. For each position in the original content:
- * - Checks if the next line matches the start anchor
- * - If it does, jumps ahead by the search block size
- * - Checks if that line matches the end anchor
- * - All comparisons are done after trimming whitespace
- *
- * This approach is particularly useful for matching blocks of code where:
- * - The exact content might have minor differences
- * - The beginning and end of the block are distinctive enough to serve as anchors
- * - The overall structure (number of lines) remains the same
- *
- * @param originalContent - The full content of the original file
- * @param searchContent - The content we're trying to find in the original file
- * @param startIndex - The character index in originalContent where to start searching
- * @returns A tuple of [startIndex, endIndex] if a match is found, false otherwise
- */
- function blockAnchorFallbackMatch(originalContent: string, searchContent: string, startIndex: number): [number, number] | false {
- const originalLines = originalContent.split("\n")
- const searchLines = searchContent.split("\n")
- // Only use this approach for blocks of 3+ lines
- if (searchLines.length < 3) {
- return false
- }
- // Trim trailing empty line if exists
- if (searchLines[searchLines.length - 1] === "") {
- searchLines.pop()
- }
- const firstLineSearch = searchLines[0].trim()
- const lastLineSearch = searchLines[searchLines.length - 1].trim()
- const searchBlockSize = searchLines.length
- // Find the line number where startIndex falls
- let startLineNum = 0
- let currentIndex = 0
- while (currentIndex < startIndex && startLineNum < originalLines.length) {
- currentIndex += originalLines[startLineNum].length + 1
- startLineNum++
- }
- // Look for matching start and end anchors
- for (let i = startLineNum; i <= originalLines.length - searchBlockSize; i++) {
- // Check if first line matches
- if (originalLines[i].trim() !== firstLineSearch) {
- continue
- }
- // Check if last line matches at the expected position
- if (originalLines[i + searchBlockSize - 1].trim() !== lastLineSearch) {
- continue
- }
- // Calculate exact character positions
- let matchStartIndex = 0
- for (let k = 0; k < i; k++) {
- matchStartIndex += originalLines[k].length + 1
- }
- let matchEndIndex = matchStartIndex
- for (let k = 0; k < searchBlockSize; k++) {
- matchEndIndex += originalLines[i + k].length + 1
- }
- return [matchStartIndex, matchEndIndex]
- }
- return false
- }
- /**
- * This function reconstructs the file content by applying a streamed diff (in a
- * specialized SEARCH/REPLACE block format) to the original file content. It is designed
- * to handle both incremental updates and the final resulting file after all chunks have
- * been processed.
- *
- * The diff format is a custom structure that uses three markers to define changes:
- *
- * ------- SEARCH
- * [Exact content to find in the original file]
- * =======
- * [Content to replace with]
- * +++++++ REPLACE
- *
- * Behavior and Assumptions:
- * 1. The file is processed chunk-by-chunk. Each chunk of `diffContent` may contain
- * partial or complete SEARCH/REPLACE blocks. By calling this function with each
- * incremental chunk (with `isFinal` indicating the last chunk), the final reconstructed
- * file content is produced.
- *
- * 2. Matching Strategy (in order of attempt):
- * a. Exact Match: First attempts to find the exact SEARCH block text in the original file
- * b. Line-Trimmed Match: Falls back to line-by-line comparison ignoring leading/trailing whitespace
- * c. Block Anchor Match: For blocks of 3+ lines, tries to match using first/last lines as anchors
- * If all matching strategies fail, an error is thrown.
- *
- * 3. Empty SEARCH Section:
- * - If SEARCH is empty and the original file is empty, this indicates creating a new file
- * (pure insertion).
- * - If SEARCH is empty and the original file is not empty, this indicates a complete
- * file replacement (the entire original content is considered matched and replaced).
- *
- * 4. Applying Changes:
- * - Before encountering the "=======" marker, lines are accumulated as search content.
- * - After "=======" and before ">>>>>>> REPLACE", lines are accumulated as replacement content.
- * - Once the block is complete (">>>>>>> REPLACE"), the matched section in the original
- * file is replaced with the accumulated replacement lines, and the position in the original
- * file is advanced.
- *
- * 5. Incremental Output:
- * - As soon as the match location is found and we are in the REPLACE section, each new
- * replacement line is appended to the result so that partial updates can be viewed
- * incrementally.
- *
- * 6. Partial Markers:
- * - If the final line of the chunk looks like it might be part of a marker but is not one
- * of the known markers, it is removed. This prevents incomplete or partial markers
- * from corrupting the output.
- *
- * 7. Finalization:
- * - Once all chunks have been processed (when `isFinal` is true), any remaining original
- * content after the last replaced section is appended to the result.
- * - Trailing newlines are not forcibly added. The code tries to output exactly what is specified.
- *
- * Errors:
- * - If the search block cannot be matched using any of the available matching strategies,
- * an error is thrown.
- */
- export async function constructNewFileContent(
- diffContent: string,
- originalContent: string,
- isFinal: boolean,
- version: "v1" | "v2" = "v1",
- ): Promise<string> {
- const constructor = constructNewFileContentVersionMapping[version]
- if (!constructor) {
- throw new Error(`Invalid version '${version}' for file content constructor`)
- }
- return constructor(diffContent, originalContent, isFinal)
- }
- const constructNewFileContentVersionMapping: Record<
- string,
- (diffContent: string, originalContent: string, isFinal: boolean) => Promise<string>
- > = {
- v1: constructNewFileContentV1,
- v2: constructNewFileContentV2,
- } as const
- async function constructNewFileContentV1(diffContent: string, originalContent: string, isFinal: boolean): Promise<string> {
- let result = ""
- let lastProcessedIndex = 0
- let currentSearchContent = ""
- let currentReplaceContent = ""
- let inSearch = false
- let inReplace = false
- let searchMatchIndex = -1
- let searchEndIndex = -1
- // Track all replacements to handle out-of-order edits
- let replacements: Array<{ start: number; end: number; content: string }> = []
- let pendingOutOfOrderReplacement = false
- let lines = diffContent.split("\n")
- // If the last line looks like a partial marker but isn't recognized,
- // remove it because it might be incomplete.
- const lastLine = lines[lines.length - 1]
- if (
- lines.length > 0 &&
- (lastLine.startsWith(SEARCH_BLOCK_CHAR) ||
- lastLine.startsWith(LEGACY_SEARCH_BLOCK_CHAR) ||
- lastLine.startsWith("=") ||
- lastLine.startsWith(REPLACE_BLOCK_CHAR) ||
- lastLine.startsWith(LEGACY_REPLACE_BLOCK_CHAR)) &&
- !isSearchBlockStart(lastLine) &&
- !isSearchBlockEnd(lastLine) &&
- !isReplaceBlockEnd(lastLine)
- ) {
- lines.pop()
- }
- for (const line of lines) {
- if (isSearchBlockStart(line)) {
- inSearch = true
- currentSearchContent = ""
- currentReplaceContent = ""
- continue
- }
- if (isSearchBlockEnd(line)) {
- inSearch = false
- inReplace = true
- // Remove trailing linebreak for adding the === marker
- // if (currentSearchContent.endsWith("\r\n")) {
- // currentSearchContent = currentSearchContent.slice(0, -2)
- // } else if (currentSearchContent.endsWith("\n")) {
- // currentSearchContent = currentSearchContent.slice(0, -1)
- // }
- if (!currentSearchContent) {
- // Empty search block
- if (originalContent.length === 0) {
- // New file scenario: nothing to match, just start inserting
- searchMatchIndex = 0
- searchEndIndex = 0
- } else {
- // ERROR: Empty search block with non-empty file indicates malformed SEARCH marker
- throw new Error(
- "Empty SEARCH block detected with non-empty file. This usually indicates a malformed SEARCH marker.\n" +
- "Please ensure your SEARCH marker follows the correct format:\n" +
- "- Use '------- SEARCH' (7+ dashes + space + SEARCH)\n",
- )
- }
- } else {
- // Add check for inefficient full-file search
- // if (currentSearchContent.trim() === originalContent.trim()) {
- // throw new Error(
- // "The SEARCH block contains the entire file content. Please either:\n" +
- // "1. Use an empty SEARCH block to replace the entire file, or\n" +
- // "2. Make focused changes to specific parts of the file that need modification.",
- // )
- // }
- // Exact search match scenario
- const exactIndex = originalContent.indexOf(currentSearchContent, lastProcessedIndex)
- if (exactIndex !== -1) {
- searchMatchIndex = exactIndex
- searchEndIndex = exactIndex + currentSearchContent.length
- } else {
- // Attempt fallback line-trimmed matching
- const lineMatch = lineTrimmedFallbackMatch(originalContent, currentSearchContent, lastProcessedIndex)
- if (lineMatch) {
- ;[searchMatchIndex, searchEndIndex] = lineMatch
- } else {
- // Try block anchor fallback for larger blocks
- const blockMatch = blockAnchorFallbackMatch(originalContent, currentSearchContent, lastProcessedIndex)
- if (blockMatch) {
- ;[searchMatchIndex, searchEndIndex] = blockMatch
- } else {
- // Last resort: search the entire file from the beginning
- const fullFileIndex = originalContent.indexOf(currentSearchContent, 0)
- if (fullFileIndex !== -1) {
- // Found in the file - could be out of order
- searchMatchIndex = fullFileIndex
- searchEndIndex = fullFileIndex + currentSearchContent.length
- if (searchMatchIndex < lastProcessedIndex) {
- pendingOutOfOrderReplacement = true
- }
- } else {
- throw new Error(
- `The SEARCH block:\n${currentSearchContent.trimEnd()}\n...does not match anything in the file.`,
- )
- }
- }
- }
- }
- }
- // Check if this is an out-of-order replacement
- if (searchMatchIndex < lastProcessedIndex) {
- pendingOutOfOrderReplacement = true
- }
- // For in-order replacements, output everything up to the match location
- if (!pendingOutOfOrderReplacement) {
- result += originalContent.slice(lastProcessedIndex, searchMatchIndex)
- }
- continue
- }
- if (isReplaceBlockEnd(line)) {
- // Finished one replace block
- // Store this replacement
- replacements.push({
- start: searchMatchIndex,
- end: searchEndIndex,
- content: currentReplaceContent,
- })
- // If this was an in-order replacement, advance lastProcessedIndex
- if (!pendingOutOfOrderReplacement) {
- lastProcessedIndex = searchEndIndex
- }
- // Reset for next block
- inSearch = false
- inReplace = false
- currentSearchContent = ""
- currentReplaceContent = ""
- searchMatchIndex = -1
- searchEndIndex = -1
- pendingOutOfOrderReplacement = false
- continue
- }
- // Accumulate content for search or replace
- // (currentReplaceContent is not being used for anything right now since we directly append to result.)
- // (We artificially add a linebreak since we split on \n at the beginning. In order to not include a trailing linebreak in the final search/result blocks we need to remove it before using them. This allows for partial line matches to be correctly identified.)
- // NOTE: search/replace blocks must be arranged in the order they appear in the file due to how we build the content using lastProcessedIndex. We also cannot strip the trailing newline since for non-partial lines it would remove the linebreak from the original content. (If we remove end linebreak from search, then we'd also have to remove it from replace but we can't know if it's a partial line or not since the model may be using the line break to indicate the end of the block rather than as part of the search content.) We require the model to output full lines in order for our fallbacks to work as well.
- if (inSearch) {
- currentSearchContent += line + "\n"
- } else if (inReplace) {
- currentReplaceContent += line + "\n"
- // Only output replacement lines immediately for in-order replacements
- if (searchMatchIndex !== -1 && !pendingOutOfOrderReplacement) {
- result += line + "\n"
- }
- }
- }
- // If this is the final chunk, we need to apply all replacements and build the final result
- if (isFinal) {
- // Handle the case where we're still in replace mode when processing ends
- // and this is the final chunk - treat it as if we encountered the REPLACE marker
- if (inReplace && searchMatchIndex !== -1) {
- // Store this replacement
- replacements.push({
- start: searchMatchIndex,
- end: searchEndIndex,
- content: currentReplaceContent,
- })
- // If this was an in-order replacement, advance lastProcessedIndex
- if (!pendingOutOfOrderReplacement) {
- lastProcessedIndex = searchEndIndex
- }
- // Reset state
- inSearch = false
- inReplace = false
- currentSearchContent = ""
- currentReplaceContent = ""
- searchMatchIndex = -1
- searchEndIndex = -1
- pendingOutOfOrderReplacement = false
- }
- // end of handling missing replace marker
- // Sort replacements by start position
- replacements.sort((a, b) => a.start - b.start)
- // Rebuild the entire result by applying all replacements
- result = ""
- let currentPos = 0
- for (const replacement of replacements) {
- // Add original content up to this replacement
- result += originalContent.slice(currentPos, replacement.start)
- // Add the replacement content
- result += replacement.content
- // Move position to after the replaced section
- currentPos = replacement.end
- }
- // Add any remaining original content
- result += originalContent.slice(currentPos)
- }
- return result
- }
- enum ProcessingState {
- Idle = 0,
- StateSearch = 1 << 0,
- StateReplace = 1 << 1,
- }
- class NewFileContentConstructor {
- private originalContent: string
- private isFinal: boolean
- private state: number
- private pendingNonStandardLines: string[]
- private result: string
- private lastProcessedIndex: number
- private currentSearchContent: string
- private currentReplaceContent: string
- private searchMatchIndex: number
- private searchEndIndex: number
- constructor(originalContent: string, isFinal: boolean) {
- this.originalContent = originalContent
- this.isFinal = isFinal
- this.pendingNonStandardLines = []
- this.result = ""
- this.lastProcessedIndex = 0
- this.state = ProcessingState.Idle
- this.currentSearchContent = ""
- this.currentReplaceContent = ""
- this.searchMatchIndex = -1
- this.searchEndIndex = -1
- }
- private resetForNextBlock() {
- // Reset for next block
- this.state = ProcessingState.Idle
- this.currentSearchContent = ""
- this.currentReplaceContent = ""
- this.searchMatchIndex = -1
- this.searchEndIndex = -1
- }
- private findLastMatchingLineIndex(regx: RegExp, lineLimit: number) {
- for (let i = lineLimit; i > 0; ) {
- i--
- if (this.pendingNonStandardLines[i].match(regx)) {
- return i
- }
- }
- return -1
- }
- private updateProcessingState(newState: ProcessingState) {
- const isValidTransition =
- (this.state === ProcessingState.Idle && newState === ProcessingState.StateSearch) ||
- (this.state === ProcessingState.StateSearch && newState === ProcessingState.StateReplace)
- if (!isValidTransition) {
- throw new Error(
- `Invalid state transition.\n` +
- "Valid transitions are:\n" +
- "- Idle → StateSearch\n" +
- "- StateSearch → StateReplace",
- )
- }
- this.state |= newState
- }
- private isStateActive(state: ProcessingState): boolean {
- return (this.state & state) === state
- }
- private activateReplaceState() {
- this.updateProcessingState(ProcessingState.StateReplace)
- }
- private activateSearchState() {
- this.updateProcessingState(ProcessingState.StateSearch)
- this.currentSearchContent = ""
- this.currentReplaceContent = ""
- }
- private isSearchingActive(): boolean {
- return this.isStateActive(ProcessingState.StateSearch)
- }
- private isReplacingActive(): boolean {
- return this.isStateActive(ProcessingState.StateReplace)
- }
- private hasPendingNonStandardLines(pendingNonStandardLineLimit: number): boolean {
- return this.pendingNonStandardLines.length - pendingNonStandardLineLimit < this.pendingNonStandardLines.length
- }
- public processLine(line: string) {
- this.internalProcessLine(line, true, this.pendingNonStandardLines.length)
- }
- public getResult() {
- // If this is the final chunk, append any remaining original content
- if (this.isFinal && this.lastProcessedIndex < this.originalContent.length) {
- this.result += this.originalContent.slice(this.lastProcessedIndex)
- }
- if (this.isFinal && this.state !== ProcessingState.Idle) {
- throw new Error("File processing incomplete - SEARCH/REPLACE operations still active during finalization")
- }
- return this.result
- }
- private internalProcessLine(
- line: string,
- canWritependingNonStandardLines: boolean,
- pendingNonStandardLineLimit: number,
- ): number {
- let removeLineCount = 0
- if (isSearchBlockStart(line)) {
- removeLineCount = this.trimPendingNonStandardTrailingEmptyLines(pendingNonStandardLineLimit)
- if (removeLineCount > 0) {
- pendingNonStandardLineLimit = pendingNonStandardLineLimit - removeLineCount
- }
- if (this.hasPendingNonStandardLines(pendingNonStandardLineLimit)) {
- this.tryFixSearchReplaceBlock(pendingNonStandardLineLimit)
- canWritependingNonStandardLines && (this.pendingNonStandardLines.length = 0)
- }
- this.activateSearchState()
- } else if (isSearchBlockEnd(line)) {
- // 校验非标内容
- if (!this.isSearchingActive()) {
- this.tryFixSearchBlock(pendingNonStandardLineLimit)
- canWritependingNonStandardLines && (this.pendingNonStandardLines.length = 0)
- }
- this.activateReplaceState()
- this.beforeReplace()
- } else if (isReplaceBlockEnd(line)) {
- if (!this.isReplacingActive()) {
- this.tryFixReplaceBlock(pendingNonStandardLineLimit)
- canWritependingNonStandardLines && (this.pendingNonStandardLines.length = 0)
- }
- this.lastProcessedIndex = this.searchEndIndex
- this.resetForNextBlock()
- } else {
- // Accumulate content for search or replace
- // (currentReplaceContent is not being used for anything right now since we directly append to result.)
- // (We artificially add a linebreak since we split on \n at the beginning. In order to not include a trailing linebreak in the final search/result blocks we need to remove it before using them. This allows for partial line matches to be correctly identified.)
- // NOTE: search/replace blocks must be arranged in the order they appear in the file due to how we build the content using lastProcessedIndex. We also cannot strip the trailing newline since for non-partial lines it would remove the linebreak from the original content. (If we remove end linebreak from search, then we'd also have to remove it from replace but we can't know if it's a partial line or not since the model may be using the line break to indicate the end of the block rather than as part of the search content.) We require the model to output full lines in order for our fallbacks to work as well.
- if (this.isReplacingActive()) {
- this.currentReplaceContent += line + "\n"
- // Output replacement lines immediately if we know the insertion point
- if (this.searchMatchIndex !== -1) {
- this.result += line + "\n"
- }
- } else if (this.isSearchingActive()) {
- this.currentSearchContent += line + "\n"
- } else {
- let appendToPendingNonStandardLines = canWritependingNonStandardLines
- if (appendToPendingNonStandardLines) {
- // 处理非标内容
- this.pendingNonStandardLines.push(line)
- }
- }
- }
- return removeLineCount
- }
- private beforeReplace() {
- // Remove trailing linebreak for adding the === marker
- // if (currentSearchContent.endsWith("\r\n")) {
- // currentSearchContent = currentSearchContent.slice(0, -2)
- // } else if (currentSearchContent.endsWith("\n")) {
- // currentSearchContent = currentSearchContent.slice(0, -1)
- // }
- if (!this.currentSearchContent) {
- // Empty search block
- if (this.originalContent.length === 0) {
- // New file scenario: nothing to match, just start inserting
- this.searchMatchIndex = 0
- this.searchEndIndex = 0
- } else {
- // Complete file replacement scenario: treat the entire file as matched
- this.searchMatchIndex = 0
- this.searchEndIndex = this.originalContent.length
- }
- } else {
- // Add check for inefficient full-file search
- // if (currentSearchContent.trim() === originalContent.trim()) {
- // throw new Error(
- // "The SEARCH block contains the entire file content. Please either:\n" +
- // "1. Use an empty SEARCH block to replace the entire file, or\n" +
- // "2. Make focused changes to specific parts of the file that need modification.",
- // )
- // }
- // Exact search match scenario
- const exactIndex = this.originalContent.indexOf(this.currentSearchContent, this.lastProcessedIndex)
- if (exactIndex !== -1) {
- this.searchMatchIndex = exactIndex
- this.searchEndIndex = exactIndex + this.currentSearchContent.length
- } else {
- // Attempt fallback line-trimmed matching
- const lineMatch = lineTrimmedFallbackMatch(
- this.originalContent,
- this.currentSearchContent,
- this.lastProcessedIndex,
- )
- if (lineMatch) {
- ;[this.searchMatchIndex, this.searchEndIndex] = lineMatch
- } else {
- // Try block anchor fallback for larger blocks
- const blockMatch = blockAnchorFallbackMatch(
- this.originalContent,
- this.currentSearchContent,
- this.lastProcessedIndex,
- )
- if (blockMatch) {
- ;[this.searchMatchIndex, this.searchEndIndex] = blockMatch
- } else {
- throw new Error(
- `The SEARCH block:\n${this.currentSearchContent.trimEnd()}\n...does not match anything in the file.`,
- )
- }
- }
- }
- }
- if (this.searchMatchIndex < this.lastProcessedIndex) {
- throw new Error(
- `The SEARCH block:\n${this.currentSearchContent.trimEnd()}\n...matched an incorrect content in the file.`,
- )
- }
- // Output everything up to the match location
- this.result += this.originalContent.slice(this.lastProcessedIndex, this.searchMatchIndex)
- }
- private tryFixSearchBlock(lineLimit: number): number {
- let removeLineCount = 0
- if (lineLimit < 0) {
- lineLimit = this.pendingNonStandardLines.length
- }
- if (!lineLimit) {
- throw new Error("Invalid SEARCH/REPLACE block structure - no lines available to process")
- }
- let searchTagRegexp = /^([-]{3,}|[<]{3,}) SEARCH$/
- const searchTagIndex = this.findLastMatchingLineIndex(searchTagRegexp, lineLimit)
- if (searchTagIndex !== -1) {
- let fixLines = this.pendingNonStandardLines.slice(searchTagIndex, lineLimit)
- fixLines[0] = SEARCH_BLOCK_START
- for (const line of fixLines) {
- removeLineCount += this.internalProcessLine(line, false, searchTagIndex)
- }
- } else {
- throw new Error(
- `Invalid REPLACE marker detected - could not find matching SEARCH block starting from line ${searchTagIndex + 1}`,
- )
- }
- return removeLineCount
- }
- private tryFixReplaceBlock(lineLimit: number): number {
- let removeLineCount = 0
- if (lineLimit < 0) {
- lineLimit = this.pendingNonStandardLines.length
- }
- if (!lineLimit) {
- throw new Error()
- }
- let replaceBeginTagRegexp = /^[=]{3,}$/
- const replaceBeginTagIndex = this.findLastMatchingLineIndex(replaceBeginTagRegexp, lineLimit)
- if (replaceBeginTagIndex !== -1) {
- // // 校验非标内容
- // if (!this.isSearchingActive()) {
- // removeLineCount += this.tryFixSearchBlock(replaceBeginTagIndex)
- // }
- let fixLines = this.pendingNonStandardLines.slice(replaceBeginTagIndex - removeLineCount, lineLimit - removeLineCount)
- fixLines[0] = SEARCH_BLOCK_END
- for (const line of fixLines) {
- removeLineCount += this.internalProcessLine(line, false, replaceBeginTagIndex - removeLineCount)
- }
- } else {
- throw new Error(`Malformed REPLACE block - missing valid separator after line ${replaceBeginTagIndex + 1}`)
- }
- return removeLineCount
- }
- private tryFixSearchReplaceBlock(lineLimit: number): number {
- let removeLineCount = 0
- if (lineLimit < 0) {
- lineLimit = this.pendingNonStandardLines.length
- }
- if (!lineLimit) {
- throw new Error()
- }
- let replaceEndTagRegexp = /^([+]{3,}|[>]{3,}) REPLACE$/
- const replaceEndTagIndex = this.findLastMatchingLineIndex(replaceEndTagRegexp, lineLimit)
- const likeReplaceEndTag = replaceEndTagIndex === lineLimit - 1
- if (likeReplaceEndTag) {
- // // 校验非标内容
- // if (!this.isReplacingActive()) {
- // removeLineCount += this.tryFixReplaceBlock(replaceEndTagIndex)
- // }
- let fixLines = this.pendingNonStandardLines.slice(replaceEndTagIndex - removeLineCount, lineLimit - removeLineCount)
- fixLines[fixLines.length - 1] = REPLACE_BLOCK_END
- for (const line of fixLines) {
- removeLineCount += this.internalProcessLine(line, false, replaceEndTagIndex - removeLineCount)
- }
- } else {
- throw new Error("Malformed SEARCH/REPLACE block structure: Missing valid closing REPLACE marker")
- }
- return removeLineCount
- }
- /**
- * Removes trailing empty lines from the pendingNonStandardLines array
- * @param lineLimit - The index to start checking from (exclusive).
- * Removes empty lines from lineLimit-1 backwards.
- * @returns The number of empty lines removed
- */
- private trimPendingNonStandardTrailingEmptyLines(lineLimit: number): number {
- let removedCount = 0
- let i = Math.min(lineLimit, this.pendingNonStandardLines.length) - 1
- while (i >= 0 && this.pendingNonStandardLines[i].trim() === "") {
- this.pendingNonStandardLines.pop()
- removedCount++
- i--
- }
- return removedCount
- }
- }
- export async function constructNewFileContentV2(diffContent: string, originalContent: string, isFinal: boolean): Promise<string> {
- let newFileContentConstructor = new NewFileContentConstructor(originalContent, isFinal)
- let lines = diffContent.split("\n")
- // If the last line looks like a partial marker but isn't recognized,
- // remove it because it might be incomplete.
- const lastLine = lines[lines.length - 1]
- if (
- lines.length > 0 &&
- (lastLine.startsWith(SEARCH_BLOCK_CHAR) ||
- lastLine.startsWith(LEGACY_SEARCH_BLOCK_CHAR) ||
- lastLine.startsWith("=") ||
- lastLine.startsWith(REPLACE_BLOCK_CHAR) ||
- lastLine.startsWith(LEGACY_REPLACE_BLOCK_CHAR)) &&
- lastLine !== SEARCH_BLOCK_START &&
- lastLine !== SEARCH_BLOCK_END &&
- lastLine !== REPLACE_BLOCK_END
- ) {
- lines.pop()
- }
- for (const line of lines) {
- newFileContentConstructor.processLine(line)
- }
- let result = newFileContentConstructor.getResult()
- return result
- }
|