Skip to content

Commit 1915d10

Browse files
committed
Refactor code completion message handling and ensure the prompt field is focused after executing commands or closing the history picker
1 parent 00cc8de commit 1915d10

5 files changed

Lines changed: 29 additions & 66 deletions

File tree

‎packages/vscode/src/commands/code-completion-commands.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ const get_code_completion_config = async (
192192
quick_pick.activeItems = [last_selected_item]
193193
} else if (items.length > 0) {
194194
const first_selectable = items.find(
195-
(i) => i.kind !== vscode.QuickPickItemKind.Separator
195+
(i) => i.kind != vscode.QuickPickItemKind.Separator
196196
)
197197
if (first_selectable) {
198198
quick_pick.activeItems = [first_selectable]

‎packages/vscode/src/views/panel/backend/message-handlers/handle-code-completion.ts‎

Lines changed: 23 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ const get_code_completion_config = async (
166166
quick_pick.activeItems = [last_selected_item]
167167
} else if (items.length > 0) {
168168
const first_selectable = items.find(
169-
(i) => i.kind !== vscode.QuickPickItemKind.Separator
169+
(i) => i.kind != vscode.QuickPickItemKind.Separator
170170
)
171171
if (first_selectable) {
172172
quick_pick.activeItems = [first_selectable]
@@ -255,60 +255,37 @@ const get_code_completion_config = async (
255255
}
256256
}
257257

258-
const perform_code_completion = async (params: {
259-
file_tree_provider: any
260-
open_editors_provider: any
261-
context: vscode.ExtensionContext
262-
with_completion_instructions: boolean
263-
show_quick_pick?: boolean
264-
completion_instructions?: string
265-
config_id?: string
266-
panel_provider: PanelProvider
267-
invocation_count: number
268-
}): Promise<void> => {
269-
const api_providers_manager = new ModelProvidersManager(params.context)
270-
271-
let completion_instructions: string | undefined =
272-
params.completion_instructions
273-
if (params.with_completion_instructions && !completion_instructions) {
274-
const last_value =
275-
params.context.workspaceState.get<string>(
276-
'last-completion-instructions'
277-
) || ''
278-
completion_instructions = await vscode.window.showInputBox({
279-
placeHolder: 'Enter completion instructions',
280-
prompt: 'E.g. "Include explanatory comments".',
281-
value: last_value
282-
})
283-
284-
if (completion_instructions === undefined) return
285-
286-
await params.context.workspaceState.update(
287-
'last-completion-instructions',
288-
completion_instructions || ''
289-
)
290-
}
258+
export const handle_code_completion = async (
259+
panel_provider: PanelProvider,
260+
message: CodeCompletionMessage
261+
): Promise<void> => {
262+
const api_providers_manager = new ModelProvidersManager(
263+
panel_provider.context
264+
)
265+
const completion_instructions = panel_provider.code_completion_instructions
291266

292267
const config_result = await get_code_completion_config(
293268
api_providers_manager,
294-
params.show_quick_pick,
295-
params.context,
296-
params.config_id,
297-
params.panel_provider
269+
message.use_quick_pick,
270+
panel_provider.context,
271+
message.config_id,
272+
panel_provider
298273
)
299274

300275
if (!config_result) {
301276
return
302277
}
303278

279+
panel_provider.send_message({ command: 'FOCUS_PROMPT_FIELD' })
280+
304281
const { provider, config: code_completions_config } = config_result
305282

306283
if (!code_completions_config.provider_name) {
307284
vscode.window.showErrorMessage(
308285
dictionary.error_message.API_PROVIDER_NOT_SPECIFIED_FOR_CODE_COMPLETIONS
309286
)
310287
Logger.warn({
311-
function_name: 'perform_code_completion',
288+
function_name: 'handle_code_completion',
312289
message: 'API provider is not specified for Code Completions tool.'
313290
})
314291
return
@@ -317,7 +294,7 @@ const perform_code_completion = async (params: {
317294
dictionary.error_message.MODEL_NOT_SPECIFIED_FOR_CODE_COMPLETIONS
318295
)
319296
Logger.warn({
320-
function_name: 'perform_code_completion',
297+
function_name: 'handle_code_completion',
321298
message: 'Model is not specified for Code Completions tool.'
322299
})
323300
return
@@ -331,7 +308,7 @@ const perform_code_completion = async (params: {
331308
dictionary.error_message.BUILT_IN_PROVIDER_NOT_FOUND(provider.name)
332309
)
333310
Logger.warn({
334-
function_name: 'perform_code_completion',
311+
function_name: 'handle_code_completion',
335312
message: `Built-in provider "${provider.name}" not found.`
336313
})
337314
return
@@ -368,8 +345,8 @@ const perform_code_completion = async (params: {
368345
)
369346

370347
const files_collector = new FilesCollector(
371-
params.file_tree_provider,
372-
params.open_editors_provider
348+
panel_provider.workspace_provider,
349+
panel_provider.open_editors_provider
373350
)
374351

375352
const context_text = await files_collector.collect_files({
@@ -408,12 +385,12 @@ const perform_code_completion = async (params: {
408385

409386
let error_occurred = false
410387

411-
const promises = Array.from({ length: params.invocation_count }).map(
388+
const promises = Array.from({ length: message.invocation_count }).map(
412389
async () => {
413390
const request_id = randomUUID()
414391

415392
try {
416-
const result = await params.panel_provider.api_manager.get({
393+
const result = await panel_provider.api_manager.get({
417394
endpoint_url,
418395
api_key: provider.api_key,
419396
body,
@@ -444,7 +421,7 @@ const perform_code_completion = async (params: {
444421
return
445422
}
446423
Logger.error({
447-
function_name: 'perform_code_completion',
424+
function_name: 'handle_code_completion',
448425
message: 'code completion error',
449426
data: err
450427
})
@@ -463,20 +440,3 @@ const perform_code_completion = async (params: {
463440
vscode.window.showWarningMessage(dictionary.warning_message.NO_EDITOR_OPEN)
464441
}
465442
}
466-
467-
export const handle_code_completion = async (
468-
panel_provider: PanelProvider,
469-
message: CodeCompletionMessage
470-
): Promise<void> => {
471-
perform_code_completion({
472-
file_tree_provider: panel_provider.workspace_provider,
473-
open_editors_provider: panel_provider.open_editors_provider,
474-
context: panel_provider.context,
475-
with_completion_instructions: false,
476-
show_quick_pick: message.use_quick_pick,
477-
completion_instructions: panel_provider.code_completion_instructions,
478-
config_id: message.config_id,
479-
panel_provider: panel_provider,
480-
invocation_count: message.invocation_count
481-
})
482-
}

‎packages/vscode/src/views/panel/backend/message-handlers/handle-edit-context.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ const get_edit_context_config = async (
174174
quick_pick.activeItems = [last_selected_item]
175175
} else if (items.length > 0) {
176176
const first_selectable = items.find(
177-
(i) => i.kind !== vscode.QuickPickItemKind.Separator
177+
(i) => i.kind != vscode.QuickPickItemKind.Separator
178178
)
179179
if (first_selectable) {
180180
quick_pick.activeItems = [first_selectable]
@@ -359,6 +359,8 @@ export const handle_edit_context = async (
359359
return
360360
}
361361

362+
panel_provider.send_message({ command: 'FOCUS_PROMPT_FIELD' })
363+
362364
const { provider, config: edit_context_config } = config_result
363365

364366
let endpoint_url = ''

‎packages/vscode/src/views/panel/backend/message-handlers/handle-send-prompt.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ export const handle_send_prompt = async (params: {
271271
command: 'SHOW_AUTO_CLOSING_MODAL',
272272
title: 'Opened in the connected browser'
273273
})
274+
params.panel_provider.send_message({ command: 'FOCUS_PROMPT_FIELD' })
274275
}
275276

276277
async function show_preset_quick_pick(params: {

‎packages/vscode/src/views/panel/backend/message-handlers/handle-show-history-quick-pick.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export const handle_show_history_quick_pick = async (
148148
}
149149
} else if (pinned_history.length > 0) {
150150
const first_pinned_item_index = items.findIndex(
151-
(item) => item.kind !== vscode.QuickPickItemKind.Separator
151+
(item) => item.kind != vscode.QuickPickItemKind.Separator
152152
)
153153
if (first_pinned_item_index != -1) {
154154
quick_pick.activeItems = [items[first_pinned_item_index]]

0 commit comments

Comments
 (0)