mirror of
https://github.com/microsoft/vscode.git
synced 2026-05-24 17:31:37 +01:00
debt - remove last bit of old snippet syntax
This commit is contained in:
@@ -332,20 +332,14 @@ export class SnippetParser {
|
||||
}
|
||||
|
||||
static parse(template: string, enforceFinalTabstop?: boolean): TextmateSnippet {
|
||||
const marker = new SnippetParser(true, false).parse(template, true, enforceFinalTabstop);
|
||||
const marker = new SnippetParser().parse(template, true, enforceFinalTabstop);
|
||||
return new TextmateSnippet(marker);
|
||||
}
|
||||
|
||||
private _enableTextMate: boolean;
|
||||
private _enableInternal: boolean;
|
||||
private _scanner = new Scanner();
|
||||
private _token: Token;
|
||||
private _prevToken: Token;
|
||||
|
||||
constructor(enableTextMate: boolean = true, enableInternal: boolean = true) {
|
||||
this._enableTextMate = enableTextMate;
|
||||
this._enableInternal = enableInternal;
|
||||
}
|
||||
|
||||
text(value: string): string {
|
||||
return Marker.toString(this.parse(value));
|
||||
@@ -415,18 +409,10 @@ export class SnippetParser {
|
||||
return false;
|
||||
}
|
||||
|
||||
private _return(token: Token): void {
|
||||
this._prevToken = undefined;
|
||||
this._token = token;
|
||||
this._scanner.pos = token.pos + token.len;
|
||||
}
|
||||
|
||||
private _parseAny(marker: Marker[]): boolean {
|
||||
if (this._parseEscaped(marker)) {
|
||||
return true;
|
||||
} else if (this._enableInternal && this._parseInternal(marker)) {
|
||||
return true;
|
||||
} else if (this._enableTextMate && this._parseTM(marker)) {
|
||||
} else if (this._parseTM(marker)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -491,69 +477,9 @@ export class SnippetParser {
|
||||
return false;
|
||||
}
|
||||
|
||||
private _parseInternal(marker: Marker[]): boolean {
|
||||
if (this._accept(TokenType.CurlyOpen)) {
|
||||
|
||||
if (!this._accept(TokenType.CurlyOpen)) {
|
||||
this._return(this._prevToken);
|
||||
return false;
|
||||
}
|
||||
|
||||
// {{name:children}}, {{name}}, {{name:}}
|
||||
let name: Marker[] = [];
|
||||
let children: Marker[] = [];
|
||||
let target = name;
|
||||
|
||||
while (true) {
|
||||
|
||||
if (this._accept(TokenType.Colon)) {
|
||||
target = children;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (this._accept(TokenType.CurlyClose)) {
|
||||
|
||||
if (!this._accept(TokenType.CurlyClose)) {
|
||||
this._return(this._prevToken);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (children !== target) {
|
||||
// we have not seen the colon which
|
||||
// means use the ident also as
|
||||
// default value
|
||||
children = name;
|
||||
}
|
||||
|
||||
marker.push(new Placeholder(Marker.toString(name), children));
|
||||
return true;
|
||||
}
|
||||
|
||||
if (this._parseAny(target) || this._parseText(target)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// fallback
|
||||
if (children.length > 0) {
|
||||
marker.push(new Text('{{' + Marker.toString(name) + ':'));
|
||||
marker.push(...children);
|
||||
} else {
|
||||
marker.push(new Text('{{'));
|
||||
marker.push(...name);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private _parseEscaped(marker: Marker[]): boolean {
|
||||
if (this._accept(TokenType.Backslash)) {
|
||||
if (// Internal style
|
||||
(this._enableInternal && (this._accept(TokenType.CurlyOpen) || this._accept(TokenType.CurlyClose) || this._accept(TokenType.Backslash)))
|
||||
// TextMate style
|
||||
|| (this._enableTextMate && (this._accept(TokenType.Dollar) || this._accept(TokenType.CurlyClose) || this._accept(TokenType.Backslash)))
|
||||
) {
|
||||
if (this._accept(TokenType.Dollar) || this._accept(TokenType.CurlyClose) || this._accept(TokenType.Backslash)) {
|
||||
// just consume them
|
||||
}
|
||||
marker.push(new Text(this._scanner.tokenText(this._prevToken)));
|
||||
|
||||
@@ -126,23 +126,23 @@ suite('SnippetParser', () => {
|
||||
assertText('\\}', '}');
|
||||
assertText('\\abc', '\\abc');
|
||||
assertText('foo${f:\\}}bar', 'foo}bar');
|
||||
assertText('\\{', '{');
|
||||
assertText('\\{', '\\{');
|
||||
assertText('I need \\\\\\$', 'I need \\$');
|
||||
assertText('\\', '\\');
|
||||
assertText('\\{{', '{{');
|
||||
assertText('\\{{', '\\{{');
|
||||
assertText('{{', '{{');
|
||||
assertText('{{dd', '{{dd');
|
||||
assertText('}}', '}}');
|
||||
assertText('ff}}', 'ff}}');
|
||||
|
||||
assertText('farboo', 'farboo');
|
||||
assertText('far{{}}boo', 'farboo');
|
||||
assertText('far{{123}}boo', 'far123boo');
|
||||
assertText('far\\{{123}}boo', 'far{{123}}boo');
|
||||
assertText('far{{id:bern}}boo', 'farbernboo');
|
||||
assertText('far{{id:bern {{basel}}}}boo', 'farbern baselboo');
|
||||
assertText('far{{id:bern {{id:basel}}}}boo', 'farbern baselboo');
|
||||
assertText('far{{id:bern {{id2:basel}}}}boo', 'farbern baselboo');
|
||||
assertText('far{{}}boo', 'far{{}}boo');
|
||||
assertText('far{{123}}boo', 'far{{123}}boo');
|
||||
assertText('far\\{{123}}boo', 'far\\{{123}}boo');
|
||||
assertText('far{{id:bern}}boo', 'far{{id:bern}}boo');
|
||||
assertText('far{{id:bern {{basel}}}}boo', 'far{{id:bern {{basel}}}}boo');
|
||||
assertText('far{{id:bern {{id:basel}}}}boo', 'far{{id:bern {{id:basel}}}}boo');
|
||||
assertText('far{{id:bern {{id2:basel}}}}boo', 'far{{id:bern {{id2:basel}}}}boo');
|
||||
});
|
||||
|
||||
|
||||
@@ -152,7 +152,7 @@ suite('SnippetParser', () => {
|
||||
|
||||
assertTextAndMarker('foo${1:bar\\}${2:foo}}', 'foobar}foo', Text, Placeholder);
|
||||
|
||||
let [, placeholder] = new SnippetParser(true, false).parse('foo${1:bar\\}${2:foo}}');
|
||||
let [, placeholder] = new SnippetParser().parse('foo${1:bar\\}${2:foo}}');
|
||||
let { children } = (<Placeholder>placeholder);
|
||||
|
||||
assert.equal((<Placeholder>placeholder).index, '1');
|
||||
@@ -164,9 +164,9 @@ suite('SnippetParser', () => {
|
||||
|
||||
test('Parser, placeholder', () => {
|
||||
assertTextAndMarker('farboo', 'farboo', Text);
|
||||
assertTextAndMarker('far{{}}boo', 'farboo', Text, Placeholder, Text);
|
||||
assertTextAndMarker('far{{123}}boo', 'far123boo', Text, Placeholder, Text);
|
||||
assertTextAndMarker('far\\{{123}}boo', 'far{{123}}boo', Text);
|
||||
assertTextAndMarker('far{{}}boo', 'far{{}}boo', Text);
|
||||
assertTextAndMarker('far{{123}}boo', 'far{{123}}boo', Text);
|
||||
assertTextAndMarker('far\\{{123}}boo', 'far\\{{123}}boo', Text);
|
||||
});
|
||||
|
||||
test('Parser, literal code', () => {
|
||||
@@ -194,7 +194,7 @@ suite('SnippetParser', () => {
|
||||
});
|
||||
|
||||
test('Parser, only textmate', () => {
|
||||
const p = new SnippetParser(true, false);
|
||||
const p = new SnippetParser();
|
||||
assertMarker(p.parse('far{{}}boo'), Text);
|
||||
assertMarker(p.parse('far{{123}}boo'), Text);
|
||||
assertMarker(p.parse('far\\{{123}}boo'), Text);
|
||||
@@ -204,17 +204,6 @@ suite('SnippetParser', () => {
|
||||
assertMarker(p.parse('far\\${123}boo'), Text);
|
||||
});
|
||||
|
||||
test('Parser, only internal', () => {
|
||||
const p = new SnippetParser(false, true);
|
||||
assertMarker(p.parse('far{{}}boo'), Text, Placeholder, Text);
|
||||
assertMarker(p.parse('far{{123}}boo'), Text, Placeholder, Text);
|
||||
assertMarker(p.parse('far\\{{123}}boo'), Text);
|
||||
|
||||
assertMarker(p.parse('far$0boo'), Text);
|
||||
assertMarker(p.parse('far${123}boo'), Text);
|
||||
assertMarker(p.parse('far\\${123}boo'), Text);
|
||||
});
|
||||
|
||||
test('Parser, real world', () => {
|
||||
let marker = new SnippetParser().parse('console.warn(${1: $TM_SELECTED_TEXT })');
|
||||
|
||||
@@ -242,41 +231,6 @@ suite('SnippetParser', () => {
|
||||
assert.ok(marker[0] instanceof Variable);
|
||||
});
|
||||
|
||||
test('Parser, real world, mixed', () => {
|
||||
|
||||
assertTextAndMarker(
|
||||
'finished:{{}}, second:{{2:name}}, first:{{1:}}, third:{{3:}}',
|
||||
'finished:, second:name, first:, third:',
|
||||
Text, Placeholder, Text, Placeholder, Text, Placeholder, Text, Placeholder
|
||||
);
|
||||
|
||||
|
||||
assertTextAndMarker(
|
||||
'begin\\{{{1:enumerate}}\\}\n\t{{}}\nend\\{{{1:}}\\}',
|
||||
'begin{enumerate}\n\t\nend{enumerate}',
|
||||
Text, Placeholder, Text, Placeholder, Text, Placeholder, Text
|
||||
);
|
||||
|
||||
});
|
||||
|
||||
test('Parser, default name/value', () => {
|
||||
assertTextAndMarker(
|
||||
'{{first}}-{{2:}}-{{second}}-{{1:}}',
|
||||
'first--second-',
|
||||
Placeholder, Text, Placeholder, Text, Placeholder, Text, Placeholder
|
||||
);
|
||||
|
||||
const [p1, , p2, , p3] = new SnippetParser().parse('{{first}}-{{2:}}-{{second}}-{{1:}}');
|
||||
assert.equal((<Placeholder>p1).index, 'first');
|
||||
assert.equal(Marker.toString((<Placeholder>p1).children), 'first');
|
||||
|
||||
assert.equal((<Placeholder>p2).index, '2');
|
||||
assert.equal(Marker.toString((<Placeholder>p2).children), '');
|
||||
|
||||
assert.equal((<Placeholder>p3).index, 'second');
|
||||
assert.equal(Marker.toString((<Placeholder>p3).children), 'second');
|
||||
});
|
||||
|
||||
test('Parser, default placeholder values', () => {
|
||||
|
||||
assertMarker('errorContext: `${1:err}`, error: $1', Text, Placeholder, Text, Placeholder);
|
||||
@@ -294,15 +248,15 @@ suite('SnippetParser', () => {
|
||||
});
|
||||
|
||||
test('backspace esapce in TM only, #16212', () => {
|
||||
const actual = new SnippetParser(true, false).text('Foo \\\\${abc}bar');
|
||||
const actual = new SnippetParser().text('Foo \\\\${abc}bar');
|
||||
assert.equal(actual, 'Foo \\bar');
|
||||
});
|
||||
|
||||
test('colon as variable/placeholder value, #16717', () => {
|
||||
let actual = new SnippetParser(true, false).text('${TM_SELECTED_TEXT:foo:bar}');
|
||||
let actual = new SnippetParser().text('${TM_SELECTED_TEXT:foo:bar}');
|
||||
assert.equal(actual, 'foo:bar');
|
||||
|
||||
actual = new SnippetParser(true, false).text('${1:foo:bar}');
|
||||
actual = new SnippetParser().text('${1:foo:bar}');
|
||||
assert.equal(actual, 'foo:bar');
|
||||
});
|
||||
|
||||
|
||||
@@ -114,7 +114,7 @@ export class EditorAccessor implements emmet.Editor {
|
||||
// string to string conversion that tries to fix the
|
||||
// snippet in-place
|
||||
|
||||
let marker = new SnippetParser(true, false).parse(template);
|
||||
let marker = new SnippetParser().parse(template);
|
||||
let maxIndex = -Number.MIN_VALUE;
|
||||
|
||||
// find highest placeholder index
|
||||
|
||||
@@ -159,7 +159,7 @@ function parseSnippetFile(snippetFileContent: string, extensionName?: string, co
|
||||
}
|
||||
|
||||
function _rewriteBogousVariables(snippet: ISnippet): boolean {
|
||||
const marker = new SnippetParser(true, false).parse(snippet.codeSnippet, false);
|
||||
const marker = new SnippetParser().parse(snippet.codeSnippet, false);
|
||||
|
||||
let placeholders = new Map<string, number>();
|
||||
let placeholderMax = 0;
|
||||
|
||||
Reference in New Issue
Block a user