Skip to content

Commit 0b3a0a9

Browse files
committed
Support nested snippet fields
FIX: Improve handling of nested fields in snippets. Closes codemirror/dev#1607
1 parent ab0a899 commit 0b3a0a9

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/snippet.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ 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)) {
51+
while (m = /[#$]\{(?:(\d+)(?::([^{}]*))?|((?:\\[{}]|[^{}])*))\}/.exec(line)) {
5252
let seq = m[1] ? +m[1] : null, rawName = m[2] || m[3] || "", found = -1
5353
let name = rawName.replace(/\\[{}]/g, m => m[1])
5454
for (let i = 0; i < fields.length; i++) {
@@ -61,6 +61,11 @@ class Snippet {
6161
found = i
6262
for (let pos of positions) if (pos.field >= found) pos.field++
6363
}
64+
for (let pos of positions) if (pos.line == lines.length && pos.from > m.index) {
65+
let snip = m[2] ? 3 + (m[1] || "").length : 2
66+
pos.from -= snip
67+
pos.to -= snip
68+
}
6469
positions.push(new FieldPos(found, lines.length, m.index, m.index + name.length))
6570
line = line.slice(0, m.index) + rawName + line.slice(m.index + m[0].length)
6671
}
@@ -92,7 +97,7 @@ class ActiveSnippet {
9297

9398
constructor(readonly ranges: readonly FieldRange[],
9499
readonly active: number) {
95-
this.deco = Decoration.set(ranges.map(r => (r.from == r.to ? fieldMarker : fieldRange).range(r.from, r.to)))
100+
this.deco = Decoration.set(ranges.map(r => (r.from == r.to ? fieldMarker : fieldRange).range(r.from, r.to)), true)
96101
}
97102

98103
map(changes: ChangeDesc) {

0 commit comments

Comments
 (0)