Skip to content

Commit 669b5de

Browse files
committed
Fix multiple placeholders with the same name
1 parent 70df673 commit 669b5de

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/util/snippet.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ export function transformSnippetVariables(
2222
placeholderName?: string | null,
2323
substitutions?: Record<string, string>
2424
) {
25-
var placeholderIndex = getMaxPlaceholderIndex(parsedSnippet) + 1;
25+
let nextPlaceholderIndex = getMaxPlaceholderIndex(parsedSnippet) + 1;
26+
const placeholderIndexMap: Record<string, number> = {};
2627

2728
parsedSnippet.walk((candidate) => {
2829
if (candidate instanceof Variable) {
@@ -36,7 +37,13 @@ export function transformSnippetVariables(
3637
new Text(substitutions[candidate.name]),
3738
]);
3839
} else if (!KnownSnippetVariableNames[candidate.name]) {
39-
const placeholder = new Placeholder(placeholderIndex++);
40+
let placeholderIndex: number;
41+
if (candidate.name in placeholderIndexMap) {
42+
placeholderIndex = placeholderIndexMap[candidate.name];
43+
} else {
44+
placeholderIndex = nextPlaceholderIndex++;
45+
}
46+
const placeholder = new Placeholder(placeholderIndex);
4047
candidate.children.forEach((child) => placeholder.appendChild(child));
4148
candidate.parent.replace(candidate, [placeholder]);
4249
}

0 commit comments

Comments
 (0)