Skip to content

Commit 6fa776c

Browse files
committed
Fix preview state handling for renamed files and patch repair by cleaning up old paths and propagating updated content
- apply this: [Pasted text] - in a scenario where 'patch repair' renames a file, the newly created file is displayed as deleted with '-0' line numbers. expected behavior: when 'patch repair' renames a file, it should be marked as new file with total line numbers created e.g. '+200' - in a scenario where 'patch repair' renames a file, the newly created file has correct line counts only when fixed by chatbot—when fixed by api call, it shows '+0'. Expected behavior: should show the right line count when using api as well. - In `workspace-listener.ts`, update `set_file_applied_with_patch_repair` to also set `file.previewable_file.proposed_content = new_content` when `new_content` is provided, preventing the UI from falsely displaying the "Discard user changes" action. - The same ternary is repeated about 40 times. This should be a single helper (e.g. in @shared/utils): ts export const get_error_message = (error: unknown): string => error instanceof Error ? error.message || error.name : String(error) - use `packages/shared/src/utils/get-error-message.ts` when possible - move get_error_message to `apps/editor/src/utils` ├── apps │ └── editor │ └── src │ ├── commands │ │ ├── apply-response-command │ │ │ ├── apply-response-command.ts │ │ │ ├── response-processor │ │ │ │ └── handlers │ │ │ │ ├── diff-handler.ts │ │ │ │ ├── fast-replace-handler.ts │ │ │ │ ├── patch-repair-handler.ts │ │ │ │ ├── search-replace-handler.ts │ │ │ │ └── truncated-handler.ts │ │ │ ├── types │ │ │ │ └── original-file-state.ts │ │ │ └── utils │ │ │ ├── file-operations │ │ │ │ ├── handle-deleted-file-item.ts │ │ │ │ └── undo-files.ts │ │ │ └── preview │ │ │ ├── build-history-files.ts │ │ │ ├── diff-utils.ts │ │ │ ├── file-preparer.ts │ │ │ ├── types.ts │ │ │ └── workspace-listener.ts │ │ ├── copy-markdown-command.ts │ │ ├── copy-merge-commit-details-command.ts │ │ ├── generate-commit-message-commands │ │ │ └── actions │ │ │ └── run-generate-action.ts │ │ ├── history-command │ │ │ └── actions │ │ │ ├── delete-checkpoint-with-undo.ts │ │ │ └── restore-checkpoint.ts │ │ ├── new-file-command.ts │ │ ├── new-folder-command.ts │ │ ├── rename-command.ts │ │ ├── select-clipboard-paths-command.ts │ │ ├── select-definition-file-command.ts │ │ ├── select-files-of-commit-command.ts │ │ ├── select-referencing-files-commands │ │ │ └── select-referencing-files-commands.ts │ │ └── set-ranges-command.ts │ ├── features │ │ ├── checkpoints │ │ │ └── actions │ │ │ └── create-checkpoint.ts │ │ ├── context-restoration │ │ │ └── utils │ │ │ └── context-file-utils.ts │ │ └── search-files │ │ └── search-files.ts │ ├── migrations │ │ ├── migrate-clear-all-checkpoints.ts │ │ ├── migrate-commit-message-details-filename.ts │ │ ├── migrate-configurations-to-api-configurations.ts │ │ ├── migrate-edit-context-to-edit-files-system-instructions.ts │ │ ├── migrate-model-providers-to-providers.ts │ │ ├── migrate-prompt-templates-suffixes.ts │ │ ├── migrate-prompt-templates-to-templates.ts │ │ └── migrate-settings-keys-to-chatbots-and-models.ts │ ├── utils │ │ ├── build-user-content.ts │ │ └── get-error-message.ts │ └── views │ ├── prompt │ │ └── backend │ │ ├── message-handlers │ │ │ ├── handle-patch-repair.ts │ │ │ ├── handle-pick-api-reasoning-effort.ts │ │ │ ├── handle-preview-generated-code.ts │ │ │ └── handle-voice-input.ts │ │ ├── prompt-view-provider.ts │ │ └── utils │ │ └── preview-text-in-temp-file.ts │ ├── settings │ │ └── backend │ │ └── message-handlers │ │ └── handle-pick-api-reasoning-effort.ts │ └── shared │ └── actions │ ├── agent │ │ └── delete.ts │ ├── api │ │ ├── create │ │ │ └── interactions │ │ │ └── initial-select-model.ts │ │ └── update │ │ └── interactions │ │ └── edit-model-for-api-config.ts │ └── web │ └── delete.ts └── packages ├── shared │ ├── package.json │ └── src │ └── utils │ └── logger.ts └── ui └── src └── components └── editor └── prompt └── ResponsePreview └── components └── FileItem └── FileItem.tsx
1 parent 416f0f5 commit 6fa776c

