Skip to content

Commit 4e57546

Browse files
committed
Modify prompt generation to incorporate selected text based on whether the @selection placeholder is present
1 parent 8e108f3 commit 4e57546

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

packages/vscode/src/utils/replace-selection-placeholder/replace-selection-placeholder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ export const replace_selection_placeholder = (instruction: string): string => {
2020

2121
const replacement_text = `\n\`${current_file_path}\`\n\`\`\`\n${selected_text}\n\`\`\`\n`
2222

23-
return instruction.replace(/@selection/g, replacement_text)
23+
return instruction.replace(/\s*@selection\s*/g, replacement_text)
2424
}

packages/vscode/src/view/backend/message-handlers/handle-send-prompt.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,27 @@ export const handle_send_prompt = async (
7171
} else if (!provider.is_code_completions_mode) {
7272
if (!provider.instructions) return
7373

74+
const editor = vscode.window.activeTextEditor
75+
const document = editor?.document
76+
const current_file_path = document
77+
? vscode.workspace.asRelativePath(document.uri)
78+
: ''
79+
80+
let base_instructions = provider.instructions
81+
82+
if (editor && !editor.selection.isEmpty) {
83+
if (base_instructions.includes('@selection')) {
84+
base_instructions = replace_selection_placeholder(base_instructions)
85+
} else {
86+
const selected_text = editor.document.getText(editor.selection)
87+
base_instructions = `\`${current_file_path}\`\n\`\`\`\n${selected_text}\n\`\`\`\n${base_instructions}`
88+
}
89+
}
90+
7491
const context_text = await files_collector.collect_files({
7592
active_path
7693
})
7794

78-
let base_instructions = provider.instructions
79-
base_instructions = replace_selection_placeholder(base_instructions)
80-
8195
const config = vscode.workspace.getConfiguration('codeWebChat')
8296
const edit_format_instructions = config.get<string>(
8397
`editFormatInstructions${

0 commit comments

Comments
 (0)