From 572697a2c2267430010b3534281f73337144e94f Mon Sep 17 00:00:00 2001 From: Barak Gall Date: Thu, 28 Feb 2019 23:35:00 -0500 Subject: [PATCH 1/3] Fix undetected substitution on newlines within interpolated verbatim strings Fix issue in interpolated verbatim strings where a substitution is not being detected if they occur at the start of a literal newline. --- grammars/csharp.tmLanguage | 2 +- grammars/csharp.tmLanguage.cson | 2 +- package-lock.json | 28 +++++++++++++++++++++------- src/csharp.tmLanguage.yml | 2 +- test/literals.tests.ts | 23 +++++++++++++++++++++++ 5 files changed, 47 insertions(+), 10 deletions(-) diff --git a/grammars/csharp.tmLanguage b/grammars/csharp.tmLanguage index e0ea7e9..d2588e7 100644 --- a/grammars/csharp.tmLanguage +++ b/grammars/csharp.tmLanguage @@ -4029,7 +4029,7 @@ name meta.interpolation.cs begin - (?<=[^\{])((?:\{\{)*)(\{)(?=[^\{]) + (?<=[^\{]|^)((?:\{\{)*)(\{)(?=[^\{]) beginCaptures 1 diff --git a/grammars/csharp.tmLanguage.cson b/grammars/csharp.tmLanguage.cson index da04d15..381207c 100644 --- a/grammars/csharp.tmLanguage.cson +++ b/grammars/csharp.tmLanguage.cson @@ -2441,7 +2441,7 @@ repository: ] interpolation: name: "meta.interpolation.cs" - begin: "(?<=[^\\{])((?:\\{\\{)*)(\\{)(?=[^\\{])" + begin: "(?<=[^\\{]|^)((?:\\{\\{)*)(\\{)(?=[^\\{])" beginCaptures: "1": name: "string.quoted.double.cs" diff --git a/package-lock.json b/package-lock.json index 5afaaf3..b492322 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1551,12 +1551,14 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, + "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -1571,17 +1573,20 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "core-util-is": { "version": "1.0.2", @@ -1698,7 +1703,8 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "ini": { "version": "1.3.5", @@ -1710,6 +1716,7 @@ "version": "1.0.0", "bundled": true, "dev": true, + "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -1724,6 +1731,7 @@ "version": "3.0.4", "bundled": true, "dev": true, + "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -1731,12 +1739,14 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "minipass": { "version": "2.2.4", "bundled": true, "dev": true, + "optional": true, "requires": { "safe-buffer": "^5.1.1", "yallist": "^3.0.0" @@ -1755,6 +1765,7 @@ "version": "0.5.1", "bundled": true, "dev": true, + "optional": true, "requires": { "minimist": "0.0.8" } @@ -1835,7 +1846,8 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "object-assign": { "version": "4.1.1", @@ -1847,6 +1859,7 @@ "version": "1.4.0", "bundled": true, "dev": true, + "optional": true, "requires": { "wrappy": "1" } @@ -1968,6 +1981,7 @@ "version": "1.0.2", "bundled": true, "dev": true, + "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", diff --git a/src/csharp.tmLanguage.yml b/src/csharp.tmLanguage.yml index 7d84a0f..4fd9673 100644 --- a/src/csharp.tmLanguage.yml +++ b/src/csharp.tmLanguage.yml @@ -1525,7 +1525,7 @@ repository: interpolation: name: meta.interpolation.cs - begin: (?<=[^\{])((?:\{\{)*)(\{)(?=[^\{]) + begin: (?<=[^\{]|^)((?:\{\{)*)(\{)(?=[^\{]) beginCaptures: '1': { name: string.quoted.double.cs } '2': { name: punctuation.definition.interpolation.begin.cs } diff --git a/test/literals.tests.ts b/test/literals.tests.ts index da6e624..83c8447 100644 --- a/test/literals.tests.ts +++ b/test/literals.tests.ts @@ -477,6 +477,29 @@ throw new InvalidCastException( ]); }); + it("properly treat verbatim interpolated string substitutions when they exist at the start of a new line (bug #131)", () => { + const input = Input.InMethod(` +var str = $@\" +I am a multiline string with a +{parameter} that starts after a newline! +\"; +`); + const tokens = tokenize(input); + + tokens.should.deep.equal([ + Token.Keywords.Var, + Token.Identifiers.LocalName("str"), + Token.Operators.Assignment, + Token.Punctuation.InterpolatedString.VerbatimBegin, + Token.Literals.String("I am a multiline string with a"), + Token.Punctuation.Interpolation.Begin, + Token.Variables.ReadWrite("parameter"), + Token.Punctuation.Interpolation.End, + Token.Literals.String(" that starts after a newline!"), + Token.Punctuation.InterpolatedString.End, + Token.Punctuation.Semicolon + ]); + }); it("highlight strings containing braces correctly (issue omnisharp-vscode#746)", () => { const input = ` namespace X From 2b7979f1f506f4dd4cf6a9877741ba1fd293cdaf Mon Sep 17 00:00:00 2001 From: klick-barakgall Date: Fri, 1 Mar 2019 12:01:06 -0500 Subject: [PATCH 2/3] removes unnecessary escaping from 131 test case --- test/literals.tests.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/literals.tests.ts b/test/literals.tests.ts index 83c8447..3fbdcb5 100644 --- a/test/literals.tests.ts +++ b/test/literals.tests.ts @@ -479,10 +479,10 @@ throw new InvalidCastException( it("properly treat verbatim interpolated string substitutions when they exist at the start of a new line (bug #131)", () => { const input = Input.InMethod(` -var str = $@\" +var str = $@" I am a multiline string with a {parameter} that starts after a newline! -\"; +"; `); const tokens = tokenize(input); @@ -549,4 +549,4 @@ namespace X }); }); }); -}); \ No newline at end of file +}); From 2500efd2413587ca6fc88403378f722d9f01e2be Mon Sep 17 00:00:00 2001 From: Barak Gall Date: Fri, 1 Mar 2019 21:56:05 -0500 Subject: [PATCH 3/3] moves test to more appropriate suite --- test/interpolated-string.tests.ts | 24 ++++++++++++++++++++++++ test/literals.tests.ts | 25 +------------------------ 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/test/interpolated-string.tests.ts b/test/interpolated-string.tests.ts index 8d33a5a..e7096aa 100644 --- a/test/interpolated-string.tests.ts +++ b/test/interpolated-string.tests.ts @@ -200,6 +200,30 @@ world {two}!";`); Token.Punctuation.Semicolon]); }); + it("break across two lines and start with a new line with an interpolation (verbatim)", () => { + const input = Input.InMethod(` +var str = $@\" +I am a multiline string with a +{parameter} that starts after a newline! +\"; +`); + const tokens = tokenize(input); + + tokens.should.deep.equal([ + Token.Keywords.Var, + Token.Identifiers.LocalName("str"), + Token.Operators.Assignment, + Token.Punctuation.InterpolatedString.VerbatimBegin, + Token.Literals.String("I am a multiline string with a"), + Token.Punctuation.Interpolation.Begin, + Token.Variables.ReadWrite("parameter"), + Token.Punctuation.Interpolation.End, + Token.Literals.String(" that starts after a newline!"), + Token.Punctuation.InterpolatedString.End, + Token.Punctuation.Semicolon + ]); + }); + it("break across two lines with no interpolations (verbatim)", () => { const input = Input.InClass(` diff --git a/test/literals.tests.ts b/test/literals.tests.ts index 83c8447..30c3ec7 100644 --- a/test/literals.tests.ts +++ b/test/literals.tests.ts @@ -230,7 +230,7 @@ describe("Grammar", () => { Token.Punctuation.String.End, Token.Punctuation.Semicolon]); }); - + it("escaped character escape \\t", () => { const input = Input.InClass(`string test = "hello\\tworld!";`); const tokens = tokenize(input); @@ -477,29 +477,6 @@ throw new InvalidCastException( ]); }); - it("properly treat verbatim interpolated string substitutions when they exist at the start of a new line (bug #131)", () => { - const input = Input.InMethod(` -var str = $@\" -I am a multiline string with a -{parameter} that starts after a newline! -\"; -`); - const tokens = tokenize(input); - - tokens.should.deep.equal([ - Token.Keywords.Var, - Token.Identifiers.LocalName("str"), - Token.Operators.Assignment, - Token.Punctuation.InterpolatedString.VerbatimBegin, - Token.Literals.String("I am a multiline string with a"), - Token.Punctuation.Interpolation.Begin, - Token.Variables.ReadWrite("parameter"), - Token.Punctuation.Interpolation.End, - Token.Literals.String(" that starts after a newline!"), - Token.Punctuation.InterpolatedString.End, - Token.Punctuation.Semicolon - ]); - }); it("highlight strings containing braces correctly (issue omnisharp-vscode#746)", () => { const input = ` namespace X