From 524f224da4136ac7e50d9b8046f27aac701d5b6e Mon Sep 17 00:00:00 2001 From: Cory Jones Date: Tue, 4 Aug 2015 12:41:44 -0400 Subject: [PATCH 1/3] Add support for Python3 parameter annotations --- grammars/python.cson | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/grammars/python.cson b/grammars/python.cson index 35a11ca..c50bafb 100644 --- a/grammars/python.cson +++ b/grammars/python.cson @@ -289,7 +289,7 @@ 'name': 'variable.parameter.function.python' '2': 'name': 'punctuation.separator.parameters.python' - 'match': '\\b([a-zA-Z_][a-zA-Z_0-9]*)\\s*(?:(,)|(?=[\\n\\)]))' + 'match': '\\b([a-zA-Z_][a-zA-Z_0-9]*)\\s*(?:(,)|\\:\\s*\\w*(,*))' } ] } From 35028ee65810a327dc568ee7299b2c8f69cfe7c5 Mon Sep 17 00:00:00 2001 From: Dario Bertini Date: Sat, 19 Sep 2015 23:21:18 +0100 Subject: [PATCH 2/3] Add function annotations --- grammars/python.cson | 14 +++++++++++--- spec/python-spec.coffee | 13 +++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/grammars/python.cson b/grammars/python.cson index 3d56e0f..3f512d3 100644 --- a/grammars/python.cson +++ b/grammars/python.cson @@ -238,13 +238,17 @@ 'beginCaptures': '1': 'name': 'storage.type.function.python' - 'end': '(\\))\\s*(?:(\\:)|(.*$\\n?))' + 'end': '(\\))\\s*(?:(->)\\s*([A-Za-z_][A-Za-z0-9_]*)\\s*)?(?:(\\:)|(.*$\\n?))' 'endCaptures': '1': 'name': 'punctuation.definition.parameters.end.python' '2': - 'name': 'punctuation.section.function.begin.python' + 'name': 'punctuation.definition.annotation.return.python' '3': + 'name': 'variable.annotation.function.python' + '4': + 'name': 'punctuation.section.function.begin.python' + '5': 'name': 'invalid.illegal.missing-section-begin.python' 'name': 'meta.function.python' 'patterns': [ @@ -277,8 +281,12 @@ '1': 'name': 'variable.parameter.function.python' '2': + 'name': 'punctuation.definition.annotation.parameter.python' + '3': + 'name': 'variable.annotation.function.python' + '4': 'name': 'punctuation.separator.parameters.python' - 'match': '\\b([a-zA-Z_][a-zA-Z_0-9]*)\\s*(?:(,)|\\:\\s*\\w*(,*))' + 'match': '\\b([a-zA-Z_][a-zA-Z_0-9]*)\\s*(?:(:)\\s*([a-zA-Z_][a-zA-Z_0-9]*))?(?:(,)|(?=[\\n\\)]))' } ] } diff --git a/spec/python-spec.coffee b/spec/python-spec.coffee index d586be2..b1d4dc5 100644 --- a/spec/python-spec.coffee +++ b/spec/python-spec.coffee @@ -296,3 +296,16 @@ describe "Python grammar", -> expect(tokens[1][0]).toEqual value: 'SELECT bar', scopes: ['source.python', scope] expect(tokens[2][0]).toEqual value: 'FROM foo', scopes: ['source.python', scope] expect(tokens[3][0]).toEqual value: delim, scopes: ['source.python', scope, 'punctuation.definition.string.end.python'] + it "tokenizes a function definition with annotations", -> + tokens = grammar.tokenizeLines('def f(a: int) -> int:') + + expect(tokens[0][0]).toEqual value: 'def', scopes: ['source.python', 'meta.function.python', 'storage.type.function.python'] + expect(tokens[0][2]).toEqual value: 'f', scopes: ['source.python', 'meta.function.python', 'entity.name.function.python', 'support.function.magic.python'] + expect(tokens[0][3]).toEqual value: '(', scopes: ['source.python', 'meta.function.python', 'punctuation.definition.parameters.begin.python'] + expect(tokens[1][1]).toEqual value: 'a', scopes: ['source.python', 'meta.function.python', 'meta.function.parameters.python', 'variable.parameter.function.python'] + expect(tokens[1][2]).toEqual value: ':', scopes: ['source.python', 'meta.function.python', 'meta.function.parameters.python', 'punctuation.definition.annotation.parameter.python'] + expect(tokens[2][1]).toEqual value: 'int', scopes: ['source.python', 'meta.function.python', 'meta.function.parameters.python', 'variable.annotation.function.python'] + expect(tokens[4][0]).toEqual value: ')', scopes: ['source.python', 'meta.function.python', 'punctuation.definition.parameters.end.python'] + expect(tokens[4][0]).toEqual value: '->', scopes: ['source.python', 'meta.function.python', 'punctuation.definition.annotation.return.python'] + expect(tokens[4][0]).toEqual value: 'int', scopes: ['source.python', 'meta.function.python', 'variable.annotation.function.python'] + expect(tokens[4][1]).toEqual value: ':', scopes: ['source.python', 'meta.function.python', 'punctuation.section.function.begin.python'] From a4100e14f218c86c1110ce47e5889d141829f91b Mon Sep 17 00:00:00 2001 From: Dario Bertini Date: Sat, 17 Oct 2015 13:02:14 +0100 Subject: [PATCH 3/3] Apply some fixes --- grammars/python.cson | 2 +- spec/python-spec.coffee | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/grammars/python.cson b/grammars/python.cson index 3f512d3..2350e86 100644 --- a/grammars/python.cson +++ b/grammars/python.cson @@ -245,7 +245,7 @@ '2': 'name': 'punctuation.definition.annotation.return.python' '3': - 'name': 'variable.annotation.function.python' + 'name': 'variable.other.annotation' '4': 'name': 'punctuation.section.function.begin.python' '5': diff --git a/spec/python-spec.coffee b/spec/python-spec.coffee index b1d4dc5..4188729 100644 --- a/spec/python-spec.coffee +++ b/spec/python-spec.coffee @@ -300,7 +300,7 @@ describe "Python grammar", -> tokens = grammar.tokenizeLines('def f(a: int) -> int:') expect(tokens[0][0]).toEqual value: 'def', scopes: ['source.python', 'meta.function.python', 'storage.type.function.python'] - expect(tokens[0][2]).toEqual value: 'f', scopes: ['source.python', 'meta.function.python', 'entity.name.function.python', 'support.function.magic.python'] + expect(tokens[0][2]).toEqual value: 'f', scopes: ['source.python', 'meta.function.python', 'entity.name.function.python'] expect(tokens[0][3]).toEqual value: '(', scopes: ['source.python', 'meta.function.python', 'punctuation.definition.parameters.begin.python'] expect(tokens[1][1]).toEqual value: 'a', scopes: ['source.python', 'meta.function.python', 'meta.function.parameters.python', 'variable.parameter.function.python'] expect(tokens[1][2]).toEqual value: ':', scopes: ['source.python', 'meta.function.python', 'meta.function.parameters.python', 'punctuation.definition.annotation.parameter.python']