提交 78e24f95 编写于 作者: J Johannes Rieken

snippets - handle escaped closed curly, better compacting

上级 7c21c613
...@@ -186,24 +186,32 @@ export class SnippetParser { ...@@ -186,24 +186,32 @@ export class SnippetParser {
// * fill in default for empty placeHolders // * fill in default for empty placeHolders
// * compact sibling Text markers // * compact sibling Text markers
const placeholders: { [name: string]: Marker[] } = Object.create(null); function compact(marker: Marker[], placeholders: { [name: string]: Marker[] }) {
for (let i = 0; i < marker.length; i++) {
let thisMarker = marker[i]; for (let i = 0; i < marker.length; i++) {
const thisMarker = marker[i];
if (thisMarker instanceof Placeholder) {
if (placeholders[thisMarker.name] === undefined) { if (thisMarker instanceof Placeholder) {
placeholders[thisMarker.name] = thisMarker.value; if (placeholders[thisMarker.name] === undefined) {
} else if (thisMarker.value.length === 0) { placeholders[thisMarker.name] = thisMarker.value;
thisMarker.value = placeholders[thisMarker.name].slice(0); } else if (thisMarker.value.length === 0) {
} thisMarker.value = placeholders[thisMarker.name].slice(0);
}
} else if (i > 0 && thisMarker instanceof Text && marker[i - 1] instanceof Text) { if (thisMarker.value.length > 0) {
(<Text>marker[i - 1]).string += (<Text>marker[i]).string; compact(thisMarker.value, placeholders);
marker.splice(i, 1); }
i--;
} else if (i > 0 && thisMarker instanceof Text && marker[i - 1] instanceof Text) {
(<Text>marker[i - 1]).string += (<Text>marker[i]).string;
marker.splice(i, 1);
i--;
}
} }
} }
compact(marker, Object.create(null));
return marker; return marker;
} }
...@@ -347,7 +355,7 @@ export class SnippetParser { ...@@ -347,7 +355,7 @@ export class SnippetParser {
if (// Internal style if (// Internal style
(this._enableInternal && (this._accept(TokenType.CurlyOpen) || this._accept(TokenType.CurlyClose) || this._accept(TokenType.Backslash))) (this._enableInternal && (this._accept(TokenType.CurlyOpen) || this._accept(TokenType.CurlyClose) || this._accept(TokenType.Backslash)))
// TextMate style // TextMate style
|| (this._enableTextMate && this._accept(TokenType.Dollar)) || (this._enableTextMate && (this._accept(TokenType.Dollar) || this._accept(TokenType.CurlyClose)))
) { ) {
// just consume them // just consume them
} }
......
...@@ -127,6 +127,23 @@ suite('SnippetParser', () => { ...@@ -127,6 +127,23 @@ suite('SnippetParser', () => {
assertEscape('far{{id:bern {{basel}}}}boo', 'farbern baselboo'); assertEscape('far{{id:bern {{basel}}}}boo', 'farbern baselboo');
assertEscape('far{{id:bern {{id:basel}}}}boo', 'farbern baselboo'); assertEscape('far{{id:bern {{id:basel}}}}boo', 'farbern baselboo');
assertEscape('far{{id:bern {{id2:basel}}}}boo', 'farbern baselboo'); assertEscape('far{{id:bern {{id2:basel}}}}boo', 'farbern baselboo');
});
test('Parser, TM escaping', () => {
assertEscapeAndMarker('foo${1:bar}}', 'foobar}', Text, Placeholder, Text);
assertEscapeAndMarker('foo${1:bar}${2:foo}}', 'foobarfoo}', Text, Placeholder, Placeholder, Text);
assertEscapeAndMarker('foo${1:bar\\}${2:foo}}', 'foobar}foo', Text, Placeholder);
let [, placeholder] = new SnippetParser(true, false).parse('foo${1:bar\\}${2:foo}}');
let {value} = (<Placeholder>placeholder);
assert.equal((<Placeholder>placeholder).name, '1');
assert.ok(value[0] instanceof Text);
assert.equal(value[0].toString(), 'bar}');
assert.ok(value[1] instanceof Placeholder);
assert.equal(value[1].toString(), 'foo');
}); });
test('Parser, placeholder', () => { test('Parser, placeholder', () => {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册