make Placeholder#index a number, fixes #28185

This commit is contained in:
Johannes Rieken 2017-06-08 15:00:32 +02:00
parent 61a1d9abdc
commit 0117f311aa
4 changed files with 20 additions and 12 deletions

View file

@ -187,12 +187,12 @@ export class Placeholder extends Marker {
} }
} }
constructor(public index: string = '', children: Marker[]) { constructor(public index: number, children: Marker[]) {
super(); super();
this.children = children; this.children = children;
} }
get isFinalTabstop() { get isFinalTabstop() {
return this.index === '0'; return this.index === 0;
} }
toString() { toString() {
return Marker.toString(this.children); return Marker.toString(this.children);
@ -356,7 +356,7 @@ export class SnippetParser {
// * fill in default for empty placeHolders // * fill in default for empty placeHolders
// * compact sibling Text markers // * compact sibling Text markers
function walk(marker: Marker[], placeholderDefaultValues: Map<string, Marker[]>) { function walk(marker: Marker[], placeholderDefaultValues: Map<number, Marker[]>) {
for (let i = 0; i < marker.length; i++) { for (let i = 0; i < marker.length; i++) {
const thisMarker = marker[i]; const thisMarker = marker[i];
@ -385,16 +385,16 @@ export class SnippetParser {
} }
} }
const placeholderDefaultValues = new Map<string, Marker[]>(); const placeholderDefaultValues = new Map<number, Marker[]>();
walk(marker, placeholderDefaultValues); walk(marker, placeholderDefaultValues);
if ( if (
!placeholderDefaultValues.has('0') && // there is no final tabstop !placeholderDefaultValues.has(0) && // there is no final tabstop
(insertFinalTabstop && placeholderDefaultValues.size > 0 || enforceFinalTabstop) (insertFinalTabstop && placeholderDefaultValues.size > 0 || enforceFinalTabstop)
) { ) {
// the snippet uses placeholders but has no // the snippet uses placeholders but has no
// final tabstop defined -> insert at the end // final tabstop defined -> insert at the end
marker.push(new Placeholder('0', [])); marker.push(new Placeholder(0, []));
} }
return marker; return marker;
@ -433,7 +433,7 @@ export class SnippetParser {
if (this._accept(TokenType.VariableName) || this._accept(TokenType.Int)) { if (this._accept(TokenType.VariableName) || this._accept(TokenType.Int)) {
// $FOO, $123 // $FOO, $123
const idOrName = this._scanner.tokenText(this._prevToken); const idOrName = this._scanner.tokenText(this._prevToken);
marker.push(/^\d+$/.test(idOrName) ? new Placeholder(idOrName, []) : new Variable(idOrName, [])); marker.push(/^\d+$/.test(idOrName) ? new Placeholder(Number(idOrName), []) : new Variable(idOrName, []));
return true; return true;
} else if (this._accept(TokenType.CurlyOpen)) { } else if (this._accept(TokenType.CurlyOpen)) {
@ -451,7 +451,7 @@ export class SnippetParser {
if (this._accept(TokenType.CurlyClose)) { if (this._accept(TokenType.CurlyClose)) {
const idOrName = Marker.toString(name); const idOrName = Marker.toString(name);
marker.push(/^\d+$/.test(idOrName) ? new Placeholder(idOrName, children) : new Variable(idOrName, children)); marker.push(/^\d+$/.test(idOrName) ? new Placeholder(Number(idOrName), children) : new Variable(idOrName, children));
return true; return true;
} }

View file

@ -173,9 +173,9 @@ export class OneSnippet {
// through the placeholders in the correct order // through the placeholders in the correct order
for (const nestedPlaceholder of nested._snippet.placeholders) { for (const nestedPlaceholder of nested._snippet.placeholders) {
if (nestedPlaceholder.isFinalTabstop) { if (nestedPlaceholder.isFinalTabstop) {
nestedPlaceholder.index = `${placeholder.index}.${nested._snippet.placeholders.length}`; nestedPlaceholder.index = placeholder.index + (nested._snippet.placeholders.length / 10);
} else { } else {
nestedPlaceholder.index = `${placeholder.index}.${nestedPlaceholder.index}`; nestedPlaceholder.index = placeholder.index + (nestedPlaceholder.index / 10);
} }
} }
this._snippet.replace(placeholder, nested._snippet.children); this._snippet.replace(placeholder, nested._snippet.children);

View file

@ -381,4 +381,12 @@ suite('SnippetParser', () => {
assert.equal(snippet.text, 'aaabbbdddeee'); assert.equal(snippet.text, 'aaabbbdddeee');
assert.equal(snippet.placeholders.length, 3); assert.equal(snippet.placeholders.length, 3);
}); });
test('Snippet order for placeholders, #28185', function () {
const _10 = new Placeholder(10, []);
const _2 = new Placeholder(2, []);
assert.equal(Placeholder.compareByIndex(_10, _2), 1);
});
}); });

View file

@ -120,7 +120,7 @@ export class EditorAccessor implements emmet.Editor {
// find highest placeholder index // find highest placeholder index
walk(marker, candidate => { walk(marker, candidate => {
if (candidate instanceof Placeholder) { if (candidate instanceof Placeholder) {
let index = Number(candidate.index); let index = candidate.index;
if (index > maxIndex) { if (index > maxIndex) {
maxIndex = index; maxIndex = index;
} }
@ -132,7 +132,7 @@ export class EditorAccessor implements emmet.Editor {
walk(marker, candidate => { walk(marker, candidate => {
if (candidate instanceof Placeholder) { if (candidate instanceof Placeholder) {
if (candidate.isFinalTabstop) { if (candidate.isFinalTabstop) {
candidate.index = String(++maxIndex); candidate.index = ++maxIndex;
} }
} }
return true; return true;