Skip to content

Commit 3af00ca

Browse files
committed
Support staging and displaying file status in commit flow with visual indicators and handle empty content file deletions
1 parent 5665e82 commit 3af00ca

15 files changed

Lines changed: 217 additions & 63 deletions

File tree

‎packages/ui/src/components/editor/panel/modals/StageFilesModal/StageFilesModal.module.scss‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@
2929
white-space: nowrap;
3030
text-overflow: ellipsis;
3131

32+
&--new {
33+
color: var(--vscode-gitDecoration-untrackedResourceForeground);
34+
}
35+
36+
&--modified {
37+
color: var(--vscode-gitDecoration-modifiedResourceForeground);
38+
}
39+
40+
&--deleted {
41+
text-decoration: line-through;
42+
color: var(--vscode-gitDecoration-deletedResourceForeground);
43+
}
44+
3245
> strong {
3346
font-weight: 500;
3447
}

‎packages/ui/src/components/editor/panel/modals/StageFilesModal/StageFilesModal.tsx‎

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,23 @@ import { Checkbox } from '../../../common/Checkbox'
66
import { IconButton } from '../../IconButton/IconButton'
77
import cn from 'classnames'
88

9+
const UNTRACKED_STATUS = 7
10+
const DELETED_STATUS = 6
11+
const MODIFIED_STATUS = 5
12+
913
type Props = {
10-
files: string[]
14+
files: { path: string; status: number }[]
1115
on_stage: (files_to_stage: string[]) => void
1216
on_cancel: () => void
1317
on_go_to_file: (file: string) => void
1418
on_show_diff: (file: string) => void
1519
}
1620

