Skip to content

Commit 053c43c

Browse files
committed
Add support for parsing new files based on empty files in diff parser
- add text case for 'new file based on empty file' - use '==' or '!=' instead of '===' or '!==' when comparing to string and applicable └── apps └── editor └── src └── commands └── apply-response-command └── utils └── response-parser └── parsers └── diff-parser ├── diff-parser.ts ├── helpers │ ├── block-utils.ts │ ├── index.ts │ ├── patch-utils.ts │ └── path-utils.ts ├── index.ts └── test ├── cases │ ├── deletion-via-header │ │ ├── 1-file.txt │ │ ├── 2-file.txt │ │ └── deletion-via-header.txt │ ├── deletion-via-redundant-header │ │ ├── 1-file.txt │ │ └── deletion-via-redundant-header.txt │ ├── file-deletion │ │ ├── 1-file.txt │ │ └── file-deletion.txt │ ├── git-and-hunk-header-same-line │ │ ├── 1-file.txt │ │ └── git-and-hunk-header-same-line.txt │ ├── git-broken-hunk-header │ │ ├── 1-file.txt │ │ └── git-broken-hunk-header.txt │ ├── git-two-header-types │ │ ├── 1-file.txt │ │ └── git-two-header-types.txt │ ├── ignore-ellipsis │ │ ├── 1-file.txt │ │ ├── 2-file.txt │ │ └── ignore-ellipsis.txt │ ├── inner-backticks │ │ ├── 1-file.txt │ │ └── inner-backticks.txt │ ├── inner-triple-backticks │ │ ├── 1-file.txt │ │ └── inner-triple-backticks.txt │ ├── markdown-missing-ending │ │ ├── 1-file.txt │ │ └── markdown-missing-ending.txt │ ├── markdown-path-above │ │ ├── 1-file.txt │ │ └── markdown-path-above.txt │ ├── markdown-path-above-no-hunk-header │ │ ├── 1-file.txt │ │ └── markdown-path-above-no-hunk-header.txt │ ├── markdown-path-above-with-text-containing-path │ │ ├── 1-text.txt │ │ ├── 2-file.txt │ │ └── markdown-path-above-with-text-containing-path.txt │ ├── markdown-redundant-closing-backticks │ │ ├── 1-file.txt │ │ └── markdown-redundant-closing-backticks.txt │ ├── markdown-repeated-path-above │ │ ├── 1-text.txt │ │ ├── 2-file.txt │ │ └── markdown-repeated-path-above.txt │ ├── markdown-unnecessary-empty-lines │ │ ├── 1-file.txt │ │ └── markdown-path-above.txt │ ├── mix-delete-and-new-file │ │ ├── 1-file.txt │ │ ├── 2-file.txt │ │ └── mix-delete-and-new-file.txt │ ├── mix-new-file-from-heading-and-diff │ │ ├── 1-text.txt │ │ ├── 2-file.txt │ │ ├── 3-file.txt │ │ └── mix-new-file-from-heading-and-diff.txt │ ├── mix-new-file-from-xml-tag-heading-and-diff │ │ ├── 1-text.txt │ │ ├── 2-file.txt │ │ ├── 3-file.txt │ │ └── mix-new-file-from-xml-tag-heading-and-diff.txt │ ├── mix-new-file-xml-and-diff-variant-1 │ │ ├── 1-file.txt │ │ ├── 2-file.txt │ │ └── mix-new-file-xml-and-diff-variant-1.txt │ ├── mix-new-file-xml-and-diff-variant-2 │ │ ├── 1-file.txt │ │ ├── 2-file.txt │ │ └── mix-new-file-xml-and-diff-variant-2.txt │ ├── mix-rename-and-update │ │ ├── 1-file.txt │ │ └── mix-rename-and-update.txt │ ├── mix-rename-one-liner-and-diff │ │ ├── 1-file.txt │ │ ├── 2-file.txt │ │ └── mix-rename-one-liner-and-diff.txt │ ├── multiple-edits-for-the-same-file │ │ ├── 1-text.txt │ │ ├── 2-file.txt │ │ └── multiple-edits-for-the-same-file.txt │ ├── multiple-files-git-and-hunk-header-same-line │ │ ├── 1-file.txt │ │ ├── 2-file.txt │ │ └── multiple-files-git-and-hunk-header-same-line.txt │ ├── multiple-files-no-markdown-block │ │ ├── 1-file.txt │ │ ├── 2-file.txt │ │ └── multiple-files-no-markdown-block.txt │ ├── multiple-files-no-prefix-single-markdown-block │ │ ├── 1-file.txt │ │ ├── 2-file.txt │ │ └── multiple-files-no-prefix-single-markdown-block.txt │ ├── multiple-files-same-line-backticks │ │ ├── 1-file.txt │ │ ├── 2-file.txt │ │ └── multiple-files-same-line-backticks.txt │ ├── multiple-files-separate-markdown-blocks │ │ ├── 1-file.txt │ │ ├── 2-file.txt │ │ └── multiple-files-separate-markdown-blocks.txt │ ├── multiple-files-single-markdown-block │ │ ├── 1-file.txt │ │ ├── 2-file.txt │ │ └── multiple-files-single-markdown-block.txt │ ├── multiple-files-text-between │ │ ├── 1-text.txt │ │ ├── 2-file.txt │ │ ├── 3-text.txt │ │ ├── 4-file.txt │ │ ├── 5-text.txt │ │ └── multiple-files-text-between.txt │ ├── multiple-files-with-git-headers-no-file-lines │ │ ├── 1-file.txt │ │ ├── 2-file.txt │ │ └── multiple-files-with-git-headers-no-file-lines.txt │ ├── multiple-files-with-git-headers-separate-markdown-blocks │ │ ├── 1-file.txt │ │ ├── 2-file.txt │ │ └── multiple-files-with-git-headers-separate-markdown-blocks.txt │ ├── multiple-files-with-quoted-paths │ │ ├── 1-file.txt │ │ ├── 2-file.txt │ │ └── multiple-files-with-quoted-paths.txt │ ├── multiple-files-with-timestamps │ │ ├── 1-file.txt │ │ ├── 2-file.txt │ │ └── multiple-files-with-timestamps.txt │ ├── multiple-new-files-separate-markdown-blocks │ │ ├── 1-file.txt │ │ ├── 2-file.txt │ │ └── multiple-new-files-separate-markdown-blocks.txt │ ├── new-file-based-on-empty-file │ │ ├── 1-file.txt │ │ └── new-file-based-on-empty-file.txt │ ├── new-file-with-git-header │ │ ├── 1-file.txt │ │ └── new-file-with-git-header.txt │ ├── no-markdown-or-git-header │ │ ├── 1-file.txt │ │ └── no-markdown-or-git-header.txt │ ├── no-prefix │ │ ├── 1-file.txt │ │ └── no-prefix.txt │ ├── non-diff-code-blocks │ │ ├── 1-text.txt │ │ ├── 2-inline-file.txt │ │ ├── 3-file.txt │ │ └── non-diff-code-blocks.txt │ ├── rename │ │ ├── 1-file.txt │ │ └── rename.txt │ ├── with-empty-lines-between │ │ ├── 1-file.txt │ │ └── with-empty-lines-between.txt │ ├── with-git-header-no-file-lines │ │ ├── 1-file.txt │ │ └── with-git-header-no-file-lines.txt │ ├── with-git-header-no-markdown │ │ ├── 1-file.txt │ │ └── with-git-header-no-markdown.txt │ ├── with-quoted-paths │ │ ├── 1-file.txt │ │ └── with-quoted-paths.txt │ ├── with-timestamps │ │ ├── 1-file.txt │ │ └── with-timestamps.txt │ ├── with-xml-file-path │ │ ├── 1-file.txt │ │ └── with-xml-file-path.txt │ ├── within-xml-tags │ │ ├── 1-file.txt │ │ └── within-xml-tags.txt │ ├── within-xml-tags-unquoted-path │ │ ├── 1-file.txt │ │ └── within-xml-tags-unquoted-path.txt │ └── within-xml-tags-with-cdata │ ├── 1-file.txt │ └── within-xml-tags-with-cdata.txt └── diff-parser.spec.ts
1 parent 8438973 commit 053c43c

