--- name: MagicPython scopeName: source.python # NOTE: remember to update package.json with VSCode file types. fileTypes: [py, py3, rpy, pyw, cpy, pyi, SConstruct, Sconstruct, sconstruct, SConscript, gyp, gypi, wsgi, kv, Snakefile, smk, tac] firstLineMatch: ^#![ \t]*/.*\bpython[\d\.]*\b uuid: 742deb57-6e38-4192-bed6-410746efd85d patterns: - include: '#statement' - include: '#expression' repository: $apply: - file: 'regexp-common.inc.syntax.yaml' - file: 'comment.inc.syntax.yaml' vars: suffix: '-base' marker: "" - file: 'comment.inc.syntax.yaml' vars: suffix: '-string-single-three' marker: "|(?=''')" - file: 'comment.inc.syntax.yaml' vars: suffix: '-string-double-three' marker: '|(?=""")' - file: 'regexp.inc.syntax.yaml' vars: prefix: 'single-one-' basename: 'regexp' marker: "|(?=\\')" nested: '' guard: "|((?=(?)) - name: comment.typehint.variable.notation.python match: ([[:alpha:]_]\w*) - include: '#comments-base' docstring-statement: begin: ^(?=\s*[rR]?(\'\'\'|\"\"\"|\'|\")) comment: the string either terminates correctly or by the beginning of a new line (this is for single line docstrings that aren't terminated) AND it's not followed by another docstring end: ((?<=\1)|^)(?!\s*[rR]?(\'\'\'|\"\"\"|\'|\")) patterns: - include: '#docstring' docstring: patterns: - name: string.quoted.docstring.multi.python begin: (\'\'\'|\"\"\") end: (\1) beginCaptures: '1': {name: punctuation.definition.string.begin.python} endCaptures: '1': {name: punctuation.definition.string.end.python} patterns: - include: '#docstring-prompt' - include: '#codetags' - include: '#docstring-guts-unicode' - name: string.quoted.docstring.raw.multi.python begin: ([rR])(\'\'\'|\"\"\") end: (\2) beginCaptures: '1': {name: storage.type.string.python} '2': {name: punctuation.definition.string.begin.python} endCaptures: '1': {name: punctuation.definition.string.end.python} patterns: - include: '#string-consume-escape' - include: '#docstring-prompt' - include: '#codetags' - name: string.quoted.docstring.single.python begin: (\'|\") end: (\1)|(\n) beginCaptures: '1': {name: punctuation.definition.string.begin.python} endCaptures: '1': {name: punctuation.definition.string.end.python} '2': {name: invalid.illegal.newline.python} patterns: - include: '#codetags' - include: '#docstring-guts-unicode' - name: string.quoted.docstring.raw.single.python begin: ([rR])(\'|\") end: (\2)|(\n) beginCaptures: '1': {name: storage.type.string.python} '2': {name: punctuation.definition.string.begin.python} endCaptures: '1': {name: punctuation.definition.string.end.python} '2': {name: invalid.illegal.newline.python} patterns: - include: '#string-consume-escape' - include: '#codetags' docstring-guts-unicode: patterns: - include: '#escape-sequence-unicode' - include: '#escape-sequence' - include: '#string-line-continuation' docstring-prompt: match: | (?x) (?: (?:^|\G) \s* (?# '\G' is necessary for ST) ((?:>>>|\.\.\.) \s) (?=\s*\S) ) captures: '1': {name: keyword.control.flow.python} statement-keyword: patterns: - name: storage.type.function.python match: \b((async\s+)?\s*def)\b - name: keyword.control.flow.python comment: | if `as` is eventually followed by `:` or line continuation it's probably control flow like: with foo as bar, \ Foo as Bar: try: do_stuff() except Exception as e: pass match: \b(?>= | //= | \*\*= | \+= | -= | /= | @= | \*= | %= | ~= | \^= | &= | \|= | =(?!=) operator: match: | (?x) \b(?> | & | \| | \^ | ~) (?# 3) | (\*\* | \* | \+ | - | % | // | / | @) (?# 4) | (!= | == | >= | <= | < | >) (?# 5) captures: '1': {name: keyword.operator.logical.python} '2': {name: keyword.control.flow.python} '3': {name: keyword.operator.bitwise.python} '4': {name: keyword.operator.arithmetic.python} '5': {name: keyword.operator.comparison.python} punctuation: patterns: - name: punctuation.separator.colon.python match: ":" - name: punctuation.separator.element.python match: "," literal: patterns: - name: constant.language.python match: \b(True|False|None|NotImplemented|Ellipsis)\b - include: '#number' number: name: constant.numeric.python patterns: - include: '#number-float' - include: '#number-dec' - include: '#number-hex' - include: '#number-oct' - include: '#number-bin' - include: '#number-long' - name: invalid.illegal.name.python match: \b[0-9]+\w+ number-float: name: constant.numeric.float.python match: | (?x) (?=^]? [-+ ]? \#? \d* ,? (\.\d+)? [bcdeEfFgGnosxX%]? )? }) ) captures: # We use this awkward scope name instead of perhaps more # elegant "support.other.format.python" because it appears # that by default it gives a better visual result in various # systems (e.g. GitHub). This is due to the fact that # "storage.type..." is more likely to have special # highlighting in any given color scheme than # "support.other..." '1': {name: constant.character.format.placeholder.other.python} '3': {name: storage.type.format.python} '4': {name: storage.type.format.python} # The reason for 2 separate rules is to establish which one # should be matched with a higher priority. The rule below is # more generic, so it should be matched only if the specific one # didn't. - name: meta.format.brace.python match: | (?x) ( { \w* (\.[[:alpha:]_]\w* | \[[^\]'"]+\])* (![rsa])? (:) [^'"{}\n]* (?: \{ [^'"}\n]*? \} [^'"{}\n]* )* } ) captures: '1': {name: constant.character.format.placeholder.other.python} '3': {name: storage.type.format.python} '4': {name: storage.type.format.python} fstring-formatting: patterns: - include: '#fstring-formatting-braces' - include: '#fstring-formatting-singe-brace' fstring-formatting-singe-brace: name: invalid.illegal.brace.python match: (}(?!})) import: comment: | Import statements used to correctly mark `from`, `import`, and `as` patterns: - begin: \b(?) end: (?=:) beginCaptures: '1': {name: punctuation.separator.annotation.result.python} patterns: - include: '#expression' item-access: patterns: - name: meta.item-access.python begin: | (?x) \b(?= [[:alpha:]_]\w* \s* \[ ) end: (\]) endCaptures: '1': {name: punctuation.definition.arguments.end.python} patterns: - include: '#item-name' - include: '#item-index' - include: '#expression' item-name: patterns: - include: '#special-variables' - include: '#builtin-functions' - include: '#special-names' - name: meta.indexed-name.python match: | (?x) \b ([[:alpha:]_]\w*) \b item-index: begin: (\[) end: (?=\]) beginCaptures: '1': {name: punctuation.definition.arguments.begin.python} contentName: meta.item-access.arguments.python patterns: - name: punctuation.separator.slice.python match: ":" - include: '#expression' decorator: name: meta.function.decorator.python begin: | (?x) ^\s* ((@)) \s* (?=[[:alpha:]_]\w*) end: | (?x) ( \) ) # trailing whitespace and comments are legal (?: (.*?) (?=\s*(?:\#|$)) ) | (?=\n|\#) beginCaptures: '1': {name: entity.name.function.decorator.python} '2': {name: punctuation.definition.decorator.python} endCaptures: '1': {name: punctuation.definition.arguments.end.python} '2': {name: invalid.illegal.decorator.python} patterns: - include: '#decorator-name' - include: '#function-arguments' decorator-name: patterns: - include: '#builtin-callables' - include: '#illegal-object-name' - name: entity.name.function.decorator.python match: | (?x) ([[:alpha:]_]\w*) | (\.) captures: '2': {name: punctuation.separator.period.python} - include: '#line-continuation' - name: invalid.illegal.decorator.python match: | (?x) \s* ([^([:alpha:]\s_\.#\\] .*?) (?=\#|$) captures: '1': {name: invalid.illegal.decorator.python} call-wrapper-inheritance: comment: same as a function call, but in inheritance context name: meta.function-call.python begin: | (?x) \b(?= ([[:alpha:]_]\w*) \s* (\() ) end: (\)) endCaptures: '1': {name: punctuation.definition.arguments.end.python} patterns: - include: '#inheritance-name' - include: '#function-arguments' inheritance-name: patterns: - include: '#lambda-incomplete' - include: '#builtin-possible-callables' - include: '#inheritance-identifier' function-call: name: meta.function-call.python comment: Regular function call of the type "name(args)" begin: | (?x) \b(?= ([[:alpha:]_]\w*) \s* (\() ) end: (\)) endCaptures: '1': {name: punctuation.definition.arguments.end.python} patterns: - include: '#special-variables' - include: '#function-name' - include: '#function-arguments' function-name: patterns: - include: '#builtin-possible-callables' - comment: Some color schemas support meta.function-call.generic scope name: meta.function-call.generic.python match: | (?x) \b ([[:alpha:]_]\w*) \b function-arguments: begin: (\() end: (?=\))(?!\)\s*\() beginCaptures: '1': {name: punctuation.definition.arguments.begin.python} contentName: meta.function-call.arguments.python patterns: - name: punctuation.separator.arguments.python match: (,) - match: | (?x) (?:(?<=[,(])|^) \s* (\*{1,2}) captures: '1': {name: keyword.operator.unpacking.arguments.python} - include: '#lambda-incomplete' - include: '#illegal-names' - match: '\b([[:alpha:]_]\w*)\s*(=)(?!=)' captures: '1': {name: variable.parameter.function-call.python} '2': {name: keyword.operator.assignment.python} - name: keyword.operator.assignment.python match: =(?!=) - include: '#expression' - match: \s*(\))\s*(\() captures: '1': {name: punctuation.definition.arguments.end.python} '2': {name: punctuation.definition.arguments.begin.python} builtin-callables: patterns: - include: '#illegal-names' - include: '#illegal-object-name' - include: '#builtin-exceptions' - include: '#builtin-functions' - include: '#builtin-types' builtin-possible-callables: patterns: - include: '#builtin-callables' - include: '#magic-names' builtin-exceptions: name: support.type.exception.python match: | (?x) (?' ...