Skip to content

Commit

Permalink
Syntax highlighting support for class/interface generic constrains an…
Browse files Browse the repository at this point in the history
…d tests.
  • Loading branch information
ivanz committed Dec 4, 2016
1 parent 425dd6b commit 32d3ef4
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ trim_trailing_whitespace = true
[{.travis.yml},package.json]
indent_style = space
indent_size = 2

[syntaxes/csharp.json]
indent_style = space
indent_size = 2
46 changes: 43 additions & 3 deletions syntaxes/csharp.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,43 @@
}
]
},
"genericConstraints": {
"begin": "(where)\\s*(\\w+)\\s*:",
"end": "(?={)",
"beginCaptures": {
"1": {
"name": "keyword.other.cs"
},
"2": {
"name": "storage.type.cs"
}
},
"patterns": [
{
"match": "\\b(class|struct)\\b",
"name": "keyword.other.cs"
},
{
"match": "(new)\\s*\\(\\s*\\)",
"captures": {
"1": {
"name": "keyword.other.cs"
}
}
},
{
"match": "([\\w<>]+)\\s*(?=,|where|{)",
"captures": {
"1": {
"name": "storage.type.cs"
}
}
},
{
"include": "#genericConstraints"
}
]
},
"class": {
"begin": "(?=\\w?[\\w\\s]*[^@]?(?:class|struct|interface|enum)\\s+\\w+)",
"end": "}",
Expand Down Expand Up @@ -153,18 +190,21 @@
},
{
"begin": ":",
"end": "(?={)",
"end": "(?={|where)",
"patterns": [
{
"match": "([\\w<>]+)\\s*",
"captures": {
"1": {
"name": "storage.type.cs"
}
},
"match": "\\s*,?([A-Za-z_]\\w*)\\b"
}
}
]
},
{
"include": "#genericConstraints"
},
{
"begin": "{",
"beginCaptures": {
Expand Down
54 changes: 53 additions & 1 deletion test/syntaxes/class.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe("Grammar", function() {
});

describe("Class", function() {
it("has a class keyword, a name and optional storage modifiers", function() {
it("class keyword and storage modifiers", function() {

const input = `
namespace TestNamespace
Expand Down Expand Up @@ -73,6 +73,58 @@ namespace TestNamespace

});

it("inheritance", function() {

const input = `
namespace TestNamespace
{
class PublicClass : IInterface, IInterfaceTwo { }
class PublicClass<T> : IInterface<T>, IInterfaceTwo { }
}`;
let tokens: Token[] = TokenizerUtil.tokenize(input);

tokens.should.contain(Tokens.ClassKeyword("class", 4, 5));
tokens.should.contain(Tokens.ClassIdentifier("PublicClass", 4, 11));
tokens.should.contain(Tokens.Type("IInterface", 4, 28));
tokens.should.contain(Tokens.Type("IInterfaceTwo", 4, 43));

tokens.should.contain(Tokens.ClassKeyword("class", 5, 5));
tokens.should.contain(Tokens.ClassIdentifier("PublicClass", 5, 11));
tokens.should.contain(Tokens.Type("IInterface<T>", 5, 28));
tokens.should.contain(Tokens.Type("IInterfaceTwo", 5, 43));
});

it("generic constraints", function() {

const input = `
namespace TestNamespace
{
class PublicClass<T> where T : ISomething { }
class PublicClass<T, X> : List<T>, ISomething where T : ICar, new() where X : struct { }
}`;
let tokens: Token[] = TokenizerUtil.tokenize(input);

tokens.should.contain(Tokens.ClassKeyword("class", 4, 5));
tokens.should.contain(Tokens.ClassIdentifier("PublicClass", 4, 11));
tokens.should.contain(Tokens.Keyword("where", 4, 26));
tokens.should.contain(Tokens.Type("T", 4, 32));
tokens.should.contain(Tokens.Type("ISomething", 4, 36));

tokens.should.contain(Tokens.ClassKeyword("class", 5, 5));
tokens.should.contain(Tokens.ClassIdentifier("PublicClass", 5, 11));
tokens.should.contain(Tokens.Type("List<T>", 5, 31));
tokens.should.contain(Tokens.Type("ISomething", 5, 40));
tokens.should.contain(Tokens.Keyword("where", 5, 51));
tokens.should.contain(Tokens.Type("T", 5, 57));
tokens.should.contain(Tokens.Type("ICar", 5, 61));
tokens.should.contain(Tokens.Keyword("new", 5, 67));
tokens.should.contain(Tokens.Keyword("where", 5, 73));
tokens.should.contain(Tokens.Type("X", 5, 79));
tokens.should.contain(Tokens.Keyword("struct", 5, 83));

});


});
});

Expand Down
5 changes: 5 additions & 0 deletions test/syntaxes/utils/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,10 @@ export namespace Tokens {
export const StorageModifierKeyword = (text: string, line?: number, column?: number) =>
createToken(text, "storage.modifier.cs", line, column);

export const Type = (text: string, line?: number, column?: number) =>
createToken(text, "storage.type.cs", line, column);

export const Keyword = (text: string, line?: number, column?: number) =>
createToken(text, "keyword.other.cs", line, column);
}

0 comments on commit 32d3ef4

Please sign in to comment.