Skip to content

Commit 7f67d8c

Browse files
committed
Refactor file operations to reuse shared utility functions for directory creation and tab closing
- when rejecting response preview and restoring removed by it file - it shows up for a second. Expected bevavior is to not show it to the user while restoring. - sometimes we don't use utils, e.g. close_file_tabs in relocate-file.ts. look for other such cases of unused util we already have ├── apps │ └── editor │ └── src │ ├── commands │ │ └── apply-response-command │ │ ├── apply-response-command.ts │ │ ├── index.ts │ │ ├── response-processor │ │ │ ├── handlers │ │ │ │ ├── diff-handler.ts │ │ │ │ ├── fast-replace-handler.ts │ │ │ │ ├── patch-repair-handler.ts │ │ │ │ ├── restore-preview-handler.ts │ │ │ │ ├── search-replace-handler.ts │ │ │ │ └── truncated-handler.ts │ │ │ ├── index.ts │ │ │ └── response-processor.ts │ │ ├── types │ │ │ └── original-file-state.ts │ │ └── utils │ │ ├── file-checks.ts │ │ ├── file-operations │ │ │ ├── apply-file-content.ts │ │ │ ├── apply-file-relocations.ts │ │ │ ├── cleanup-rename-source.ts │ │ │ ├── close-file-tabs.ts │ │ │ ├── create-file-if-needed.ts │ │ │ ├── ensure-directory.ts │ │ │ ├── get-rename-source-info.ts │ │ │ ├── handle-deleted-file-item.ts │ │ │ ├── index.ts │ │ │ ├── prepare-rename-operation.ts │ │ │ ├── preserve-newline.ts │ │ │ ├── read-rename-source-file.ts │ │ │ ├── relocate-file.ts │ │ │ ├── remove-directory-if-empty.ts │ │ │ ├── undo-files.ts │ │ │ └── uri-exists.ts │ │ ├── preview │ │ │ ├── build-history-files.ts │ │ │ ├── diff-utils.ts │ │ │ ├── file-preparer.ts │ │ │ ├── index.ts │ │ │ ├── preview.ts │ │ │ ├── temp-file-manager.ts │ │ │ ├── types.ts │ │ │ ├── virtual-document-provider.ts │ │ │ ├── vscode-ui.ts │ │ │ └── workspace-listener.ts │ │ ├── preview-handler.ts │ │ ├── state-manager.ts │ │ ├── tab-group-manager.ts │ │ └── workspace.ts │ ├── context │ │ └── providers │ │ └── workspace │ │ └── workspace-provider.ts │ └── views │ └── prompt │ ├── backend │ │ ├── message-handlers │ │ │ ├── handle-response-preview.ts │ │ │ ├── handle-undo.ts │ │ │ └── index.ts │ │ └── prompt-view-provider.ts │ ├── frontend │ │ ├── Prompt.tsx │ │ └── hooks │ │ └── use-preview-manager.ts │ └── types │ └── messages.ts └── packages ├── shared │ └── src │ └── types │ └── file-in-preview.ts └── ui └── src └── components └── editor └── prompt-view ├── ResponsePreview │ └── ResponsePreview.tsx └── ResponsePreviewFooter └── ResponsePreviewFooter.tsx
1 parent cd96400 commit 7f67d8c

5 files changed

Lines changed: 26 additions & 89 deletions

File tree

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

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import {
1515
relocate_file,
1616
preserve_trailing_newline,
1717
replace_document_content,
18-
ensure_directory_exists
18+
ensure_directory_exists,
19+
close_file_tabs
1920
} from '../../utils/file-operations'
2021

