Skip to content

Commit 4f0f3b6

Browse files
committed
Fix backslash-unescaping of braces in snippets
FIX: Fix a bug where multiple backslashes before a brace in a snippet were all removed. See https://discuss.codemirror.net/t/inserting-literal-via-snippets/8136
1 parent 0d719cc commit 4f0f3b6

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/snippet.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Snippet {
4646

4747
static parse(template: string) {
4848
let fields: {seq: number | null, name: string}[] = []
49-
let lines = [], positions = [], m
49+
let lines = [], positions: FieldPos[] = [], m
5050
for (let line of template.split(/\r\n?|\n/)) {
5151
while (m = /[#$]\{(?:(\d+)(?::([^}]*))?|([^}]*))\}/.exec(line)) {
5252
let seq = m[1] ? +m[1] : null, name = m[2] || m[3] || "", found = -1
@@ -63,13 +63,13 @@ class Snippet {
6363
positions.push(new FieldPos(found, lines.length, m.index, m.index + name.length))
6464
line = line.slice(0, m.index) + name + line.slice(m.index + m[0].length)
6565
}
66-
for (let esc; esc = /\\([{}])/.exec(line);) {
67-
line = line.slice(0, esc.index) + esc[1] + line.slice(esc.index + esc[0].length)
68-
for (let pos of positions) if (pos.line == lines.length && pos.from > esc.index) {
66+
line = line.replace(/\\([{}])/g, (_, brace, index) => {
67+
for (let pos of positions) if (pos.line == lines.length && pos.from > index) {
6968
pos.from--
7069
pos.to--
7170
}
72-
}
71+
return brace
72+
})
7373
lines.push(line)
7474
}
7575
return new Snippet(lines, positions)

0 commit comments

Comments
 (0)