Skip to content

Commit

Permalink
[Python] Add syntax highlighting for type comments
Browse files Browse the repository at this point in the history
  • Loading branch information
TeamSpen210 committed Mar 25, 2019
1 parent 8c76dd3 commit 137308c
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
62 changes: 62 additions & 0 deletions Python/Python.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
27 changes: 27 additions & 0 deletions Python/syntax_test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 137308c

Please sign in to comment.