Skip to content

Commit f48ac58

Browse files
committed
Refactor rename file operations into shared utilities and add titles to configuration quick picks
1 parent 88e2eb7 commit f48ac58

6 files changed

Lines changed: 159 additions & 88 deletions

File tree

‎apps/editor/src/commands/apply-chat-response-command/handlers/conflict-markers-handler.ts‎

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ 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 '../types/original-file-state'
9-
import { remove_directory_if_empty } from '../utils/file-operations'
9+
import {
10+
read_rename_source_file,
11+
cleanup_rename_source
12+
} from '../utils/file-operations'
1013
import { apply_conflict_markers_to_content } from '../utils/edit-formats/conflict-markers/apply-conflict-markers-to-content'
1114
import { parse_conflict_segments } from '../utils/edit-formats/conflict-markers'
1215

@@ -70,24 +73,13 @@ export const handle_conflict_markers = async (
7073
let rename_source_content: string | undefined
7174

7275
if (file.renamed_from) {
73-
const sanitized_rename_path = sanitize_file_name(file.renamed_from)
74-
const safe_rename_path = create_safe_path(
75-
workspace_root,
76-
sanitized_rename_path
77-
)
78-
if (safe_rename_path && fs.existsSync(safe_rename_path)) {
79-
rename_source_path = safe_rename_path
80-
try {
81-
const document =
82-
await vscode.workspace.openTextDocument(safe_rename_path)
83-
rename_source_content = document.getText()
84-
} catch (e) {
85-
Logger.warn({
86-
function_name: 'handle_conflict_markers',
87-
message: 'Failed to read rename source file',
88-
data: safe_rename_path
89-
})
90-
}
76+
const source_info = await read_rename_source_file({
77+
renamed_from: file.renamed_from,
78+
workspace_root
79+
})
80+
if (source_info) {
81+
rename_source_path = source_info.path
82+
rename_source_content = source_info.content
9183
}
9284
}
9385

@@ -103,25 +95,8 @@ export const handle_conflict_markers = async (
10395
new_content = file.content
10496
}
10597

106-
const tabs_to_close: vscode.Tab[] = []
107-
for (const tab_group of vscode.window.tabGroups.all) {
108-
tabs_to_close.push(
109-
...tab_group.tabs.filter((tab) => {
110-
const tab_uri = (tab.input as any)?.uri as
111-
| vscode.Uri
112-
| undefined
113-
return tab_uri && tab_uri.fsPath === rename_source_path
114-
})
115-
)
116-
}
117-
118-
if (tabs_to_close.length > 0) {
119-
await vscode.window.tabGroups.close(tabs_to_close)
120-
}
121-
122-
await vscode.workspace.fs.delete(vscode.Uri.file(rename_source_path))
123-
await remove_directory_if_empty({
124-
dir_path: path.dirname(rename_source_path),
98+
await cleanup_rename_source({
99+
source_path: rename_source_path,
125100
workspace_root
126101
})
127102

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

Lines changed: 22 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ 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'
9+
import {
10+
remove_directory_if_empty,
11+
close_file_tabs,
12+
read_rename_source_file,
13+
cleanup_rename_source
14+
} from '../utils/file-operations'
1015

1116
export const handle_fast_replace = async (
1217
files: FileItem[]
@@ -102,16 +107,13 @@ export const handle_fast_replace = async (
102107
if (file.is_deleted) {
103108
// If the file was also renamed (moved then deleted), we need to ensure the source is deleted too.
104109
if (file.renamed_from) {
105-
const sanitized_rename_path = sanitize_file_name(file.renamed_from)
106-
const safe_rename_path = create_safe_path(
107-
workspace_root,
108-
sanitized_rename_path
109-
)
110-
if (safe_rename_path && fs.existsSync(safe_rename_path)) {
111-
await close_file_tabs(safe_rename_path)
112-
await vscode.workspace.fs.delete(vscode.Uri.file(safe_rename_path))
113-
await remove_directory_if_empty({
114-
dir_path: path.dirname(safe_rename_path),
110+
const source_info = await read_rename_source_file({
111+
renamed_from: file.renamed_from,
112+
workspace_root
113+
})
114+
if (source_info) {
115+
await cleanup_rename_source({
116+
source_path: source_info.path,
115117
workspace_root
116118
})
117119
}
@@ -156,24 +158,13 @@ export const handle_fast_replace = async (
156158
let rename_source_content: string | undefined
157159

158160
if (file.renamed_from) {
159-
const sanitized_rename_path = sanitize_file_name(file.renamed_from)
160-
const safe_rename_path = create_safe_path(
161-
workspace_root,
162-
sanitized_rename_path
163-
)
164-
if (safe_rename_path && fs.existsSync(safe_rename_path)) {
165-
rename_source_path = safe_rename_path
166-
try {
167-
const document =
168-
await vscode.workspace.openTextDocument(safe_rename_path)
169-
rename_source_content = document.getText()
170-
} catch (e) {
171-
Logger.warn({
172-
function_name: 'handle_fast_replace',
173-
message: 'Failed to read rename source file',
174-
data: safe_rename_path
175-
})
176-
}
161+
const source_info = await read_rename_source_file({
162+
renamed_from: file.renamed_from,
163+
workspace_root
164+
})
165+
if (source_info) {
166+
rename_source_path = source_info.path
167+
rename_source_content = source_info.content
177168
}
178169
}
179170

@@ -184,11 +175,8 @@ export const handle_fast_replace = async (
184175
new_content = rename_source_content
185176
}
186177

187-
await close_file_tabs(rename_source_path)
188-
189-
await vscode.workspace.fs.delete(vscode.Uri.file(rename_source_path))
190-
await remove_directory_if_empty({
191-
dir_path: path.dirname(rename_source_path),
178+
await cleanup_rename_source({
179+
source_path: rename_source_path,
192180
workspace_root
193181
})
194182

@@ -349,18 +337,3 @@ export const handle_fast_replace = async (
349337
return { success: false }
350338
}
351339
}
352-
353-
const close_file_tabs = async (file_path: string) => {
354-
const tabs_to_close: vscode.Tab[] = []
355-
for (const tab_group of vscode.window.tabGroups.all) {
356-
tabs_to_close.push(
357-
...tab_group.tabs.filter((tab) => {
358-
const tab_uri = (tab.input as any)?.uri as vscode.Uri | undefined
359-
return tab_uri && tab_uri.fsPath === file_path
360-
})
361-
)
362-
}
363-
if (tabs_to_close.length > 0) {
364-
await vscode.window.tabGroups.close(tabs_to_close)
365-
}
366-
}

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import { create_safe_path, sanitize_file_name } from '@/utils/path-sanitizer'
77
import { FileItem } from '../utils/clipboard-parser'
88
import { OriginalFileState } from '../types/original-file-state'
99
import { process_truncated_content } from '../utils/edit-formats/truncations'
10+
import {
11+
read_rename_source_file,
12+
cleanup_rename_source
13+
} from '../utils/file-operations'
1014

1115
export const handle_truncated_edit = async (
1216
files: FileItem[]
@@ -58,6 +62,67 @@ export const handle_truncated_edit = async (
5862
continue
5963
}
6064

65+
let rename_source_path: string | undefined
66+
let rename_source_content: string | undefined
67+
68+
if (file.renamed_from) {
69+
const source_info = await read_rename_source_file({
70+
renamed_from: file.renamed_from,
71+
workspace_root
72+
})
73+
if (source_info) {
74+
rename_source_path = source_info.path
75+
rename_source_content = source_info.content
76+
}
77+
}
78+
79+
if (rename_source_path && rename_source_content !== undefined) {
80+
try {
81+
const new_content = process_truncated_content(
82+
file.content,
83+
rename_source_content
84+
)
85+
86+
await cleanup_rename_source({
87+
source_path: rename_source_path,
88+
workspace_root
89+
})
90+
91+
const directory = path.dirname(safe_path)
92+
if (!fs.existsSync(directory)) {
93+
await fs.promises.mkdir(directory, { recursive: true })
94+
}
95+
await vscode.workspace.fs.writeFile(
96+
vscode.Uri.file(safe_path),
97+
Buffer.from(new_content, 'utf8')
98+
)
99+
100+
original_states.push({
101+
file_path: file.file_path,
102+
content: rename_source_content,
103+
workspace_name: file.workspace_name,
104+
file_path_to_restore: file.renamed_from,
105+
ai_content: file.content,
106+
proposed_content: new_content
107+
})
108+
109+
Logger.info({
110+
function_name: 'handle_truncated_edit',
111+
message: 'Applied truncated edit',
112+
data: safe_path
113+
})
114+
continue
115+
} catch (error: any) {
116+
Logger.error({
117+
function_name: 'handle_truncated_edit',
118+
message: 'Failed to process truncated file for rename',
119+
data: { error: error.message, file_path: safe_path }
120+
})
121+
failed_files.push(file)
122+
continue
123+
}
124+
}
125+
61126
if (!fs.existsSync(safe_path)) {
62127
// Cannot fill truncations for a file that doesn't exist
63128
failed_files.push(file)

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

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as vscode from 'vscode'
22
import * as path from 'path'
3-
import { create_safe_path } from '@/utils/path-sanitizer'
3+
import * as fs from 'fs'
4+
import { create_safe_path, sanitize_file_name } from '@/utils/path-sanitizer'
45
import { dictionary } from '@shared/constants/dictionary'
56
import { Logger } from '@shared/utils/logger'
67
import { OriginalFileState } from '@/commands/apply-chat-response-command/types/original-file-state'
@@ -59,6 +60,61 @@ export const remove_directory_if_empty = async (params: {
5960
}
6061
}
6162

63+
export const close_file_tabs = async (file_path: string) => {
64+
const tabs_to_close: vscode.Tab[] = []
65+
for (const tab_group of vscode.window.tabGroups.all) {
66+
tabs_to_close.push(
67+
...tab_group.tabs.filter((tab) => {
68+
const tab_uri = (tab.input as any)?.uri as vscode.Uri | undefined
69+
return tab_uri && tab_uri.fsPath === file_path
70+
})
71+
)
72+
}
73+
if (tabs_to_close.length > 0) {
74+
await vscode.window.tabGroups.close(tabs_to_close)
75+
}
76+
}
77+
78+
export const read_rename_source_file = async (params: {
79+
renamed_from: string
80+
workspace_root: string
81+
}): Promise<{ path: string; content: string } | undefined> => {
82+
const sanitized_rename_path = sanitize_file_name(params.renamed_from)
83+
const safe_rename_path = create_safe_path(
84+
params.workspace_root,
85+
sanitized_rename_path
86+
)
87+
88+
if (safe_rename_path && fs.existsSync(safe_rename_path)) {
89+
try {
90+
const document = await vscode.workspace.openTextDocument(safe_rename_path)
91+
return {
92+
path: safe_rename_path,
93+
content: document.getText()
94+
}
95+
} catch (e) {
96+
Logger.warn({
97+
function_name: 'read_rename_source_file',
98+
message: 'Failed to read rename source file',
99+
data: safe_rename_path
100+
})
101+
}
102+
}
103+
return undefined
104+
}
105+
106+
export const cleanup_rename_source = async (params: {
107+
source_path: string
108+
workspace_root: string
109+
}) => {
110+
await close_file_tabs(params.source_path)
111+
await vscode.workspace.fs.delete(vscode.Uri.file(params.source_path))
112+
await remove_directory_if_empty({
113+
dir_path: path.dirname(params.source_path),
114+
workspace_root: params.workspace_root
115+
})
116+
}
117+
62118
export const create_file_if_needed = async (params: {
63119
file_path: string
64120
content: string

‎apps/editor/src/views/panel/backend/message-handlers/handle-edit-context.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ const get_edit_context_config = async (params: {
183183

184184
const quick_pick = vscode.window.createQuickPick<Item>()
185185
quick_pick.items = await create_items()
186+
quick_pick.title = 'Configurations'
186187
quick_pick.placeholder = 'Select configuration'
187188
quick_pick.matchOnDescription = true
188189

‎apps/editor/src/views/panel/backend/message-handlers/handle-prune-context.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ const get_prune_context_config = async (
165165

166166
const quick_pick = vscode.window.createQuickPick<Item>()
167167
quick_pick.items = await create_items()
168+
quick_pick.title = 'Configurations'
168169
quick_pick.placeholder = 'Select configuration'
169170
quick_pick.matchOnDescription = true
170171

0 commit comments

Comments
 (0)