Skip to content

Commit 7c6af3a

Browse files
committed
Move code at cursor response application to a dedicated internal command handler
- when applying 'code at cursor' response, we should handle the response through 'code at cursor command' instad of staying in 'apply response command'. Note that 'code at cursor' command should accept e.g. cursor position from the response in case user moved cursor to other place - in 'codeWebChat.internal.applyCodeAtCursor' if file or line/character don't match, should be updated in-editor, so that the code at cursor can be applied at the suggested place └── apps └── editor └── src ├── commands │ ├── apply-response-command │ │ ├── apply-response-command.ts │ │ ├── handlers │ │ │ ├── diff-handler.ts │ │ │ ├── fast-replace-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.ts │ │ ├── preview │ │ │ ├── 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 │ │ └── state-manager.ts │ ├── code-at-cursor-command │ │ ├── code-at-cursor-commands.ts │ │ ├── index.ts │ │ └── utils │ │ ├── get-code-at-cursor-config.ts │ │ ├── perform-code-at-cursor.ts │ │ └── show-ghost-text.ts │ └── generate-commit-message-commands │ ├── actions │ │ ├── get-prompt-data.ts │ │ └── run-generate-action.ts │ ├── generate-commit-message-commands.ts │ ├── index.ts │ └── utils │ ├── build-commit-message-prompt.ts │ ├── generate-commit-message-with-api.ts │ ├── get-commit-message-config.ts │ └── strip-wrapping-quotes.ts └── constants └── instructions.ts
1 parent 1a9a4f9 commit 7c6af3a

4 files changed

Lines changed: 119 additions & 97 deletions

