Skip to content

Commit

Permalink
Fix multiple placeholders with the same name
Browse files Browse the repository at this point in the history
  • Loading branch information
pokey committed Apr 25, 2022
1 parent e6eb3f9 commit e389949
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/util/snippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export function transformSnippetVariables(
placeholderName?: string | null,
substitutions?: Record<string, string>
) {
var placeholderIndex = getMaxPlaceholderIndex(parsedSnippet) + 1;
let nextPlaceholderIndex = getMaxPlaceholderIndex(parsedSnippet) + 1;
const placeholderIndexMap: Record<string, number> = {};

parsedSnippet.walk((candidate) => {
if (candidate instanceof Variable) {
Expand All @@ -36,7 +37,13 @@ export function transformSnippetVariables(
new Text(substitutions[candidate.name]),
]);
} else if (!KnownSnippetVariableNames[candidate.name]) {
const placeholder = new Placeholder(placeholderIndex++);
let placeholderIndex: number;
if (candidate.name in placeholderIndexMap) {
placeholderIndex = placeholderIndexMap[candidate.name];
} else {
placeholderIndex = nextPlaceholderIndex++;
}
const placeholder = new Placeholder(placeholderIndex);
candidate.children.forEach((child) => placeholder.appendChild(child));
candidate.parent.replace(candidate, [placeholder]);
}
Expand Down

0 comments on commit e389949

Please sign in to comment.