Skip to content

Commit d3e3310

Browse files
committed
fix merging snippets
increase nesting level of the snippet based on how many snippets need to be merged instead an arbitrary value of 10 and reorder the indexes afterwards to avoid floating point problems where nestinglevel< 1e-15 would be added upon index > 1, where the index then wouldn't change.
1 parent e59f82f commit d3e3310

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/vs/editor/contrib/snippet/browser/snippetSession.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,13 @@ export class OneSnippet {
289289
merge(others: OneSnippet[]): void {
290290

291291
const model = this._editor.getModel();
292-
this._nestingLevel *= 10;
292+
const nestingLevel = Math.max(8, ...others.map((o, i) => {
293+
if (i < this._placeholderGroups[this._placeholderGroupsIdx].length) {
294+
return o._snippet.placeholderInfo.last!.index || 8;
295+
}
296+
return 8;
297+
})) + 2;
298+
this._nestingLevel *= nestingLevel;
293299

294300
this._editor.changeDecorations(accessor => {
295301

@@ -309,9 +315,9 @@ export class OneSnippet {
309315

310316
for (const nestedPlaceholder of nested._snippet.placeholderInfo.all) {
311317
if (nestedPlaceholder.isFinalTabstop) {
312-
nestedPlaceholder.index = placeholder.index + ((indexLastPlaceholder + 1) / this._nestingLevel);
318+
nestedPlaceholder.index = placeholder.index + ((indexLastPlaceholder + 1) / nestingLevel);
313319
} else {
314-
nestedPlaceholder.index = placeholder.index + (nestedPlaceholder.index / this._nestingLevel);
320+
nestedPlaceholder.index = placeholder.index + (nestedPlaceholder.index / nestingLevel);
315321
}
316322
}
317323
this._snippet.replace(placeholder, nested._snippet.children);
@@ -339,6 +345,15 @@ export class OneSnippet {
339345
// Last, re-create the placeholder groups by sorting placeholders by their index.
340346
this._placeholderGroups = groupBy(this._snippet.placeholders, Placeholder.compareByIndex);
341347
});
348+
// Reorder indexes to only use ints to avoid floating point issues
349+
for (const [i, placeholderGroup] of this._placeholderGroups.entries()) {
350+
for (const placeholder of placeholderGroup) {
351+
if (placeholder.isFinalTabstop) {
352+
break;
353+
}
354+
placeholder.index = i + 1;
355+
}
356+
}
342357
}
343358

344359
getEnclosingRange(): Range | undefined {

0 commit comments

Comments
 (0)