Skip to content

Commit ad236af

Browse files
committed
Refactor code completion handling to process the raw model response directly as a chat response, enabling support for completions embedded within surrounding text and simplifying response parsing logic
1 parent 7ffa458 commit ad236af

5 files changed

Lines changed: 20 additions & 27 deletions

File tree

‎packages/vscode/src/commands/apply-chat-response-command/response-processor.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@ export const process_chat_response = async (
7777
is_single_root_folder_workspace
7878
})
7979

80-
if (clipboard_items.length == 1 && clipboard_items[0].type == 'completion') {
81-
const completion = clipboard_items[0]
80+
if (clipboard_items.some((item) => item.type == 'completion')) {
81+
const completion = clipboard_items.find(
82+
(item) => item.type == 'completion'
83+
)!
8284
const workspace_map = new Map<string, string>()
8385
vscode.workspace.workspaceFolders!.forEach((folder) => {
8486
workspace_map.set(folder.name, folder.uri.fsPath)

‎packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/clipboard-parser.spec.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ describe('clipboard-parser', () => {
584584
expect(result).toHaveLength(3)
585585
expect(result[0]).toMatchObject({
586586
type: 'text',
587-
content: 'Some text before.'
587+
content: load_test_case_file(test_case, '1-text.txt')
588588
})
589589
expect(result[1]).toMatchObject({
590590
type: 'completion',
@@ -595,7 +595,7 @@ describe('clipboard-parser', () => {
595595
})
596596
expect(result[2]).toMatchObject({
597597
type: 'text',
598-
content: 'Some text after.'
598+
content: load_test_case_file(test_case, '2-text.txt')
599599
})
600600
})
601601

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Some text before.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Some text after.

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

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import * as vscode from 'vscode'
22
import axios from 'axios'
3-
import he from 'he'
43
import { make_api_request } from '@/utils/make-api-request'
5-
import { code_completion_instructions } from '@/constants/instructions'
4+
import { chat_code_completion_instructions } from '@/constants/instructions'
65
import { FilesCollector } from '@/utils/files-collector'
76
import {
87
ModelProvidersManager,
@@ -315,6 +314,13 @@ const perform_code_completion = async (params: {
315314
new vscode.Range(position, document.positionAt(document.getText().length))
316315
)
317316

317+
const relative_path = vscode.workspace.asRelativePath(document.uri)
318+
const main_instructions = chat_code_completion_instructions(
319+
relative_path,
320+
position.line,
321+
position.character
322+
)
323+
318324
const files_collector = new FilesCollector(
319325
params.file_tree_provider,
320326
params.open_editors_provider
@@ -325,17 +331,15 @@ const perform_code_completion = async (params: {
325331
})
326332

327333
const payload = {
328-
before: `<files>\n${context_text}<file path="${vscode.workspace.asRelativePath(
329-
document.uri
330-
)}">\n<![CDATA[\n${text_before_cursor}`,
334+
before: `<files>\n${context_text}<file path="${relative_path}">\n<![CDATA[\n${text_before_cursor}`,
331335
after: `${text_after_cursor}\n]]>\n</file>\n</files>`
332336
}
333337

334-
const content = `${code_completion_instructions}\n${payload.before}${
338+
const content = `${main_instructions}\n${payload.before}${
335339
completion_instructions
336340
? `<missing_text>${completion_instructions}</missing_text>`
337341
: '<missing_text>'
338-
}${payload.after}\n${code_completion_instructions}`
342+
}${payload.after}\n${main_instructions}`
339343

340344
const messages = [
341345
{
@@ -393,22 +397,7 @@ const perform_code_completion = async (params: {
393397
})
394398

395399
if (result) {
396-
const match = result.response.match(
397-
/<replacement>([\s\S]*?)<\/replacement>/i
398-
)
399-
if (match && match[1]) {
400-
let decoded_completion = he.decode(match[1].trim())
401-
decoded_completion = decoded_completion
402-
.replace(/<!\[CDATA\[/g, '')
403-
.replace(/\]\]>/g, '')
404-
.trim()
405-
406-
const new_content =
407-
text_before_cursor + decoded_completion + text_after_cursor
408-
409-
const relative_path = vscode.workspace.asRelativePath(document.uri)
410-
response_for_apply = `\`\`\`\n// ${relative_path}\n${new_content}\n\`\`\``
411-
}
400+
response_for_apply = result.response
412401
}
413402
} catch (err: any) {
414403
if (axios.isCancel(err)) {

0 commit comments

Comments
 (0)