2122
export const handle_restore_preview = async (
@@ -162,21 +163,7 @@ export const handle_restore_preview = async (
162163

163164
if (file_exists) {
164165
try {
165-
const tabs_to_close: vscode.Tab[] = []
166-
for (const tab_group of vscode.window.tabGroups.all) {
167-
tabs_to_close.push(
168-
...tab_group.tabs.filter((tab) => {
169-
const tab_uri = (tab.input as any)?.uri as
170-
| vscode.Uri
171-
| undefined
172-
return tab_uri && tab_uri.fsPath === safe_path
173-
})
174-
)
175-
}
176-
177-
if (tabs_to_close.length > 0) {
178-
await vscode.window.tabGroups.close(tabs_to_close)
179-
}
166+
await close_file_tabs(safe_path)
180167

181168
fs.unlinkSync(safe_path)
182169
Logger.info({

‎apps/editor/src/commands/apply-response-command/utils/file-operations/create-file-if-needed.ts‎

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as vscode from 'vscode'
33
import { Logger } from '@shared/utils/logger'
44
import { t } from '@/i18n'
55
import { create_safe_path } from '@/utils/path-sanitizer'
6-
import { uri_exists } from './uri-exists'
6+
import { ensure_directory_exists } from './ensure-directory'
77
import {
88
get_workspace_map_and_default,
99
resolve_workspace_root
@@ -59,29 +59,21 @@ export const create_file_if_needed = async (params: {
5959
return false
6060
}
6161
const file_uri = vscode.Uri.file(safe_path)
62-
const directory_uri = vscode.Uri.file(path.dirname(safe_path))
6362

64-
if (!(await uri_exists(directory_uri))) {
65-
try {
66-
await vscode.workspace.fs.createDirectory(directory_uri)
67-
Logger.info({
68-
function_name: 'create_file_if_needed',
69-
message: 'Directory created',
70-
data: directory_uri.fsPath
71-
})
72-
} catch (error) {
73-
Logger.error({
74-
function_name: 'create_file_if_needed',
75-
message: 'Failed to create directory',
76-
data: { directory: directory_uri.fsPath, error }
63+
try {
64+
await ensure_directory_exists(safe_path)
65+
} catch (error) {
66+
Logger.error({
67+
function_name: 'create_file_if_needed',
68+
message: 'Failed to create directory',
69+
data: { directory: path.dirname(safe_path), error }
70+
})
71+
vscode.window.showErrorMessage(
72+
t('command.apply-response-command.error.failed-to-create-directory', {
73+
path: path.dirname(safe_path)
7774
})
78-
vscode.window.showErrorMessage(
79-
t('command.apply-response-command.error.failed-to-create-directory', {
80-
path: directory_uri.fsPath
81-
})
82-
)
83-
return false
84-
}
75+
)
76+
return false
8577
}
8678

8779
try {

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

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { Logger } from '@shared/utils/logger'
44
import { create_safe_path } from '@/utils/path-sanitizer'
55
import { uri_exists } from './uri-exists'
66
import { remove_directory_if_empty } from './remove-directory-if-empty'
7+
import { close_file_tabs } from './close-file-tabs'
8+
import { ensure_directory_exists } from './ensure-directory'
79

810
export const relocate_file = async (params: {
911
old_path: string
@@ -41,21 +43,9 @@ export const relocate_file = async (params: {
4143
}
4244

4345
const new_uri = vscode.Uri.file(new_safe_path)
44-
const new_dir_uri = vscode.Uri.file(path.dirname(new_safe_path))
45-
if (!(await uri_exists(new_dir_uri))) {
46-
await vscode.workspace.fs.createDirectory(new_dir_uri)
47-
}
46+
await ensure_directory_exists(new_safe_path)
4847

49-
const text_editors = vscode.window.visibleTextEditors.filter(
50-
(editor) => editor.document.uri.toString() === old_uri.toString()
51-
)
52-
for (const editor of text_editors) {
53-
await vscode.window.showTextDocument(editor.document, {
54-
preview: false,
55-
preserveFocus: false
56-
})
57-
await vscode.commands.executeCommand('workbench.action.closeActiveEditor')
58-
}
48+
await close_file_tabs(old_safe_path)
5949

6050
await vscode.workspace.fs.rename(old_uri, new_uri, { overwrite: true })
6151

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

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ 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 { close_file_tabs } from './close-file-tabs'
11+
import { ensure_directory_exists } from './ensure-directory'
1012
import { get_error_message } from '@/utils/get-error-message'
1113
import {
1214
get_workspace_map_and_default,
@@ -62,35 +64,7 @@ export const undo_files = async (params: {
6264

6365
if (state.file_state === 'new' && !state.file_path_to_restore) {
6466
if (await uri_exists(file_uri)) {
65-
const text_editors = vscode.window.visibleTextEditors.filter(
66-
(editor) => editor.document.uri.toString() === file_uri.toString()
67-
)
68-
69-
const tabs_to_close: vscode.Tab[] = []
70-
for (const tab_group of vscode.window.tabGroups.all) {
71-
tabs_to_close.push(
72-
...tab_group.tabs.filter((tab) => {
73-
const tab_uri = (tab.input as any)?.uri as
74-
| vscode.Uri
75-
| undefined
76-
return tab_uri && tab_uri.fsPath === safe_path
77-
})
78-
)
79-
}
80-
81-
if (tabs_to_close.length > 0) {
82-
await vscode.window.tabGroups.close(tabs_to_close)
83-
}
84-
85-
for (const editor of text_editors) {
86-
await vscode.window.showTextDocument(editor.document, {
87-
preview: false,
88-
preserveFocus: false
89-
})
90-
await vscode.commands.executeCommand(
91-
'workbench.action.closeActiveEditor'
92-
)
93-
}
67+
await close_file_tabs(safe_path)
9468

9569
try {
9670
await vscode.workspace.fs.delete(file_uri)
@@ -146,15 +120,12 @@ export const undo_files = async (params: {
146120
file_uri = vscode.Uri.file(safe_path)
147121

148122
if (state.is_pure_rename) {
149-
continue
123+
continue
150124
}
151125

152126
if (!(await uri_exists(file_uri))) {
153127
try {
154-
const dir_uri = vscode.Uri.file(path.dirname(safe_path))
155-
if (!(await uri_exists(dir_uri))) {
156-
await vscode.workspace.fs.createDirectory(dir_uri)
157-
}
128+
await ensure_directory_exists(safe_path)
158129
await vscode.workspace.fs.writeFile(
159130
file_uri,
160131
Buffer.from(state.content, 'utf8')
@@ -165,8 +136,6 @@ export const undo_files = async (params: {
165136
message: 'Recreated deleted file.',
166137
data: { file_path: state.file_path }
167138
})
168-
const document = await vscode.workspace.openTextDocument(safe_path)
169-
await vscode.window.showTextDocument(document, { preview: false })
170139
} catch (err) {
171140
Logger.warn({
172141
function_name: 'undo_files',

‎apps/editor/src/constants/agents.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ export const AGENTS = {
6868
flags_placeholder: '-m deepseek/deepseek-flash#high',
6969
example_flags: [
7070
'-m deepseek/deepseek-flash#high',
71-
'-m deepseek/deepseek-flash#medium',
7271
'-m deepseek/deepseek-flash#low'
7372
]
7473
}

0 commit comments

Comments
 (0)