File tree

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,24 @@ export const apply_response_command = (params: {
124124
(item) => item.type == 'code-at-cursor'
125125
)
126126

127+
if (is_code_at_cursor) {
128+
const item = response_items.find(
129+
(i) => i.type == 'code-at-cursor'
130+
) as any
131+
132+
await vscode.commands.executeCommand(
133+
'codeWebChat.internal.applyCodeAtCursor',
134+
{
135+
file_path: item.file_path,
136+
workspace_name: item.workspace_name,
137+
line: item.line,
138+
character: item.character,
139+
content: item.content
140+
}
141+
)
142+
return
143+
}
144+
127145
if (resolve_fn && !is_intelligent_file_search_results) {
128146
const history = params.prompt_view_provider.response_history
129147

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

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -364,85 +364,6 @@ export const process_response = async (params: {
364364

365365
return null
366366
} else {
367-
if (params.response_items.some((item) => item.type == 'code-at-cursor')) {
368-
const completion = params.response_items.find(
369-
(item) => item.type == 'code-at-cursor'
370-
)!
371-
const workspace_map = new Map<string, string>()
372-
vscode.workspace.workspaceFolders!.forEach((folder) => {
373-
workspace_map.set(folder.name, folder.uri.fsPath)
374-
})
375-
const default_workspace = vscode.workspace.workspaceFolders![0].uri.fsPath
376-
let workspace_root = default_workspace
377-
if (
378-
completion.workspace_name &&
379-
workspace_map.has(completion.workspace_name)
380-
) {
381-
workspace_root = workspace_map.get(completion.workspace_name)!
382-
}
383-
const safe_path = create_safe_path(workspace_root, completion.file_path)
384-
if (!safe_path || !fs.existsSync(safe_path)) {
385-
vscode.window.showErrorMessage(
386-
t('command.apply-response.error.file-not-found', {
387-
path: completion.file_path
388-
})
389-
)
390-
Logger.warn({
391-
function_name: 'process_response',
392-
message: 'File not found for code completion.',
393-
data: { file_path: completion.file_path, safe_path }
394-
})
395-
return null
396-
}
397-
398-
const document = await vscode.workspace.openTextDocument(safe_path)
399-
const original_content = document.getText()
400-
const line_index = completion.line - 1
401-
const char_index = completion.character - 1
402-
403-
if (
404-
line_index < 0 ||
405-
char_index < 0 ||
406-
line_index >= document.lineCount ||
407-
char_index > document.lineAt(line_index).text.length
408-
) {
409-
vscode.window.showErrorMessage(
410-
t('command.apply-response.error.invalid-position', {
411-
path: completion.file_path
412-
})
413-
)
414-
return null
415-
}
416-
417-
const position_offset = document.offsetAt(
418-
new vscode.Position(line_index, char_index)
419-
)
420-
const new_content =
421-
original_content.slice(0, position_offset) +
422-
completion.content +
423-
original_content.slice(position_offset)
424-
425-
if (!params.args) params.args = {}
426-
if (!params.args.original_editor_state) {
427-
params.args.original_editor_state = {
428-
file_path: safe_path,
429-
position: {
430-
line: line_index,
431-
character: char_index
432-
}
433-
}
434-
}
435-
436-
params.response_items = [
437-
{
438-
type: 'file',
439-
file_path: completion.file_path,
440-
content: new_content,
441-
workspace_name: completion.workspace_name
442-
}
443-
]
444-
}
445-
446367
const files = params.response_items.filter(
447368
(item): item is FileItem => item.type == 'file'
448369
)

‎apps/editor/src/commands/code-at-cursor-command/code-at-cursor-commands.ts‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import * as vscode from 'vscode'
2+
import * as fs from 'fs'
3+
import * as path from 'path'
4+
import { create_safe_path } from '@/utils/path-sanitizer'
5+
import { show_ghost_text } from './utils/show-ghost-text'
6+
import { normalize_path } from '@/utils/normalize-path'
27
import { WorkspaceProvider } from '@/context/providers/workspace/workspace-provider'
38
import { OpenEditorsProvider } from '@/context/providers/open-editors/open-editors-provider'
49
import { CommitMessageDetails } from '@/utils/commit-message-details'
@@ -12,6 +17,102 @@ export const code_at_cursor_commands = (params: {
1217
websocket_manager: WebSocketManager
1318
}) => {
1419
return [
20+
vscode.commands.registerCommand(
21+
'codeWebChat.internal.applyCodeAtCursor',
22+
async (args: {
23+
file_path: string
24+
workspace_name?: string
25+
line: number
26+
character: number
27+
content: string
28+
}) => {
29+
const workspace_map = new Map<string, string>()
30+
vscode.workspace.workspaceFolders?.forEach((folder) => {
31+
workspace_map.set(folder.name, folder.uri.fsPath)
32+
})
33+
const default_workspace =
34+
vscode.workspace.workspaceFolders?.[0]?.uri.fsPath
35+
if (!default_workspace) {
36+
vscode.window.showErrorMessage('No workspace folder open')
37+
return
38+
}
39+
40+
let workspace_root = default_workspace
41+
if (args.workspace_name && workspace_map.has(args.workspace_name)) {
42+
workspace_root = workspace_map.get(args.workspace_name)!
43+
}
44+
45+
const safe_path = create_safe_path(workspace_root, args.file_path)
46+
if (!safe_path || !fs.existsSync(safe_path)) {
47+
vscode.window.showErrorMessage(`File not found: ${args.file_path}`)
48+
return
49+
}
50+
51+
const document = await vscode.workspace.openTextDocument(safe_path)
52+
const editor = await vscode.window.showTextDocument(document)
53+
54+
const line_index = args.line - 1
55+
const char_index = args.character - 1
56+
57+
if (
58+
line_index < 0 ||
59+
char_index < 0 ||
60+
line_index >= document.lineCount ||
61+
char_index > document.lineAt(line_index).text.length
62+
) {
63+
vscode.window.showErrorMessage(
64+
`Invalid position in file: ${args.file_path}`
65+
)
66+
return
67+
}
68+
69+
const position = new vscode.Position(line_index, char_index)
70+
71+
if (
72+
editor.selection.active.line !== position.line ||
73+
editor.selection.active.character !== position.character
74+
) {
75+
editor.selection = new vscode.Selection(position, position)
76+
editor.revealRange(
77+
new vscode.Range(position, position),
78+
vscode.TextEditorRevealType.InCenterIfOutsideViewport
79+
)
80+
}
81+
82+
const decoded_completion = args.content
83+
84+
const selected_files: string[] = []
85+
const checked_files = params.workspace_provider.get_checked_files()
86+
for (const file of checked_files) {
87+
const file_workspace_root =
88+
params.workspace_provider.get_workspace_root_for_file(file)
89+
if (file_workspace_root === workspace_root) {
90+
const relative_path = normalize_path(
91+
path.relative(workspace_root, file)
92+
)
93+
selected_files.push(relative_path)
94+
}
95+
}
96+
97+
await show_ghost_text({
98+
editor,
99+
position,
100+
ghost_text: decoded_completion,
101+
command: {
102+
title: 'Code at Cursor Accepted',
103+
command: 'codeWebChat.internal.codeAtCursorAccepted',
104+
arguments: [
105+
{
106+
workspace_root,
107+
prompt: '',
108+
file_path: safe_path,
109+
selected_files
110+
}
111+
]
112+
}
113+
})
114+
}
115+
),
15116
vscode.commands.registerCommand(
16117
'codeWebChat.internal.codeAtCursorAccepted',
17118
async (args: {

‎apps/editor/src/i18n/translations/commands/apply-response.ts‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -200,24 +200,6 @@ export const translations = {
200200
hu: 'Fájl nem található: {{path}}.',
201201
bg: 'Файлът не е намерен: {{path}}.'
202202
},
203-
'command.apply-response.error.invalid-position': {
204-
en: 'Invalid position for code completion in {{path}}.',
205-
pl: 'Nieprawidłowa pozycja dla uzupełniania kodu w {{path}}.',
206-
'zh-cn': '{{path}} 中的代码补全位置无效。',
207-
ja: '{{path}} のコード補完位置が無効です。',
208-
'zh-tw': '{{path}} 中的代碼補全位置無效。',
209-
de: 'Ungültige Position für Codevervollständigung in {{path}}.',
210-
es: 'Posición no válida para la finalización de código en {{path}}.',
211-
fr: 'Position invalide pour la complétion de code dans {{path}}.',
212-
'pt-br': 'Posição inválida para preenchimento de código em {{path}}.',
213-
ru: 'Недопустимая позиция для автодополнения кода в {{path}}.',
214-
ko: '{{path}}의 코드 완성 위치가 잘못되었습니다.',
215-
it: 'Posizione non valida per il completamento del codice in {{path}}.',
216-
tr: '{{path}} içinde kod tamamlama için geçersiz konum.',
217-
cs: 'Neplatná pozice pro dokončování kódu v {{path}}.',
218-
hu: 'Érvénytelen pozíció a kódkiegészítéshez a következőben: {{path}}.',
219-
bg: 'Невалидна позиция за завършване на код в {{path}}.'
220-
},
221203
'command.apply-response.error.invalid-file-path-traversal': {
222204
en: 'Invalid file path: {{path}}. Path may contain traversal attempts.',
223205
pl: 'Nieprawidłowa ścieżka pliku: {{path}}. Ścieżka może zawierać próby przejścia.',

0 commit comments

Comments
 (0)