47 files changed

Lines changed: 194 additions & 75 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/editor/src/commands/apply-response-command/response-processor/handlers/diff-handler.ts‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { create_safe_path } from '@/utils/path-sanitizer'
88
import { apply_diff } from '../../../../utils/changes-integration/diff-processor'
99
import { remove_directory_if_empty } from '../../utils/file-operations'
1010
import { DiffItem } from '../../utils/response-parser'
11+
import { get_error_message } from '@/utils/get-error-message'
1112

1213
export const sanitize_patch_content = (
1314
patch_content: string,
@@ -376,7 +377,7 @@ const handle_new_file_patch = async (
376377
function_name: 'handle_new_file_patch',
377378
message: 'Failed to create new file from patch.',
378379
data: {
379-
error: error instanceof Error ? error.message : String(error),
380+
error: get_error_message(error),
380381
file_path
381382
}
382383
})
@@ -457,7 +458,7 @@ const handle_deleted_file_patch = async (
457458
function_name: 'handle_deleted_file_patch',
458459
message: 'Failed to delete file from patch.',
459460
data: {
460-
error: error instanceof Error ? error.message : String(error),
461+
error: get_error_message(error),
461462
file_path
462463
}
463464
})
@@ -679,10 +680,7 @@ export const apply_git_patch = async (
679680
// This outer catch handles setup errors and final application failures
680681
await reopen_closed_files(closed_files)
681682

682-
const has_rejects =
683-
error instanceof Error
684-
? error.message.includes('.rej')
685-
: String(error).includes('.rej')
683+
const has_rejects = get_error_message(error).includes('.rej')
686684
if (has_rejects) {
687685
const file_paths = extract_file_paths_from_patch(patch_content)
688686
await process_modified_files(file_paths, workspace_path)

‎apps/editor/src/commands/apply-response-command/response-processor/handlers/fast-replace-handler.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
get_workspace_map_and_default,
1111
resolve_workspace_root
1212
} from '../../utils/workspace'
13+
import { get_error_message } from '@/utils/get-error-message'
1314
import {
1415
cleanup_rename_source,
1516
handle_deleted_file_item,
@@ -175,7 +176,7 @@ export const handle_fast_replace = async (params: {
175176
vscode.window.showErrorMessage(
176177
t('command.apply-response-command.error.processing-file', {
177178
path: file.file_path,
178-
msg: error instanceof Error ? error.message : String(error)
179+
msg: get_error_message(error)
179180
})
180181
)
181182
continue
@@ -288,7 +289,7 @@ export const handle_fast_replace = async (params: {
288289
vscode.window.showErrorMessage(
289290
t('command.apply-response-command.error.processing-file', {
290291
path: file.file_path,
291-
msg: error instanceof Error ? error.message : String(error)
292+
msg: get_error_message(error)
292293
})
293294
)
294295
continue
@@ -310,7 +311,7 @@ export const handle_fast_replace = async (params: {
310311
console.error('Error during direct file replacement:', error)
311312
vscode.window.showErrorMessage(
312313
t('command.apply-response-command.error.replacing-files', {
313-
msg: error instanceof Error ? error.message : String(error)
314+
msg: get_error_message(error)
314315
})
315316
)
316317
return { success: false }

‎apps/editor/src/commands/apply-response-command/response-processor/handlers/patch-repair-handler.ts‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
get_workspace_map_and_default,
1010
resolve_workspace_root
1111
} from '../../utils/workspace'
12+
import { LAST_APPLIED_CHANGES_STATE_KEY } from '@/constants/state-keys'
13+
import { OriginalFileState } from '../../types/original-file-state'
1214

1315
export const handle_patch_repair = async (params: {
1416
patch_items: PatchRepairItem[]
@@ -17,6 +19,8 @@ export const handle_patch_repair = async (params: {
1719
}): Promise<void> => {
1820
const { workspace_map, default_workspace } = get_workspace_map_and_default()
1921

22+
const original_states = params.prompt_view_provider.extension_context.workspaceState.get<OriginalFileState[]>(LAST_APPLIED_CHANGES_STATE_KEY)
23+
2024
params.workspace_provider.pause_file_watcher()
2125
try {
2226
for (const patch_item of params.patch_items) {
@@ -27,14 +31,40 @@ export const handle_patch_repair = async (params: {
2731
})
2832
const safe_path = create_safe_path(workspace_root, patch_item.file_path)
2933
if (safe_path) {
34+
if (original_states) {
35+
const state = original_states.find(
36+
(s) =>
37+
s.file_path == patch_item.file_path &&
38+
s.workspace_name == patch_item.workspace_name
39+
)
40+
if (state?.file_path_to_restore) {
41+
const old_workspace_root = resolve_workspace_root({
42+
workspace_name:
43+
state.restore_workspace_name ?? patch_item.workspace_name,
44+
workspace_map,
45+
default_workspace
46+
})
47+
const old_safe_path = create_safe_path(
48+
old_workspace_root,
49+
state.file_path_to_restore
50+
)
51+
if (old_safe_path) {
52+
try {
53+
await vscode.workspace.fs.delete(vscode.Uri.file(old_safe_path))
54+
} catch (e) {}
55+
}
56+
}
57+
}
58+
3059
await vscode.workspace.fs.writeFile(
3160
vscode.Uri.file(safe_path),
3261
Buffer.from(patch_item.content, 'utf8')
3362
)
3463
if (set_file_applied_with_patch_repair) {
3564
set_file_applied_with_patch_repair({
3665
file_path: patch_item.file_path,
37-
workspace_name: patch_item.workspace_name
66+
workspace_name: patch_item.workspace_name,
67+
new_content: patch_item.content
3868
})
3969
}
4070
}
@@ -47,4 +77,4 @@ export const handle_patch_repair = async (params: {
4777
} finally {
4878
params.workspace_provider.resume_file_watcher()
4979
}
50-
}
80+
}

‎apps/editor/src/commands/apply-response-command/response-processor/handlers/search-replace-handler.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
handle_deleted_file_item,
1616
get_rename_source_info
1717
} from '../../utils/file-operations'
18+
import { get_error_message } from '@/utils/get-error-message'
1819
import { apply_search_replace_to_content } from '../../../../utils/changes-integration/search-replace-processor/apply-search-replace-to-content'
1920
import { parse_search_replace_segments } from '../../../../utils/changes-integration/search-replace-processor/parse-search-replace-segments'
2021

@@ -263,7 +264,7 @@ export const handle_search_replace = async (params: {
263264
})
264265
vscode.window.showErrorMessage(
265266
t('command.apply-response-command.error.applying-changes', {
266-
msg: error instanceof Error ? error.message : String(error)
267+
msg: get_error_message(error)
267268
})
268269
)
269270
return { success: false }

‎apps/editor/src/commands/apply-response-command/response-processor/handlers/truncated-handler.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
get_workspace_map_and_default,
1111
resolve_workspace_root
1212
} from '../../utils/workspace'
13+
import { get_error_message } from '@/utils/get-error-message'
1314
import { process_truncations } from '../../../../utils/changes-integration/truncations-processor'
1415
import {
1516
cleanup_rename_source,
@@ -141,7 +142,7 @@ export const handle_truncated_edit = async (params: {
141142
function_name: 'handle_truncated_edit',
142143
message: 'Failed to process truncated file for rename',
143144
data: {
144-
error: error instanceof Error ? error.message : String(error),
145+
error: get_error_message(error),
145146
file_path: safe_path
146147
}
147148
})
@@ -174,7 +175,7 @@ export const handle_truncated_edit = async (params: {
174175
function_name: 'handle_truncated_edit',
175176
message: 'Failed to create new file',
176177
data: {
177-
error: error instanceof Error ? error.message : String(error),
178+
error: get_error_message(error),
178179
file_path: safe_path
179180
}
180181
})
@@ -216,7 +217,7 @@ export const handle_truncated_edit = async (params: {
216217
function_name: 'handle_truncated_edit',
217218
message: 'Failed to process truncated file',
218219
data: {
219-
error: error instanceof Error ? error.message : String(error),
220+
error: get_error_message(error),
220221
file_path: safe_path
221222
}
222223
})

‎apps/editor/src/commands/apply-response-command/response-processor/response-processor.ts‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -544,12 +544,17 @@ const create_failed_file_state = (params: {
544544
default_workspace: string
545545
workspace_map: Map<string, string>
546546
}): OriginalFileState => {
547+
const read_workspace_name = params.file.renamed_from
548+
? (params.file.renamed_from_workspace ?? params.file.workspace_name)
549+
: params.file.workspace_name
550+
const read_file_path = params.file.renamed_from ?? params.file.file_path
551+
547552
const workspace_root = resolve_workspace_root({
548-
workspace_name: params.file.workspace_name,
553+
workspace_name: read_workspace_name,
549554
workspace_map: params.workspace_map,
550555
default_workspace: params.default_workspace
551556
})
552-
const safe_path = create_safe_path(workspace_root, params.file.file_path)
557+
const safe_path = create_safe_path(workspace_root, read_file_path)
553558
let content = ''
554559

555560
if (safe_path && fs.existsSync(safe_path)) {
@@ -558,11 +563,20 @@ const create_failed_file_state = (params: {
558563
} catch (e) {}
559564
}
560565

561-
return {
566+
const result: OriginalFileState = {
562567
file_path: params.file.file_path,
563568
workspace_name: params.file.workspace_name,
564569
content,
565570
apply_failed: true,
566571
ai_content: params.file.content
567572
}
568-
}
573+
574+
if (params.file.renamed_from) {
575+
result.file_path_to_restore = params.file.renamed_from
576+
if (params.file.renamed_from_workspace) {
577+
result.restore_workspace_name = params.file.renamed_from_workspace
578+
}
579+
}
580+
581+
return result
582+
}

‎apps/editor/src/commands/apply-response-command/utils/file-operations/handle-deleted-file-item.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { get_rename_source_info } from './get-rename-source-info'
88
import { cleanup_rename_source } from './cleanup-rename-source'
99
import { close_file_tabs } from './close-file-tabs'
1010
import { remove_directory_if_empty } from './remove-directory-if-empty'
11+
import { get_error_message } from '@/utils/get-error-message'
1112

1213
export const handle_deleted_file_item = async (params: {
1314
file: FileItem
@@ -54,7 +55,7 @@ export const handle_deleted_file_item = async (params: {
5455
function_name: params.function_name,
5556
message: 'Failed to delete file',
5657
data: {
57-
error: error instanceof Error ? error.message : String(error),
58+
error: get_error_message(error),
5859
file_path: params.safe_path
5960
}
6061
})

‎apps/editor/src/commands/apply-response-command/utils/file-operations/undo-files.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { create_safe_path } from '@/utils/path-sanitizer'
77
import { uri_exists } from './uri-exists'
88
import { remove_directory_if_empty } from './remove-directory-if-empty'
99
import { relocate_file } from './relocate-file'
10+
import { get_error_message } from '@/utils/get-error-message'
1011
import {
1112
get_workspace_map_and_default,
1213
resolve_workspace_root
@@ -243,7 +244,7 @@ export const undo_files = async (params: {
243244
console.error('Error during undo:', error)
244245
vscode.window.showErrorMessage(
245246
t('command.apply-response-command.error.failed-to-undo', {
246-
msg: error instanceof Error ? error.message : String(error)
247+
msg: get_error_message(error)
247248
})
248249
)
249250
return false

‎apps/editor/src/commands/apply-response-command/utils/preview/build-history-files.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export const build_history_files = async (params: {
6767
total_lines_removed += diff_stats.lines_removed
6868

6969
const is_deleted =
70-
state.file_state != 'new' && !file_exists && state.content != ''
70+
state.file_state != 'new' && !is_rename && !file_exists && state.content != ''
7171

7272
files_for_history.push({
7373
type: 'file',

‎apps/editor/src/commands/apply-response-command/utils/preview/file-preparer.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export const prepare_files_from_original_states = async (params: {
7474
})
7575
const is_deleted =
7676
state.file_state == 'deleted' ||
77-
(state.file_state != 'new' && !file_exists && state.content != '')
77+
(state.file_state != 'new' && !state.file_path_to_restore && !file_exists && state.content != '')
7878

7979
const previewable_file: PreviewableFile = {
8080
type: 'file',

0 commit comments

Comments
 (0)