Skip to content

Commit 8cd69d5

Browse files
committed
Support diff parsing for both unified diff format and raw git diff output
1 parent 40d3d93 commit 8cd69d5

2 files changed

Lines changed: 28 additions & 9 deletions

File tree

apps/editor/src/commands/apply-chat-response-command/utils/clipboard-parser/parsers/diff-parser/diff-parser.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,15 @@ const convert_code_block_to_new_file_diff = (params: {
176176
} else {
177177
for (let i = 0; i < Math.min(params.lines.length, 5); i++) {
178178
const line = params.lines[i].trim()
179+
180+
if (
181+
line.startsWith('--- ') ||
182+
line.startsWith('+++ ') ||
183+
line.startsWith('diff --git ')
184+
) {
185+
continue
186+
}
187+
179188
const match = line.match(path_regex)
180189

181190
if (match && match[1]) {
@@ -217,22 +226,32 @@ const convert_code_block_to_new_file_diff = (params: {
217226
const first_real_content_line = content_lines.find((l) => l.trim() != '')
218227
if (
219228
first_real_content_line &&
220-
first_real_content_line
229+
(first_real_content_line
221230
.trim()
222-
.match(/^(@@ -\d+(?:,\d+)? \+\d+(?:,\d+)? @@)/)
231+
.match(/^(@@ -\d+(?:,\d+)? \+\d+(?:,\d+)? @@)/) ||
232+
first_real_content_line.startsWith('diff --git ') ||
233+
first_real_content_line.startsWith('--- '))
223234
) {
224235
const { workspace_name, relative_path } = extract_workspace_and_path({
225236
raw_file_path: file_path,
226237
is_single_root_folder_workspace: params.is_single_root
227238
})
228239

229-
const is_new_file = first_real_content_line.trim().startsWith('@@ -0,0')
240+
let patch_content = ''
241+
if (
242+
first_real_content_line.startsWith('diff --git ') ||
243+
first_real_content_line.startsWith('--- ')
244+
) {
245+
patch_content = content_lines.join('\n')
246+
} else {
247+
const is_new_file = first_real_content_line.trim().startsWith('@@ -0,0')
230248

231-
const patch_content = [
232-
is_new_file ? '--- /dev/null' : `--- a/${relative_path}`,
233-
`+++ b/${relative_path}`,
234-
...content_lines
235-
].join('\n')
249+
patch_content = [
250+
is_new_file ? '--- /dev/null' : `--- a/${relative_path}`,
251+
`+++ b/${relative_path}`,
252+
...content_lines
253+
].join('\n')
254+
}
236255

237256
return {
238257
type: 'diff',

apps/editor/src/commands/apply-chat-response-command/utils/clipboard-parser/test-cases/diffs/diff-markdown-repeated-path-above/diff-markdown-repeated-path-above.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pnpm add -D prettier
77

88
Lorem ipsum.
99

10-
```diff
10+
```typescript
1111
--- a/src/index.ts
1212
+++ b/src/index.ts
1313
@@ -1,3 +1,3 @@

0 commit comments

Comments
 (0)