From 137308caf6c4d3f74b442e9bbbd04fbd300e3473 Mon Sep 17 00:00:00 2001 From: TeamSpen210 Date: Mon, 25 Mar 2019 08:14:55 +1000 Subject: [PATCH] [Python] Add syntax highlighting for type comments --- Python/Python.sublime-syntax | 62 ++++++++++++++++++++++++++++++++++++ Python/syntax_test_python.py | 27 ++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/Python/Python.sublime-syntax b/Python/Python.sublime-syntax index d15e5eb32c..639a92f163 100644 --- a/Python/Python.sublime-syntax +++ b/Python/Python.sublime-syntax @@ -402,6 +402,68 @@ contexts: pop: true comments: + # Type-ignore must be the only hint allowed! + - match: '(#)\s*(type)(:)\s*(ignore)' + captures: + 1: punctuation.definition.comment.python comment.line.number-sign.python + 2: keyword.other.annotation.type-comment + 3: punctuation.separator.annotation.type-comment + 4: keyword.other.annotation.type-comment.ignore + set: + - meta_scope: comment.line.type-hint + - include: comments_noannotation + - match: \n + pop: true + - match: \S + scope: invalid.illegal + - match: '(#)\s*(type)(:)' + scope: comment.line.type-hint + captures: + 1: punctuation.definition.comment.python comment.line.number-sign.python + 2: keyword.other.annotation.type-comment + 3: punctuation.separator.annotation.type-comment + embed: type-comment + escape: '\n' + embed_scope: comment.line.type-hint + + - include: comments_noannotation + + type-comment: + - include: comments_noannotation + + # Don't use expressions here, only a few types are reasonable + # as annotations. + - include: constants + - include: builtin-exceptions + - include: builtin-types + - include: builtin-functions + - match: '{{identifier}}' + scope: support.class.python + + - match: '\s*(\[)' + captures: + 1: meta.item-access.python punctuation.section.brackets.begin.python + push: + - meta_content_scope: meta.item-access.arguments.python + - match: \] + scope: meta.item-access.python punctuation.section.brackets.end.python + pop: true + - match: ':' + scope: punctuation.separator.slice.python + - include: type-comment + + - match: ',' + scope: punctuation.separator.sequence.python + - match: \) + scope: invalid.illegal.stray.brace.round.python + - match: \] + scope: invalid.illegal.stray.brace.square.python + - match: \} + scope: invalid.illegal.stray.brace.curly.python + - match: \S + scope: invalid.illegal + + comments_noannotation: - match: "#" scope: punctuation.definition.comment.python push: diff --git a/Python/syntax_test_python.py b/Python/syntax_test_python.py index 1302866116..cefdb191f5 100644 --- a/Python/syntax_test_python.py +++ b/Python/syntax_test_python.py @@ -1350,6 +1350,33 @@ class Starship: # ^ punctuation.separator.annotation.variable.python # ^ keyword.operator.assignment +primes = 5 # type: ignore # type: not-a-type-comment +# ^ comment.line.type-hint punctuation.definition.comment.python comment.line.number-sign.python +# ^^^^ comment.line.type-hint keyword.other.annotation.type-comment +# ^ comment.line.type-hint punctuation.separator.annotation.type-comment +# ^^^^^^ comment.line.type-hint keyword.other.annotation.type-comment.ignore +# ^ comment.line.type-hint comment.line.number-sign.python punctuation.definition.comment.python +# ^^^^^^^^^^^^^^^^^^^^^^^^ comment.line.type-hint comment.line.number-sign.python + +primes = 5 # type: List[Dict[property:str, ...]], bool # comment +# ^ comment.line.type-hint punctuation.definition.comment.python comment.line.number-sign.python +# ^^^^ comment.line.type-hint keyword.other.annotation.type-comment +# ^ comment.line.type-hint punctuation.separator.annotation.type-comment +# ^^^^ comment.line.type-hint support.class.python +# ^ comment.line.type-hint meta.item-access.python punctuation.section.brackets.begin.python +# ^^^^ comment.line.type-hint meta.item-access.arguments.python support.class.python +# ^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.python punctuation.section.brackets.begin.python +# ^^^^^^^^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.arguments.python support.function.builtin.python +# ^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.arguments.python punctuation.separator.slice.python +# ^^^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.arguments.python support.type.python +# ^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.arguments.python punctuation.separator.sequence.python +# ^^^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.arguments.python constant.language.python +# ^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.python punctuation.section.brackets.end.python +# ^ comment.line.type-hint meta.item-access.python punctuation.section.brackets.end.python +# ^ comment.line.type-hint punctuation.separator.sequence.python +# ^^^^ comment.line.type-hint support.type.python +# ^ comment.line.type-hint comment.line.number-sign.python punctuation.definition.comment.python + # <- - meta # ensure we're not leaking a context