Skip to content

Commit 10b7892

Browse files
committed
Improve regex escaping for prompt template variable replacement
1 parent 87ccceb commit 10b7892

1 file changed

Lines changed: 6 additions & 9 deletions

File tree

packages/vscode/src/view/backend/message-handlers/handle-show-prompt-template-quick-pick.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ type PromptTemplate = {
88
template: string
99
}
1010

11+
const ADD_NEW_TEMPLATE_LABEL = '$(add) Add a new prompt template'
12+
1113
export const handle_show_prompt_template_quick_pick = async (
1214
provider: ViewProvider
1315
): Promise<void> => {
@@ -43,23 +45,18 @@ export const handle_show_prompt_template_quick_pick = async (
4345
config.get<PromptTemplate[]>(prompt_templates_key, []) || []
4446

4547
const set_instructions = async (text: string) => {
46-
let instruction_key: string
4748
switch (mode) {
4849
case 'ask':
4950
provider.ask_instructions = text
50-
instruction_key = 'ask-instructions'
5151
break
5252
case 'edit-context':
5353
provider.edit_instructions = text
54-
instruction_key = 'edit-instructions'
5554
break
5655
case 'no-context':
5756
provider.no_context_instructions = text
58-
instruction_key = 'no-context-instructions'
5957
break
6058
case 'code-completions':
6159
provider.code_completion_instructions = text
62-
instruction_key = 'code-completions-instructions'
6360
break
6461
default:
6562
return
@@ -105,7 +102,7 @@ export const handle_show_prompt_template_quick_pick = async (
105102
index?: number
106103
})[] = [
107104
{
108-
label: '$(add) Add new template'
105+
label: ADD_NEW_TEMPLATE_LABEL
109106
}
110107
]
111108
if (templates.length > 0) {
@@ -270,7 +267,7 @@ export const handle_show_prompt_template_quick_pick = async (
270267
return
271268
}
272269

273-
if (selected_template.label == '$(add) Add new template') {
270+
if (selected_template.label == ADD_NEW_TEMPLATE_LABEL) {
274271
is_editing_template = true
275272
const name = await vscode.window.showInputBox({
276273
prompt: 'Enter an optional name for the template'
@@ -334,14 +331,14 @@ export const handle_show_prompt_template_quick_pick = async (
334331
if (value) {
335332
const double_regex = new RegExp(
336333
`\\{\\{\\s*${variable.replace(
337-
/[.*+?^${}()|[\]\\]/g,
334+
/[.*+?^${}()|[\\]\\\\]/g,
338335
'\\$&'
339336
)}\\s*\\}\\}`,
340337
'g'
341338
)
342339
const single_regex = new RegExp(
343340
`\\{\\s*${variable.replace(
344-
/[.*+?^${}()|[\]\\]/g,
341+
/[.*+?^${}()|[\\]\\\\]/g,
345342
'\\$&'
346343
)}\\s*\\}`,
347344
'g'

0 commit comments

Comments
 (0)