Skip to content

Commit

Permalink
[Python] Refactor decorator matching
Browse files Browse the repository at this point in the history
Adheres to conventions discussed in
sublimehq#709 and
sublimehq#737.
  • Loading branch information
FichteFoll committed Dec 19, 2016
1 parent 2483930 commit ab72cdd
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 17 deletions.
44 changes: 39 additions & 5 deletions Python/Python.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -516,14 +516,48 @@ contexts:
pop: true

decorators:
- match: '^\s*(?=@)'
- match: ^\s*(?=@)
push:
- meta_content_scope: meta.statement.decorator.python
# Due to line continuations, we don't know whether this is a "function call" yet
- meta_content_scope: meta.annotation.python
- match: '@'
scope: keyword.other.decorator.python
scope: punctuation.definition.annotation.python
- match: $
pop: true
- include: line-continuation-or-pop
- include: function-calls
- include: qualified-name
- match: (?=\.?\s*{{path}}\s*\() # now we do
set: [decorator-function-call-wrapper, qualified-name-until-leaf]
- match: (?=\.?\s*{{path}})
push: [decorator-wrapper, qualified-name-until-leaf]
- match: \S
scope: invalid.illegal.character.python
pop: true

decorator-wrapper:
- meta_scope: meta.annotation.python
- match: '{{identifier}}'
scope: meta.qualified-name.python variable.annotation.python
- match: ''
pop: true

decorator-function-call-wrapper:
- meta_scope: meta.annotation.function.python
- match: '{{identifier}}'
scope: meta.qualified-name.python variable.annotation.function.python
- match: \)
scope: punctuation.section.arguments.end.python
set: after-expression
- match: \(
scope: punctuation.section.arguments.begin.python
push:
- meta_content_scope: meta.annotation.arguments.python
- match: (?=\))
pop: true
- include: keyword-arguments
- match: ','
scope: punctuation.separator.arguments.python
- include: inline-for
- include: expressions

item-access:
- match: '(?={{path}}\s*\[)'
Expand Down
71 changes: 59 additions & 12 deletions Python/syntax_test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,21 +471,68 @@ class Unterminated(Inherited:
# ^ invalid.illegal


@normal . decorator
#^^^^^^^^^^^^^^^^^^ meta.statement.decorator
# <- keyword.other.decorator
# ^ punctuation.accessor.dot
##################
# Decorators
##################

@ normal . decorator
# <- meta.annotation punctuation.definition.annotation
#^^^^^^^^^^^^^^^^^^^ meta.annotation
# ^^^^^^^^^^^^^^^^^^ meta.qualified-name
# ^^^^^^ meta.generic-name - variable.annotation
# ^^^^^^^^^ variable.annotation
# ^ punctuation.accessor.dot
# ^ - meta.annotation
class Class():

@wraps(method, 12)# comment
#^^^ - meta.statement.decorator
# ^^^^^^^^^^^^^^^^^^ meta.statement.decorator
# ^ keyword.other.decorator
# ^^^^^^^^^^^^^^^^^ meta.function-call
# ^^^^^^^^^ comment
@functools.wraps(method, 12, kwarg=None)# comment
#^^^ - meta.annotation
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.annotation.function
# ^ punctuation.definition.annotation
# ^^^^^^^^^^^^^^^ meta.qualified-name
# ^^^^^^^^^ meta.generic-name - variable.annotation
# ^ punctuation.accessor.dot
# ^^^^^ variable.annotation.function
# ^ punctuation.section.arguments.begin
# ^ punctuation.separator.arguments
# ^^ constant.numeric
# ^^^^^ variable.parameter
# ^ keyword.operator
# ^^^^ constant.language
# ^ punctuation.separator.arguments
# ^ punctuation.section.arguments.end
# ^^^^^^^^^ comment - meta.annotation
def wrapper(self):
(self, __class__)
pass
return self.__class__(method)

@deco #comment
#^^^ - meta.annotation
# ^^^^^ meta.annotation
# ^^^^ meta.qualified-name variable.annotation.function
# ^^ - meta.annotation
# ^^^^^^^^ comment

@staticmethod
# ^^^^^^^^^^^^^ meta.annotation
# ^^^^^^^^^^^^ support.function.builtin
# ^ - meta.annotation

@deco[4]
# ^ invalid.illegal.character

@deco \
. rator
# ^^^^^^^ meta.annotation
# ^ punctuation.accessor.dot

@ deco \
. rator()
# ^^^^^^^ meta.annotation.function
# ^^^^^ variable.annotation.function

@ deco \
# ^^^^ meta.qualified-name meta.generic-name - variable.annotation
# ^ punctuation.separator.continuation.line


##################
Expand Down

0 comments on commit ab72cdd

Please sign in to comment.