Skip to content

Commit 96a271b

Browse files
committed
Handle escaped braces in snippet field content
FIX: Allow backslash-escaped closing braces inside snippet field names/content. See https://discuss.codemirror.net/t/inserting-literal-via-snippets/8136
1 parent 5f58ba2 commit 96a271b

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/snippet.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ class Snippet {
4848
let fields: {seq: number | null, name: string}[] = []
4949
let lines = [], positions: FieldPos[] = [], m
5050
for (let line of template.split(/\r\n?|\n/)) {
51-
while (m = /[#$]\{(?:(\d+)(?::([^}]*))?|([^}]*))\}/.exec(line)) {
52-
let seq = m[1] ? +m[1] : null, name = m[2] || m[3] || "", found = -1
51+
while (m = /[#$]\{(?:(\d+)(?::([^}]*))?|((?:\\[{}]|[^}])*))\}/.exec(line)) {
52+
let seq = m[1] ? +m[1] : null, rawName = m[2] || m[3] || "", found = -1
53+
let name = rawName.replace(/\\[{}]/g, m => m[1])
5354
for (let i = 0; i < fields.length; i++) {
5455
if (seq != null ? fields[i].seq == seq : name ? fields[i].name == name : false) found = i
5556
}
@@ -61,7 +62,7 @@ class Snippet {
6162
for (let pos of positions) if (pos.field >= found) pos.field++
6263
}
6364
positions.push(new FieldPos(found, lines.length, m.index, m.index + name.length))
64-
line = line.slice(0, m.index) + name + line.slice(m.index + m[0].length)
65+
line = line.slice(0, m.index) + rawName + line.slice(m.index + m[0].length)
6566
}
6667
line = line.replace(/\\([{}])/g, (_, brace, index) => {
6768
for (let pos of positions) if (pos.line == lines.length && pos.from > index) {

0 commit comments

Comments
 (0)