1721
export const StageFilesModal: React.FC<Props> = (props) => {
18-
const [selected_files, set_selected_files] = useState<string[]>(props.files)
22+
console.log('xxx', props.files)
23+
const [selected_files, set_selected_files] = useState<string[]>(
24+
props.files.map((f) => f.path)
25+
)
1926

2027
const handle_toggle_file = (file: string, is_checked: boolean) => {
2128
if (is_checked) {
@@ -27,7 +34,7 @@ export const StageFilesModal: React.FC<Props> = (props) => {
2734

2835
const handle_toggle_select_all = (is_checked: boolean) => {
2936
if (is_checked) {
30-
set_selected_files(props.files)
37+
set_selected_files(props.files.map((f) => f.path))
3138
} else {
3239
set_selected_files([])
3340
}
@@ -59,7 +66,8 @@ export const StageFilesModal: React.FC<Props> = (props) => {
5966
/>
6067
<div className={styles.files__item__details}>Select all</div>
6168
</label>
62-
{props.files.map((file) => {
69+
{props.files.map((file_item) => {
70+
const file = file_item.path
6371
const last_slash_index = file.lastIndexOf('/')
6472
const filename =
6573
last_slash_index == -1
@@ -76,29 +84,44 @@ export const StageFilesModal: React.FC<Props> = (props) => {
7684
handle_toggle_file(file, is_checked)
7785
}
7886
/>
79-
<div className={styles.files__item__details} title={file}>
87+
<div
88+
className={cn(styles.files__item__details, {
89+
[styles['files__item__details--new']]:
90+
file_item.status == UNTRACKED_STATUS,
91+
[styles['files__item__details--modified']]:
92+
file_item.status == MODIFIED_STATUS,
93+
[styles['files__item__details--deleted']]:
94+
file_item.status == DELETED_STATUS
95+
})}
96+
title={file}
97+
>
8098
<span>{filename}</span>
8199
{dir_path && (
82100
<span className={styles.files__item__path}>{dir_path}</span>
83101
)}
84102
</div>
85103
<div className={styles.files__item__actions}>
86-
<IconButton
87-
codicon_icon="diff-single"
88-
title="Show Diff"
89-
on_click={(e) => {
90-
e.stopPropagation()
91-
props.on_show_diff(file)
92-
}}
93-
/>
94-
<IconButton
95-
codicon_icon="go-to-file"
96-
title="Go To File"
97-
on_click={(e) => {
98-
e.stopPropagation()
99-
props.on_go_to_file(file)
100-
}}
101-
/>
104+
{file_item.status != UNTRACKED_STATUS &&
105+
file_item.status != DELETED_STATUS && (
106+
<IconButton
107+
codicon_icon="diff-single"
108+
title="Show Diff"
109+
on_click={(e) => {
110+
e.stopPropagation()
111+
props.on_show_diff(file)
112+
}}
113+
/>
114+
)}
115+
{file_item.status != DELETED_STATUS && (
116+
<IconButton
117+
codicon_icon="go-to-file"
118+
title="Go To File"
119+
on_click={(e) => {
120+
e.stopPropagation()
121+
props.on_go_to_file(file)
122+
}}
123+
/>
124+
)}
102125
</div>
103126
</label>
104127
)

‎packages/vscode/src/commands/apply-chat-response-command/handlers/fast-replace-handler.ts‎

Lines changed: 76 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { dictionary } from '@shared/constants/dictionary'
66
import { create_safe_path, sanitize_file_name } from '@/utils/path-sanitizer'
77
import { FileItem } from '../utils/clipboard-parser'
88
import { OriginalFileState } from '@/commands/apply-chat-response-command/types/original-file-state'
9+
import { remove_directory_if_empty } from '../utils/file-operations'
910

1011
export const handle_fast_replace = async (
1112
files: FileItem[]
@@ -102,46 +103,89 @@ export const handle_fast_replace = async (
102103

103104
try {
104105
if (file_exists) {
105-
const file_uri = vscode.Uri.file(safe_path)
106-
const document = await vscode.workspace.openTextDocument(file_uri)
107-
const original_content = document.getText()
108-
original_states.push({
109-
file_path: file.file_path,
110-
content: original_content,
111-
is_new: false,
112-
workspace_name: file.workspace_name
113-
})
114-
const editor = await vscode.window.showTextDocument(document)
115-
let final_content = file.content
116-
if (
117-
original_content.endsWith('\n') &&
118-
!final_content.endsWith('\n')
119-
) {
120-
final_content += '\n'
121-
}
122-
await editor.edit((edit) => {
123-
edit.replace(
124-
new vscode.Range(
125-
document.positionAt(0),
126-
document.positionAt(document.getText().length)
127-
),
128-
final_content
129-
)
130-
})
106+
if (file.content == '') {
107+
const file_uri = vscode.Uri.file(safe_path)
108+
const document = await vscode.workspace.openTextDocument(file_uri)
109+
const original_content = document.getText()
110+
original_states.push({
111+
file_path: file.file_path,
112+
content: original_content,
113+
is_new: false,
114+
workspace_name: file.workspace_name,
115+
is_deleted: true
116+
})
131117

132-
await document.save()
133-
Logger.info({
134-
function_name: 'handle_fast_replace',
135-
message: 'Existing file replaced and saved',
136-
data: safe_path
137-
})
118+
const tabs_to_close: vscode.Tab[] = []
119+
for (const tab_group of vscode.window.tabGroups.all) {
120+
tabs_to_close.push(
121+
...tab_group.tabs.filter((tab) => {
122+
const tab_uri = (tab.input as any)?.uri as
123+
| vscode.Uri
124+
| undefined
125+
return tab_uri && tab_uri.fsPath === safe_path
126+
})
127+
)
128+
}
129+
130+
if (tabs_to_close.length > 0) {
131+
await vscode.window.tabGroups.close(tabs_to_close)
132+
}
133+
134+
fs.unlinkSync(safe_path)
135+
Logger.info({
136+
function_name: 'handle_fast_replace',
137+
message: 'File deleted due to empty content',
138+
data: safe_path
139+
})
140+
await remove_directory_if_empty({
141+
dir_path: path.dirname(safe_path),
142+
workspace_root
143+
})
144+
} else {
145+
const file_uri = vscode.Uri.file(safe_path)
146+
const document = await vscode.workspace.openTextDocument(file_uri)
147+
const original_content = document.getText()
148+
original_states.push({
149+
file_path: file.file_path,
150+
content: original_content,
151+
is_new: false,
152+
workspace_name: file.workspace_name
153+
})
154+
const editor = await vscode.window.showTextDocument(document)
155+
let final_content = file.content
156+
if (
157+
original_content.endsWith('\n') &&
158+
!final_content.endsWith('\n')
159+
) {
160+
final_content += '\n'
161+
}
162+
await editor.edit((edit) => {
163+
edit.replace(
164+
new vscode.Range(
165+
document.positionAt(0),
166+
document.positionAt(document.getText().length)
167+
),
168+
final_content
169+
)
170+
})
171+
172+
await document.save()
173+
Logger.info({
174+
function_name: 'handle_fast_replace',
175+
message: 'Existing file replaced and saved',
176+
data: safe_path
177+
})
178+
}
138179
} else {
139180
original_states.push({
140181
file_path: file.file_path,
141182
content: '',
142183
is_new: true,
143184
workspace_name: file.workspace_name
144185
})
186+
if (file.content == '') {
187+
continue
188+
}
145189
const directory = path.dirname(safe_path)
146190
if (!fs.existsSync(directory)) {
147191
try {

‎packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/clipboard-parser.spec.ts‎

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,17 @@ describe('clipboard-parser', () => {
4141
is_single_root_folder_workspace: true
4242
})
4343

44-
expect(result).toHaveLength(1)
44+
expect(result).toHaveLength(2)
4545
expect(result[0]).toMatchObject({
4646
type: 'file',
47-
file_path: 'src/hello.ts',
47+
file_path: 'src/index.ts',
4848
content: load_test_case_file(test_case, '1-file.txt')
4949
})
50+
expect(result[1]).toMatchObject({
51+
type: 'file',
52+
file_path: 'src/hello.ts',
53+
content: load_test_case_file(test_case, '2-file.txt')
54+
})
5055
})
5156

5257
it('parses file when using file-xml format within a markdown code block', () => {
@@ -1299,6 +1304,30 @@ describe('clipboard-parser', () => {
12991304
})
13001305
})
13011306

1307+
it('parses deleted file header', () => {
1308+
const test_case = 'delete-file-header'
1309+
const text = load_test_case_file(test_case, `${test_case}.txt`)
1310+
const result = parse_response({
1311+
response: text,
1312+
is_single_root_folder_workspace: true
1313+
})
1314+
1315+
expect(result).toHaveLength(3)
1316+
expect(result[0]).toMatchObject({
1317+
type: 'text',
1318+
content: load_test_case_file(test_case, '1-text.txt')
1319+
})
1320+
expect(result[1]).toMatchObject({
1321+
type: 'file',
1322+
file_path: 'src/index.ts',
1323+
content: load_test_case_file(test_case, '2-file.txt')
1324+
})
1325+
expect(result[2]).toMatchObject({
1326+
type: 'text',
1327+
content: load_test_case_file(test_case, '3-text.txt')
1328+
})
1329+
})
1330+
13021331
it('parses a mix of a new file from heading and code block, and a separate diff block', () => {
13031332
const test_case = 'diff-mix-new-file-from-heading-and-diff'
13041333
const text = load_test_case_file(test_case, `${test_case}.txt`)

‎packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/parsers/multiple-files-parser.ts‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,25 @@ const create_or_update_file_item = (params: {
5252
}): void => {
5353
const { file_name, content, workspace_name, file_ref_map, results } = params
5454

55-
if (!file_name || !has_real_code(content)) {
55+
if (!file_name) {
5656
return
5757
}
5858

5959
const file_key = `${workspace_name || ''}:${file_name}`
6060

6161
if (file_ref_map.has(file_key)) {
6262
const existing_file = file_ref_map.get(file_key)!
63+
const had_content_before = has_real_code(existing_file.content)
6364
existing_file.content = content
65+
const has_content_now = has_real_code(content)
66+
67+
if (!had_content_before && has_content_now) {
68+
const index = results.indexOf(existing_file)
69+
if (index > -1) {
70+
results.splice(index, 1)
71+
results.push(existing_file)
72+
}
73+
}
6474
} else {
6575
const new_file: FileItem = {
6676
type: 'file' as const,
@@ -238,6 +248,31 @@ export const parse_multiple_files = (params: {
238248
}
239249
}
240250

251+
const deleted_file_match = line
252+
.trim()
253+
.match(/^###\s+Deleted file:\s*`([^`]+)`$/i)
254+
if (deleted_file_match && deleted_file_match[1]) {
255+
flush_text_block({
256+
text_block: current_text_block,
257+
results
258+
})
259+
current_text_block = ''
260+
const { workspace_name, relative_path } =
261+
extract_and_set_workspace_path({
262+
raw_file_path: deleted_file_match[1],
263+
is_single_root_folder_workspace:
264+
params.is_single_root_folder_workspace
265+
})
266+
create_or_update_file_item({
267+
file_name: relative_path,
268+
content: '',
269+
workspace_name,
270+
file_ref_map,
271+
results
272+
})
273+
continue
274+
}
275+
241276
let extracted_filename = extract_path_from_line_of_code(line)
242277
if (!extracted_filename) {
243278
const trimmed = line.trim()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Above.

‎packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/test-cases/delete-file-header/2-file.txt‎

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Below.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Above.
2+
3+
### Deleted file: `src/index.ts`
4+
5+
Below.
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
console.log("hello")

0 commit comments

Comments
 (0)