Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Allow root functions for embedding, fix all specs
Browse files Browse the repository at this point in the history
  • Loading branch information
damieng committed Dec 29, 2016
1 parent 89d5577 commit 4bf9d94
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 59 deletions.
19 changes: 5 additions & 14 deletions grammars/c#.cson
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,10 @@ patterns: [
include: "#namespace"
}
{
include: "#comments"
include: "#code"
}
{
include: "#enum-declaration"
}
{
include: "#type-declaration"
}
{
include: "#preprocessor"
}
{
include: "#keywords"
include: "#method"
}
]
repository:
Expand Down Expand Up @@ -255,7 +246,7 @@ repository:
match: "(\\w+([?*])?)"
captures:
"1":
name: "meta.generic.type.specifier"
name: "meta.generic.type.specifier.cs"
patterns: [
{
include: "#type"
Expand Down Expand Up @@ -285,7 +276,7 @@ repository:
"1":
name: "keyword.other.cs"
"2":
name: "meta.generic.type.specifier"
name: "meta.generic.type.specifier.cs"
patterns: [
{
include: "#type"
Expand Down Expand Up @@ -831,7 +822,7 @@ repository:
match: "(\\w+)"
captures:
"1":
name: "meta.generic.type.specifier"
name: "meta.generic.type.specifier.cs"
patterns: [
{
include: "#type"
Expand Down
76 changes: 31 additions & 45 deletions spec/grammar-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,40 @@ describe "Language C# package", ->
}
"""

expect(tokens[1][1]).toEqual value: 'byte', scopes: ['source.cs', 'meta.class.cs', 'meta.class.body.cs', 'storage.value.type.cs']
expect(tokens[1][5]).toEqual value: '//', scopes: ['source.cs', 'meta.class.cs', 'meta.class.body.cs', 'comment.line.double-slash.cs']
expect(tokens[1][6]).toEqual value: '(', scopes: ['source.cs', 'meta.class.cs', 'meta.class.body.cs', 'comment.line.double-slash.cs']
expect(tokens[1][1]).toEqual value: 'byte', scopes: ['source.cs', 'meta.field.declaration.cs', 'storage.value.type.cs']
expect(tokens[1][5]).toEqual value: '//', scopes: ['source.cs', 'comment.line.double-slash.cs']
expect(tokens[1][6]).toEqual value: '(', scopes: ['source.cs', 'comment.line.double-slash.cs']

tokens = grammar.tokenizeLines """
struct hi {
byte q; /*(*/
}
"""
expect(tokens[1][1]).toEqual value: 'byte', scopes: ['source.cs', 'meta.class.cs', 'meta.class.body.cs', 'storage.value.type.cs']
expect(tokens[1][5]).toEqual value: '/*', scopes: ['source.cs', 'meta.class.cs', 'meta.class.body.cs', 'comment.block.cs', 'punctuation.definition.comment.cs']
expect(tokens[1][6]).toEqual value: '(', scopes: ['source.cs', 'meta.class.cs', 'meta.class.body.cs', 'comment.block.cs']
expect(tokens[1][1]).toEqual value: 'byte', scopes: ['source.cs', 'meta.field.declaration.cs', 'storage.value.type.cs']
expect(tokens[1][5]).toEqual value: '/*', scopes: ['source.cs', 'comment.block.cs', 'punctuation.definition.comment.cs']
expect(tokens[1][6]).toEqual value: '(', scopes: ['source.cs', 'comment.block.cs']

fit "tokenizes method definitions correctly", ->
it "tokenizes method definitions correctly", ->
{tokens} = grammar.tokenizeLine("void func() { }")
expect(tokens[6]).toEqual value: 'void', scopes: ['source.cs', 'meta.method.cs', 'meta.method.return-type.cs', 'storage.type.cs']
expect(tokens[8]).toEqual value: 'func', scopes: ['source.cs', 'meta.method.cs', 'meta.method.identifier.cs', 'entity.name.function.declaration.cs']
expect(tokens[9]).toEqual value: '(', scopes: ['source.cs', 'meta.method.cs', 'meta.method.identifier.cs', 'punctuation.definition.method-parameters.begin.cs']
expect(tokens[0]).toEqual value: 'void', scopes: ['source.cs', 'meta.method.cs', 'meta.method.return-type.cs', 'storage.type.cs']
expect(tokens[2]).toEqual value: 'func', scopes: ['source.cs', 'meta.method.cs', 'meta.method.identifier.cs', 'entity.name.function.declaration.cs']
expect(tokens[3]).toEqual value: '(', scopes: ['source.cs', 'meta.method.cs', 'meta.method.identifier.cs', 'punctuation.definition.method-parameters.begin.cs']

{tokens} = grammar.tokenizeLine("dictionary<int, string> func() { }")
console.log(tokens)
expect(tokens[5]).toEqual value: 'func', scopes: ['source.cs', 'meta.method-call.cs', 'meta.method.cs']
expect(tokens[6]).toEqual value: '(', scopes: ['source.cs', 'meta.method-call.cs', 'punctuation.definition.method-parameters.begin.cs']
expect(tokens[0]).toEqual value: 'dictionary', scopes: ['source.cs', 'meta.method.cs', 'meta.method.return-type.cs', 'meta.generic.class.identifier.cs']
expect(tokens[2]).toEqual value: 'int', scopes: ['source.cs', 'meta.method.cs', 'meta.method.return-type.cs', 'meta.generic.type.specifier.cs', 'storage.value.type.cs']
expect(tokens[6]).toEqual value: 'func', scopes: ['source.cs', 'meta.method.cs', 'meta.method.identifier.cs', 'entity.name.function.declaration.cs']
expect(tokens[7]).toEqual value: '(', scopes: ['source.cs', 'meta.method.cs', 'meta.method.identifier.cs', 'punctuation.definition.method-parameters.begin.cs']

{tokens} = grammar.tokenizeLine("void func(test = default_value) { }")
expect(tokens[0]).toEqual value: 'void ', scopes: ['source.cs']
expect(tokens[1]).toEqual value: 'func', scopes: ['source.cs', 'meta.method-call.cs', 'meta.method.cs']
expect(tokens[2]).toEqual value: '(', scopes: ['source.cs', 'meta.method-call.cs', 'punctuation.definition.method-parameters.begin.cs']
expect(tokens[0]).toEqual value: 'void', scopes: ['source.cs', 'meta.method.cs', 'meta.method.return-type.cs', 'storage.type.cs']
expect(tokens[2]).toEqual value: 'func', scopes: ['source.cs', 'meta.method.cs', 'meta.method.identifier.cs', 'entity.name.function.declaration.cs']
expect(tokens[3]).toEqual value: '(', scopes: ['source.cs', 'meta.method.cs', 'meta.method.identifier.cs', 'punctuation.definition.method-parameters.begin.cs']

it "tokenizes method calls", ->
{tokens} = grammar.tokenizeLine("a = func(1)")
expect(tokens[1]).toEqual value: 'func', scopes: ['source.cs', 'meta.method-call.cs', 'meta.method.cs']
expect(tokens[2]).toEqual value: '(', scopes: ['source.cs', 'meta.method-call.cs', 'punctuation.definition.method-parameters.begin.cs']
{tokens} = grammar.tokenizeLine("var a = func(1)")
expect(tokens[4]).toEqual value: 'func', scopes: ['source.cs', 'meta.method-call.cs', 'meta.method.cs']
expect(tokens[5]).toEqual value: '(', scopes: ['source.cs', 'meta.method-call.cs', 'punctuation.definition.method-parameters.begin.cs']

{tokens} = grammar.tokenizeLine("func()")
expect(tokens[0]).toEqual value: 'func', scopes: ['source.cs', 'meta.method-call.cs', 'meta.method.cs']
Expand All @@ -75,13 +76,13 @@ describe "Language C# package", ->
})
"""

expect(tokens[1][5]).toEqual value: 'C', scopes: ['source.cs', 'meta.class.cs', 'meta.class.body.cs', 'meta.method-call.cs', 'meta.method.cs']
expect(tokens[1][6]).toEqual value: '(', scopes: ['source.cs', 'meta.class.cs', 'meta.class.body.cs', 'meta.method-call.cs', 'punctuation.definition.method-parameters.begin.cs']
expect(tokens[1][7]).toEqual value: '"', scopes: ['source.cs', 'meta.class.cs', 'meta.class.body.cs', 'meta.method-call.cs', 'string.quoted.double.cs', 'punctuation.definition.string.begin.cs']
expect(tokens[1][8]).toEqual value: '1.1 10', scopes: ['source.cs', 'meta.class.cs', 'meta.class.body.cs', 'meta.method-call.cs', 'string.quoted.double.cs']
expect(tokens[1][9]).toEqual value: '\\n', scopes: ['source.cs', 'meta.class.cs', 'meta.class.body.cs', 'meta.method-call.cs', 'string.quoted.double.cs', 'constant.character.escape.cs']
expect(tokens[1][10]).toEqual value: '"', scopes: ['source.cs', 'meta.class.cs', 'meta.class.body.cs', 'meta.method-call.cs', 'string.quoted.double.cs', 'punctuation.definition.string.end.cs']
expect(tokens[1][11]).toEqual value: ')', scopes: ['source.cs', 'meta.class.cs', 'meta.class.body.cs', 'meta.method-call.cs', 'punctuation.definition.method-parameters.end.cs']
expect(tokens[1][5]).toEqual value: 'C', scopes: ['source.cs', 'meta.field.declaration.cs', 'meta.method-call.cs', 'meta.method.cs']
expect(tokens[1][6]).toEqual value: '(', scopes: ['source.cs', 'meta.field.declaration.cs', 'meta.method-call.cs', 'punctuation.definition.method-parameters.begin.cs']
expect(tokens[1][7]).toEqual value: '"', scopes: ['source.cs', 'meta.field.declaration.cs', 'meta.method-call.cs', 'string.quoted.double.cs', 'punctuation.definition.string.begin.cs']
expect(tokens[1][8]).toEqual value: '1.1 10', scopes: ['source.cs', 'meta.field.declaration.cs', 'meta.method-call.cs', 'string.quoted.double.cs']
expect(tokens[1][9]).toEqual value: '\\n', scopes: ['source.cs', 'meta.field.declaration.cs', 'meta.method-call.cs', 'string.quoted.double.cs', 'constant.character.escape.cs']
expect(tokens[1][10]).toEqual value: '"', scopes: ['source.cs', 'meta.field.declaration.cs', 'meta.method-call.cs', 'string.quoted.double.cs', 'punctuation.definition.string.end.cs']
expect(tokens[1][11]).toEqual value: ')', scopes: ['source.cs', 'meta.field.declaration.cs', 'meta.method-call.cs', 'punctuation.definition.method-parameters.end.cs']

it "tokenizes strings in classes", ->
tokens = grammar.tokenizeLines """
Expand All @@ -91,25 +92,10 @@ describe "Language C# package", ->
}
"""

expect(tokens[2][5]).toEqual value: '"', scopes: ['source.cs', 'meta.class.cs', 'meta.class.body.cs', 'string.quoted.double.cs', 'punctuation.definition.string.begin.cs']
expect(tokens[2][6]).toEqual value: '(', scopes: ['source.cs', 'meta.class.cs', 'meta.class.body.cs', 'string.quoted.double.cs']
expect(tokens[2][7]).toEqual value: '"', scopes: ['source.cs', 'meta.class.cs', 'meta.class.body.cs', 'string.quoted.double.cs', 'punctuation.definition.string.end.cs']
expect(tokens[2][8]).toEqual value: ';', scopes: ['source.cs', 'meta.class.cs', 'meta.class.body.cs']

tokens = grammar.tokenizeLines """
class a
{
b([c(d = "Command e")] string f)
{
}
}
"""

expect(tokens[2][7]).toEqual value: '"', scopes: ['source.cs', 'meta.class.cs', 'meta.class.body.cs', 'meta.method-call.cs', 'meta.method-call.cs', 'string.quoted.double.cs', 'punctuation.definition.string.begin.cs']
expect(tokens[2][8]).toEqual value: 'Command e', scopes: ['source.cs', 'meta.class.cs', 'meta.class.body.cs', 'meta.method-call.cs', 'meta.method-call.cs', 'string.quoted.double.cs']
expect(tokens[2][9]).toEqual value: '"', scopes: ['source.cs', 'meta.class.cs', 'meta.class.body.cs', 'meta.method-call.cs', 'meta.method-call.cs', 'string.quoted.double.cs', 'punctuation.definition.string.end.cs']
expect(tokens[2][12]).toEqual value: 'string ', scopes: ['source.cs', 'meta.class.cs', 'meta.class.body.cs', 'meta.method-call.cs', 'storage.reference.type.cs']
expect(tokens[2][13]).toEqual value: 'f', scopes: ['source.cs', 'meta.class.cs', 'meta.class.body.cs', 'meta.method-call.cs']
expect(tokens[2][5]).toEqual value: '"', scopes: ['source.cs', 'meta.field.declaration.cs', 'string.quoted.double.cs', 'punctuation.definition.string.begin.cs']

This comment has been minimized.

Copy link
@tomchkk

tomchkk Jan 7, 2017

Contributor

@damieng Why is 'meta.class.cs' no longer part of the required scope here?

This comment has been minimized.

Copy link
@damieng

damieng Jan 7, 2017

Author Contributor

It was removed as it messes up a whole bunch of syntax highlighting in various themes. It also doesn't make much sense as you can have classes in classes etc and so it starts nesting.

Are there some themes/scenarios that were actually using it?

This comment has been minimized.

Copy link
@tomchkk

tomchkk Jan 8, 2017

Contributor

I'm using a fork of this grammar for something completely different. But it seemed to me that the very essence of scopes is that they are nested.

I can see why having more generalised scopes might not usually matter for syntax-highlighting, but say you wanted – for whatever reason – to highlight nested classes differently to non-nested classes, the scope wouldn't give you that level of detail anymore. You'd only know that a nested class is a class.

Likewise, if a scope doesn't tell you that a method-call is within a class, for instance, then as far as your syntax highlighting is concerned that method-call could be anywhere in the source.

This comment has been minimized.

Copy link
@damieng

damieng Jan 8, 2017

Author Contributor

The problem from a syntax highlighting point of view is it's very hard to use the meta classes without messing things up because of precedence rules. Some of the themes I tried had overridden meta.class which basically wiped out all the syntax highlighting.

This comment has been minimized.

Copy link
@winstliu

winstliu Jan 8, 2017

Contributor

That's really unfortunate that those themes try to style meta.class. Meta scopes are meant to be used for autocomplete, not styling (as long as autocomplete could theoretically benefit from those scopes, unlike this).

Overall, I agree that meta.class should exist, but if themes are attempting to style it, then I guess this is the better short-term solution.

This comment has been minimized.

Copy link
@damieng

damieng via email Jan 8, 2017

Author Contributor
expect(tokens[2][6]).toEqual value: '(', scopes: ['source.cs', 'meta.field.declaration.cs', 'string.quoted.double.cs']
expect(tokens[2][7]).toEqual value: '"', scopes: ['source.cs', 'meta.field.declaration.cs', 'string.quoted.double.cs', 'punctuation.definition.string.end.cs']
expect(tokens[2][8]).toEqual value: ';', scopes: ['source.cs']

describe "Preprocessor directives", ->
directives = [ '#if DEBUG', '#else', '#elif RELEASE', '#endif',
Expand Down

0 comments on commit 4bf9d94

Please sign in to comment.