-
Notifications
You must be signed in to change notification settings - Fork 2.6k
More tolerant search/replace match #6537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -259,7 +259,10 @@ Each file requires its own path, start_line, and diff elements. | |
|
|
||
| const state = { current: State.START, line: 0 } | ||
|
|
||
| const SEARCH = "<<<<<<< SEARCH" | ||
| // Pattern allows optional '>' after SEARCH to handle AI-generated diffs | ||
| // (e.g., Sonnet 4 sometimes adds an extra '>') | ||
| const SEARCH_PATTERN = /^<<<<<<< SEARCH>?$/ | ||
| const SEARCH = SEARCH_PATTERN.source.replace(/[\^$]/g, "") // Remove regex anchors for display | ||
| const SEP = "=======" | ||
| const REPLACE = ">>>>>>> REPLACE" | ||
| const SEARCH_PREFIX = "<<<<<<< " | ||
|
|
@@ -329,7 +332,7 @@ Each file requires its own path, start_line, and diff elements. | |
| }) | ||
|
|
||
| const lines = diffContent.split("\n") | ||
| const searchCount = lines.filter((l) => l.trim() === SEARCH).length | ||
| const searchCount = lines.filter((l) => SEARCH_PATTERN.test(l.trim())).length | ||
| const sepCount = lines.filter((l) => l.trim() === SEP).length | ||
| const replaceCount = lines.filter((l) => l.trim() === REPLACE).length | ||
|
|
||
|
|
@@ -357,20 +360,20 @@ Each file requires its own path, start_line, and diff elements. | |
| : reportMergeConflictError(SEP, SEARCH) | ||
| if (marker === REPLACE) return reportInvalidDiffError(REPLACE, SEARCH) | ||
| if (marker.startsWith(REPLACE_PREFIX)) return reportMergeConflictError(marker, SEARCH) | ||
| if (marker === SEARCH) state.current = State.AFTER_SEARCH | ||
| if (SEARCH_PATTERN.test(marker)) state.current = State.AFTER_SEARCH | ||
| else if (marker.startsWith(SEARCH_PREFIX)) return reportMergeConflictError(marker, SEARCH) | ||
| break | ||
|
|
||
| case State.AFTER_SEARCH: | ||
| if (marker === SEARCH) return reportInvalidDiffError(SEARCH, SEP) | ||
| if (SEARCH_PATTERN.test(marker)) return reportInvalidDiffError(SEARCH_PATTERN.source, SEP) | ||
| if (marker.startsWith(SEARCH_PREFIX)) return reportMergeConflictError(marker, SEARCH) | ||
| if (marker === REPLACE) return reportInvalidDiffError(REPLACE, SEP) | ||
| if (marker.startsWith(REPLACE_PREFIX)) return reportMergeConflictError(marker, SEARCH) | ||
| if (marker === SEP) state.current = State.AFTER_SEPARATOR | ||
| break | ||
|
|
||
| case State.AFTER_SEPARATOR: | ||
| if (marker === SEARCH) return reportInvalidDiffError(SEARCH, REPLACE) | ||
| if (SEARCH_PATTERN.test(marker)) return reportInvalidDiffError(SEARCH_PATTERN.source, REPLACE) | ||
| if (marker.startsWith(SEARCH_PREFIX)) return reportMergeConflictError(marker, REPLACE) | ||
| if (marker === SEP) | ||
| return likelyBadStructure | ||
|
|
@@ -456,7 +459,7 @@ Each file requires its own path, start_line, and diff elements. | |
|
|
||
| /* Regex parts: | ||
| 1. (?:^|\n) Ensures the first marker starts at the beginning of the file or right after a newline. | ||
| 2. (?<!\\)<<<<<<< SEARCH\s*\n Matches the line "<<<<<<< SEARCH" (ignoring any trailing spaces) – the negative lookbehind makes sure it isn't escaped. | ||
| 2. (?<!\\)<<<<<<< SEARCH>?\s*\n Matches the line "<<<<<<< SEARCH" with optional '>' (ignoring any trailing spaces) – the negative lookbehind makes sure it isn't escaped. | ||
| 3. ((?:\:start_line:\s*(\d+)\s*\n))? Optionally matches a ":start_line:" line. The outer capturing group is group 1 and the inner (\d+) is group 2. | ||
| 4. ((?:\:end_line:\s*(\d+)\s*\n))? Optionally matches a ":end_line:" line. Group 3 is the whole match and group 4 is the digits. | ||
| 5. ((?<!\\)-------\s*\n)? Optionally matches the "-------" marker line (group 5). | ||
|
|
@@ -467,7 +470,7 @@ Each file requires its own path, start_line, and diff elements. | |
| */ | ||
| let matches = [ | ||
| ...diffContent.matchAll( | ||
| /(?:^|\n)(?<!\\)<<<<<<< SEARCH\s*\n((?:\:start_line:\s*(\d+)\s*\n))?((?:\:end_line:\s*(\d+)\s*\n))?((?<!\\)-------\s*\n)?([\s\S]*?)(?:\n)?(?:(?<=\n)(?<!\\)=======\s*\n)([\s\S]*?)(?:\n)?(?:(?<=\n)(?<!\\)>>>>>>> REPLACE)(?=\n|$)/g, | ||
| /(?:^|\n)(?<!\\)<<<<<<< SEARCH>?\s*\n((?:\:start_line:\s*(\d+)\s*\n))?((?:\:end_line:\s*(\d+)\s*\n))?((?<!\\)-------\s*\n)?([\s\S]*?)(?:\n)?(?:(?<=\n)(?<!\\)=======\s*\n)([\s\S]*?)(?:\n)?(?:(?<=\n)(?<!\\)>>>>>>> REPLACE)(?=\n|$)/g, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The detailed regex comment here should be updated to mention the optional
Could we update it to:
|
||
| ), | ||
| ] | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.