|
1 | 1 | import { Logger } from '@shared/utils/logger' |
2 | 2 | import { |
| 3 | + TextItem, |
3 | 4 | DiffItem, |
4 | 5 | InlineFileItem, |
5 | | - TextItem, |
6 | 6 | extract_workspace_and_path |
7 | 7 | } from '../../clipboard-parser' |
8 | 8 | import { |
@@ -729,22 +729,65 @@ const parse_multiple_raw_patches = (params: { |
729 | 729 | return patches |
730 | 730 | } |
731 | 731 |
|
732 | | -const filter_last_diff_per_file = ( |
733 | | - items: DiffParserItem[] |
734 | | -): DiffParserItem[] => { |
735 | | - const seen_files = new Set<string>() |
| 732 | +const merge_diffs_per_file = (items: DiffParserItem[]): DiffParserItem[] => { |
736 | 733 | const result: DiffParserItem[] = [] |
| 734 | + const file_diff_map = new Map<string, DiffItem>() |
737 | 735 |
|
738 | | - for (let i = items.length - 1; i >= 0; i--) { |
739 | | - const item = items[i] |
| 736 | + for (const item of items) { |
740 | 737 | if (item.type == 'diff') { |
741 | 738 | const file_key = `${item.workspace_name || ''}:${item.file_path}` |
742 | | - if (!seen_files.has(file_key)) { |
743 | | - seen_files.add(file_key) |
744 | | - result.unshift(item) |
| 739 | + if (file_diff_map.has(file_key)) { |
| 740 | + const existing_diff = file_diff_map.get(file_key)! |
| 741 | + const existing_lines = existing_diff.content |
| 742 | + .replace(/\n$/, '') |
| 743 | + .split('\n') |
| 744 | + const existing_has_real_hunks = existing_lines.some( |
| 745 | + (l) => |
| 746 | + (l.startsWith('+') && !l.startsWith('+++ ')) || |
| 747 | + (l.startsWith('-') && !l.startsWith('--- ')) || |
| 748 | + (l.startsWith(' ') && !l.startsWith(' ')) |
| 749 | + ) |
| 750 | + |
| 751 | + if (!existing_has_real_hunks) { |
| 752 | + existing_diff.content = item.content |
| 753 | + } else { |
| 754 | + const new_lines = item.content.replace(/\n$/, '').split('\n') |
| 755 | + |
| 756 | + let start_idx = new_lines.findIndex((l) => l.startsWith('@@')) |
| 757 | + if (start_idx == -1) { |
| 758 | + start_idx = new_lines.findIndex( |
| 759 | + (l) => |
| 760 | + !l.startsWith('--- ') && |
| 761 | + !l.startsWith('+++ ') && |
| 762 | + !l.startsWith('diff --git') && |
| 763 | + l.trim() != '' |
| 764 | + ) |
| 765 | + } |
| 766 | + |
| 767 | + if (start_idx != -1) { |
| 768 | + const content_to_add = new_lines.slice(start_idx).join('\n') + '\n' |
| 769 | + existing_diff.content = existing_diff.content.endsWith('\n') |
| 770 | + ? existing_diff.content + content_to_add |
| 771 | + : existing_diff.content + '\n' + content_to_add |
| 772 | + } |
| 773 | + } |
| 774 | + |
| 775 | + if (result.length > 0 && result[result.length - 1].type == 'text') { |
| 776 | + const orphaned_text = result.pop() as TextItem |
| 777 | + const diff_index = result.indexOf(existing_diff) |
| 778 | + if (diff_index > 0 && result[diff_index - 1].type == 'text') { |
| 779 | + ;(result[diff_index - 1] as TextItem).content += |
| 780 | + '\n' + orphaned_text.content |
| 781 | + } else { |
| 782 | + result.splice(diff_index, 0, orphaned_text) |
| 783 | + } |
| 784 | + } |
| 785 | + } else { |
| 786 | + file_diff_map.set(file_key, item) |
| 787 | + result.push(item) |
745 | 788 | } |
746 | 789 | } else { |
747 | | - result.unshift(item) |
| 790 | + result.push(item) |
748 | 791 | } |
749 | 792 | } |
750 | 793 | return result |
@@ -780,7 +823,7 @@ export const extract_diffs = (params: { |
780 | 823 | is_single_root: params.is_single_root |
781 | 824 | }) |
782 | 825 | } |
783 | | - return filter_last_diff_per_file(items) |
| 826 | + return merge_diffs_per_file(items) |
784 | 827 | } |
785 | 828 |
|
786 | 829 | export { extract_paths_from_lines } |
|
0 commit comments