5 files changed

Lines changed: 56 additions & 7 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ Install the [browser extension](https://github.com/robertpiosik/CodeWebChat/tree
186186

187187
- `Commit Changes` - Generate commit message in your preferred style and commit.
188188
- `Apply Commit Message from Clipboard` - Trigger `Commit Changes` flow with the copied text.
189-
- `Copy Squashed Commit Details` - Copy squashed prompts and context ASCII trees between HEAD and a selected branch.
189+
- `Copy Squashed Commit Details` - Copy consolidated prompts and context ASCII trees of all commits between HEAD and a selected branch.
190190

191191
### History
192192

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

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ const process_collected_patch_lines = (params: {
9595
is_single_root_folder_workspace: params.is_single_root
9696
})
9797
patch.new_file_path = new_relative
98-
if (new_workspace && new_workspace !== workspace_name) {
98+
if (new_workspace && new_workspace != workspace_name) {
9999
patch.new_workspace_name = new_workspace
100100
}
101101
}
@@ -307,7 +307,7 @@ const process_text_for_file_operations = (params: {
307307
}
308308
if (
309309
new_info.workspace_name &&
310-
new_info.workspace_name !== old_info.workspace_name
310+
new_info.workspace_name != old_info.workspace_name
311311
) {
312312
patch.new_workspace_name = new_info.workspace_name
313313
}
@@ -705,7 +705,7 @@ const merge_diffs_per_file = (items: DiffParserItem[]): DiffParserItem[] => {
705705
)
706706

707707
const existing_is_new_file = existing_lines.some(
708-
(l) => l === '--- /dev/null'
708+
(l) => l == '--- /dev/null'
709709
)
710710
const incoming_is_modification =
711711
!item.content.includes('--- /dev/null') && item.content.includes('@@')
@@ -817,10 +817,33 @@ const build_patch_content = (params: {
817817
let to_line: string | undefined
818818
let diff_git_line: string | undefined
819819

820+
const is_new_file_based_on_empty =
821+
formatted_body_lines.length > 0 &&
822+
formatted_body_lines.some((line) => line.startsWith('+')) &&
823+
formatted_body_lines.some((line) => line.trim() == '-') &&
824+
formatted_body_lines.every(
825+
(line) =>
826+
line.startsWith('+') ||
827+
line.startsWith('@@') ||
828+
line.trim() == '-' ||
829+
line == '\\ No newline at end of file'
830+
) &&
831+
formatted_body_lines.some(
832+
(line) => line.startsWith('@@ -1') || line.startsWith('@@ -0')
833+
)
834+
820835
for (const line of header_lines) {
821-
if (line.startsWith('--- ')) from_line = line
822-
if (line.startsWith('+++ ')) to_line = line
823-
if (line.startsWith('diff --git ')) diff_git_line = line
836+
if (line.startsWith('--- ')) {
837+
from_line = is_new_file_based_on_empty ? '--- /dev/null' : line
838+
} else if (line.startsWith('+++ ')) {
839+
to_line = line
840+
} else if (line.startsWith('diff --git ')) {
841+
diff_git_line = line
842+
}
843+
}
844+
845+
if (is_new_file_based_on_empty && !from_line) {
846+
from_line = '--- /dev/null'
824847
}
825848

826849
const final_header: string[] = []
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
--- /dev/null
2+
+++ b/src/index.ts
3+
@@ -1,2 +1,2 @@
4+
-
5+
+console.log("new message")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
--- a/src/index.ts
2+
+++ b/src/index.ts
3+
@@ -1,2 +1,2 @@
4+
-
5+
+console.log("new message")

‎apps/editor/src/commands/apply-response-command/utils/response-parser/parsers/diff-parser/test/diff-parser.spec.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,4 +945,20 @@ describe('diff-parser', () => {
945945
content: load_test_case_file(test_case, '2-file.txt')
946946
})
947947
})
948+
949+
it('parses a new file based on an empty file', () => {
950+
const test_case = 'new-file-based-on-empty-file'
951+
const text = load_test_case_file(test_case, `${test_case}.txt`)
952+
const result = parse_response({
953+
response: text,
954+
is_single_root_folder_workspace: true
955+
})
956+
957+
expect(result).toHaveLength(1)
958+
expect(result[0]).toMatchObject({
959+
type: 'diff',
960+
file_path: 'src/index.ts',
961+
content: load_test_case_file(test_case, '1-file.txt')
962+
})
963+
})
948964
})

0 commit comments

Comments
 (0)