Skip to content

Commit f6826e9

Browse files
committed
Refactor the code completion parser and instructions to use a new, standardized markdown heading format for file path and cursor position
1 parent 58da301 commit f6826e9

16 files changed

Lines changed: 35 additions & 35 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ describe('clipboard-parser', () => {
792792
file_path: 'src/index.ts',
793793
line: 25,
794794
character: 5,
795-
content: 'console.log("completion");'
795+
content: load_test_case_file(test_case, '1-file.txt')
796796
})
797797
})
798798

@@ -814,11 +814,11 @@ describe('clipboard-parser', () => {
814814
file_path: 'src/index.ts',
815815
line: 25,
816816
character: 5,
817-
content: 'console.log("completion");'
817+
content: load_test_case_file(test_case, '2-file.txt')
818818
})
819819
expect(result[2]).toMatchObject({
820820
type: 'text',
821-
content: load_test_case_file(test_case, '2-text.txt')
821+
content: load_test_case_file(test_case, '3-text.txt')
822822
})
823823
})
824824

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import {
88
const extract_path_and_position = (
99
line: string
1010
): { path: string; line: number; character: number } | null => {
11-
const path_pos_regex = /\/\/\s*"?([^"<>\s?*|:]+)"?\s+(\d+):(\d+)/
11+
const path_pos_regex =
12+
/###\s+Code completion:\s+`?([^`]+)`?\s+\((\d+):(\d+)\)/
1213

1314
const match = line.match(path_pos_regex)
1415

@@ -57,15 +58,25 @@ export const parse_code_completion = (params: {
5758
code_block_end_index = lines.length - 1
5859
}
5960

60-
const first_line_of_block = lines[code_block_start_index + 1]
61-
const completion_info = extract_path_and_position(first_line_of_block)
61+
let header_index = -1
62+
let completion_info = null
63+
64+
for (let k = code_block_start_index - 1; k >= 0; k--) {
65+
if (lines[k].trim() === '') continue
66+
const info = extract_path_and_position(lines[k])
67+
if (info) {
68+
header_index = k
69+
completion_info = info
70+
}
71+
break
72+
}
6273

6374
if (!completion_info) {
6475
return null
6576
}
6677

6778
const results: (CompletionItem | TextItem)[] = []
68-
const text_before = lines.slice(0, code_block_start_index).join('\n').trim()
79+
const text_before = lines.slice(0, header_index).join('\n').trim()
6980
if (text_before) {
7081
results.push({ type: 'text', content: text_before })
7182
}
@@ -76,7 +87,7 @@ export const parse_code_completion = (params: {
7687
})
7788

7889
const content_lines = lines.slice(
79-
code_block_start_index + 2,
90+
code_block_start_index + 1,
8091
code_block_end_index
8192
)
8293

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("completion");

packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/test-cases/code-completion-with-text/2-text.txt renamed to packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/test-cases/code-completion-with-text/3-text.txt

File renamed without changes.

‎packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/test-cases/code-completion-with-text/code-completion-with-text.txt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
Some text before.
22

3+
### Code completion: `src/index.ts` (25:5)
4+
35
```typescript
4-
// src/index.ts 25:5
56
console.log("completion");
67
```
78

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("completion");
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1+
### Code completion: `src/index.ts` (25:5)
2+
13
```typescript
2-
// src/index.ts 25:5
34
console.log("completion");
45
```

‎packages/vscode/src/commands/apply-context-command/sources/workspace-state-source.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@ export async function handle_workspace_state_source(
251251
internal_contexts
252252
)
253253
vscode.window.showInformationMessage(
254-
// NOSONAR
255254
dictionary.information_message.RESTORED_CONTEXT(
256255
deleted_context_name
257256
)

‎packages/vscode/src/commands/checkpoints-command/checkpoints-command.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,6 @@ export const checkpoints_command = (params: {
224224
)
225225
if (!temp_checkpoint) {
226226
vscode.window.showErrorMessage(
227-
// NOSONAR
228227
dictionary.error_message
229228
.COULD_NOT_FIND_TEMP_CHECKPOINT_TO_REVERT
230229
)
@@ -274,7 +273,6 @@ export const checkpoints_command = (params: {
274273
if (confirmation == 'Clear All') {
275274
await clear_all_checkpoints(params.context, params.panel_provider)
276275
vscode.window.showInformationMessage(
277-
// NOSONAR
278276
dictionary.information_message.ALL_CHECKPOINTS_CLEARED
279277
)
280278
}
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
export const code_completion_instructions =
2-
'Find correct replacement for the <missing_text> symbol. Respond with replacement text within "replacement" XML tags, without explanations or any other text.'
2+
'Find correct replacement for the <missing_text> symbol. Respond with replacement text within "replacement" XML tags, without explanations or any other text.\nExample:\n<replacement>!== undefined</replacement>\n'
33

44
export const code_completion_instructions_for_panel = (
55
file_path: string,
66
row: number,
77
column: number
88
) =>
9-
`Find correct replacement text for the <missing_text> symbol. Your response must begin with a Markdown Code Block containing the replacement text, followed by an explanation. The first line of the Markdown Code Block must be: "// ${file_path} ${
9+
`Find correct replacement text for the <missing_text> symbol.
10+
<system>
11+
Your response must begin with a markdown heading identifying the file and the cursor position, followed by a markdown code block containing the replacement text, followed by a brief explanation. The heading must be: "### Code completion: \`${file_path}\` (${
1012
row + 1
1113
}:${
1214
column + 1
13-
}". Always refer to the symbol "<missing_text>" as "cursor position" and "replacement" as "completion".`
15+
})". Always refer to the symbol "<missing_text>" as "cursor position" and "replacement" as "completion". Example:\n### Code completion: \`src/index.ts\` (25:5)
16+
\`\`\`typescript
17+
!== undefined
18+
\`\`\`
19+
The variable is possibly not defined.
20+
</system>`
1421

1522
export const intelligent_update_instructions =
1623
"Refactor the file according to the attached changes without explanations or any other text. Print the file in full because I have a disability which means I can't type and need to be able to just copy and paste."

0 commit comments

Comments
 (0)