diff --git a/grammars/csharp.tmLanguage b/grammars/csharp.tmLanguage index 12939af..6517b62 100644 --- a/grammars/csharp.tmLanguage +++ b/grammars/csharp.tmLanguage @@ -1719,7 +1719,7 @@ ) (?<interface_name>\g<type_name>\s*\.\s*)? (?<property_name>\g<identifier>)\s* -(?=\{|=>|$) +(?=\{|=>|//|/\*|$) beginCaptures 1 @@ -1899,7 +1899,7 @@ ) (?<interface_name>\g<type_name>\s*\.\s*)? (?<event_names>\g<identifier>(?:\s*,\s*\g<identifier>)*)\s* -(?=\{|;|$) +(?=\{|;|//|/\*|$) beginCaptures 1 @@ -3024,7 +3024,7 @@ with-expression begin - (?<!\.)\b(with)\b\s*(?=\{|$) + (?<!\.)\b(with)\b\s*(?=\{|//|/\*|$) beginCaptures 1 @@ -3037,6 +3037,10 @@ (?<=\}) patterns + + include + #comment + include #initializer-expression @@ -6252,7 +6256,7 @@ )* ) )\s* -(?=\{|$) +(?=\{|//|/\*|$) captures 1 @@ -6331,7 +6335,7 @@ anonymous-object-creation-expression begin - \b(new)\b\s*(?=\{|$) + \b(new)\b\s*(?=\{|//|/\*|$) beginCaptures 1 @@ -6344,6 +6348,10 @@ (?<=\}) patterns + + include + #comment + include #initializer-expression diff --git a/grammars/csharp.tmLanguage.cson b/grammars/csharp.tmLanguage.cson index 3651d30..cc17d2a 100644 --- a/grammars/csharp.tmLanguage.cson +++ b/grammars/csharp.tmLanguage.cson @@ -1091,7 +1091,7 @@ repository: ) (?\\g\\s*\\.\\s*)? (?\\g)\\s* - (?=\\{|=>|$) + (?=\\{|=>|//|/\\*|$) ''' beginCaptures: "1": @@ -1227,7 +1227,7 @@ repository: ) (?\\g\\s*\\.\\s*)? (?\\g(?:\\s*,\\s*\\g)*)\\s* - (?=\\{|;|$) + (?=\\{|;|//|/\\*|$) ''' beginCaptures: "1": @@ -1888,12 +1888,15 @@ repository: } ] "with-expression": - begin: "(?\g\s*\.\s*)? (?\g)\s* - (?=\{|=>|$) + (?=\{|=>|//|/\*|$) beginCaptures: '1': patterns: @@ -691,7 +691,7 @@ repository: ) (?\g\s*\.\s*)? (?\g(?:\s*,\s*\g)*)\s* - (?=\{|;|$) + (?=\{|;|//|/\*|$) beginCaptures: '1': { name: keyword.other.event.cs } '2': @@ -1122,11 +1122,12 @@ repository: - include: '#punctuation-comma' with-expression: - begin: (? { + const input = Input.InClass(`event EventHandler Event /* comment */ { add; remove; }`); + const tokens = await tokenize(input); + + tokens.should.deep.equal([ + Token.Keywords.Event, + Token.Type("EventHandler"), + Token.Identifiers.EventName("Event"), + Token.Comment.MultiLine.Start, + Token.Comment.MultiLine.Text(" comment "), + Token.Comment.MultiLine.End, + Token.Punctuation.OpenBrace, + Token.Keywords.Add, + Token.Punctuation.Semicolon, + Token.Keywords.Remove, + Token.Punctuation.Semicolon, + Token.Punctuation.CloseBrace, + ]); + }); + + it("comment before initializer - multiple lines (issue #264)", async () => { + const input = Input.InClass(` +event EventHandler Event // comment +{ + add; + remove; +}`); + const tokens = await tokenize(input); + + tokens.should.deep.equal([ + Token.Keywords.Event, + Token.Type("EventHandler"), + Token.Identifiers.EventName("Event"), + Token.Comment.SingleLine.Start, + Token.Comment.SingleLine.Text(" comment"), + Token.Punctuation.OpenBrace, + Token.Keywords.Add, + Token.Punctuation.Semicolon, + Token.Keywords.Remove, + Token.Punctuation.Semicolon, + Token.Punctuation.CloseBrace, + ]); + }); }); -}); \ No newline at end of file +}); diff --git a/test/expressions.tests.ts b/test/expressions.tests.ts index dc3b5b0..2365583 100644 --- a/test/expressions.tests.ts +++ b/test/expressions.tests.ts @@ -91,6 +91,31 @@ describe("Expressions", () => { Token.Punctuation.Semicolon ]); }); + + it("comment before initializer (issue #264)", async () => { + const input = Input.InMethod(` +var a = new A // comment +{ + X = 1 +};`); + const tokens = await tokenize(input); + + tokens.should.deep.equal([ + Token.Keywords.Var, + Token.Identifiers.LocalName("a"), + Token.Operators.Assignment, + Token.Keywords.New, + Token.Type("A"), + Token.Comment.SingleLine.Start, + Token.Comment.SingleLine.Text(" comment"), + Token.Punctuation.OpenBrace, + Token.Variables.ReadWrite("X"), + Token.Operators.Assignment, + Token.Literals.Numeric.Decimal("1"), + Token.Punctuation.CloseBrace, + Token.Punctuation.Semicolon, + ]); + }); }); describe("Anonymous Methods", () => { @@ -1149,6 +1174,30 @@ var x = new Token.Punctuation.Semicolon ]); }); + + it("comment before initializer (issue #264)", async () => { + const input = Input.InMethod(` +var x = new // comment +{ + ID = 42 +};`); + const tokens = await tokenize(input); + + tokens.should.deep.equal([ + Token.Keywords.Var, + Token.Identifiers.LocalName("x"), + Token.Operators.Assignment, + Token.Keywords.New, + Token.Comment.SingleLine.Start, + Token.Comment.SingleLine.Text(" comment"), + Token.Punctuation.OpenBrace, + Token.Variables.ReadWrite("ID"), + Token.Operators.Assignment, + Token.Literals.Numeric.Decimal("42"), + Token.Punctuation.CloseBrace, + Token.Punctuation.Semicolon, + ]); + }); }); describe("Arithmetic", () => { @@ -4964,6 +5013,31 @@ var p2 = p1 with Token.Punctuation.Semicolon, ]); }); + + it("comment before initializer (issue #264)", async () => { + const input = Input.InMethod(` +var p2 = p1 with // comment +{ + X = 5 +};`); + const tokens = await tokenize(input); + + tokens.should.deep.equal([ + Token.Keywords.Var, + Token.Identifiers.LocalName("p2"), + Token.Operators.Assignment, + Token.Variables.ReadWrite("p1"), + Token.Keywords.With, + Token.Comment.SingleLine.Start, + Token.Comment.SingleLine.Text(" comment"), + Token.Punctuation.OpenBrace, + Token.Variables.ReadWrite("X"), + Token.Operators.Assignment, + Token.Literals.Numeric.Decimal("5"), + Token.Punctuation.CloseBrace, + Token.Punctuation.Semicolon, + ]); + }); }); }); }); diff --git a/test/property.tests.ts b/test/property.tests.ts index 28feb52..30bc985 100644 --- a/test/property.tests.ts +++ b/test/property.tests.ts @@ -441,5 +441,47 @@ public int Timeout Token.Punctuation.Semicolon, Token.Punctuation.CloseBrace]); }); + + it("comment before initializer - multiple lines (issue #264)", async () => { + const input = Input.InClass(` +int Property // comment +{ + get; + set; +}`); + const tokens = await tokenize(input); + + tokens.should.deep.equal([ + Token.PrimitiveType.Int, + Token.Identifiers.PropertyName("Property"), + Token.Comment.SingleLine.Start, + Token.Comment.SingleLine.Text(" comment"), + Token.Punctuation.OpenBrace, + Token.Keywords.Get, + Token.Punctuation.Semicolon, + Token.Keywords.Set, + Token.Punctuation.Semicolon, + Token.Punctuation.CloseBrace, + ]); + }); + + it("comment before initializer - single line (issue #264)", async () => { + const input = Input.InClass(`int Property /* comment */ { get; set; }`); + const tokens = await tokenize(input); + + tokens.should.deep.equal([ + Token.PrimitiveType.Int, + Token.Identifiers.PropertyName("Property"), + Token.Comment.MultiLine.Start, + Token.Comment.MultiLine.Text(" comment "), + Token.Comment.MultiLine.End, + Token.Punctuation.OpenBrace, + Token.Keywords.Get, + Token.Punctuation.Semicolon, + Token.Keywords.Set, + Token.Punctuation.Semicolon, + Token.Punctuation.CloseBrace, + ]); + }); }); });