From e82ed75da511a9271f485ae67a0d6b84e2668d61 Mon Sep 17 00:00:00 2001 From: Caleb Donovick Date: Tue, 18 Dec 2018 17:35:56 -0800 Subject: [PATCH 1/6] Enable syntax highlighting in f-strings --- syntax/python.vim | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/syntax/python.vim b/syntax/python.vim index a7f6edc..63224b1 100644 --- a/syntax/python.vim +++ b/syntax/python.vim @@ -103,7 +103,7 @@ else syn match pythonStatement '\' nextgroup=pythonFunction skipwhite syn match pythonStatement '\' syn match pythonStatement '\' - syn cluster pythonExpression contains=pythonStatement,pythonRepeat,pythonConditional,pythonOperator,pythonNumber,pythonHexNumber,pythonOctNumber,pythonBinNumber,pythonFloat,pythonString,pythonBytes,pythonBoolean,pythonNone,pythonSingleton,pythonBuiltinObj,pythonBuiltinFunc,pythonBuiltinType + syn cluster pythonExpression contains=pythonStatement,pythonRepeat,pythonConditional,pythonOperator,pythonNumber,pythonHexNumber,pythonOctNumber,pythonBinNumber,pythonFloat,pythonString,pythonFString,pythonRawString,pythonRawFString,pythonBytes,pythonBoolean,pythonNone,pythonSingleton,pythonBuiltinObj,pythonBuiltinFunc,pythonBuiltinType endif @@ -264,8 +264,8 @@ if s:Enabled('g:python_highlight_string_format') syn match pythonStrFormat '{\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)\=\%(\.\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\[\%(\d\+\|[^!:\}]\+\)\]\)*\%(![rsa]\)\=\%(:\%({\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)}\|\%([^}]\=[<>=^]\)\=[ +-]\=#\=0\=\d*,\=\%(\.\d\+\)\=[bcdeEfFgGnosxX%]\=\)\=\)\=}' contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString else syn match pythonStrFormat "{\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)\=\%(\.\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\[\%(\d\+\|[^!:\}]\+\)\]\)*\%(![rsa]\)\=\%(:\%({\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)}\|\%([^}]\=[<>=^]\)\=[ +-]\=#\=0\=\d*,\=\%(\.\d\+\)\=[bcdeEfFgGnosxX%]\=\)\=\)\=}" contained containedin=pythonString,pythonRawString - syn region pythonStrInterpRegion start="{"he=e+1,rs=e+1 end="\%(![rsa]\)\=\%(:\%({\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)}\|\%([^}]\=[<>=^]\)\=[ +-]\=#\=0\=\d*,\=\%(\.\d\+\)\=[bcdeEfFgGnosxX%]\=\)\=\)\=}"hs=s-1,re=s-1 extend contained containedin=pythonFString,pythonRawFString contains=pythonStrInterpRegion,@pythonExpression - syn match pythonStrFormat "{{\|}}" contained containedin=pythonString,pythonRawString,pythonFString,pythonRawFString + syn region pythonStrInterpRegion matchgroup=pythonStrFormat start="\({\)\@=^]\)\=[ +-]\=#\=0\=\d*,\=\%(\.\d\+\)\=[bcdeEfFgGnosxX%]\=\)\=\)\=\(}\)\@= 508 || !exists('did_python_syn_inits') HiLink pythonBytesEscapeError Error HiLink pythonFString String HiLink pythonRawFString String - HiLink pythonStrInterpRegion Special endif HiLink pythonStrFormatting Special From 082a78ac7d32896928bffa76df7cf2df90a4a12b Mon Sep 17 00:00:00 2001 From: nfnty Date: Wed, 2 Dec 2020 18:12:54 +0000 Subject: [PATCH 2/6] Fix f-string boundaries and add more tests --- syntax/python.vim | 2 +- tests/test.py | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/syntax/python.vim b/syntax/python.vim index 63224b1..cd26b76 100644 --- a/syntax/python.vim +++ b/syntax/python.vim @@ -264,7 +264,7 @@ if s:Enabled('g:python_highlight_string_format') syn match pythonStrFormat '{\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)\=\%(\.\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\[\%(\d\+\|[^!:\}]\+\)\]\)*\%(![rsa]\)\=\%(:\%({\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)}\|\%([^}]\=[<>=^]\)\=[ +-]\=#\=0\=\d*,\=\%(\.\d\+\)\=[bcdeEfFgGnosxX%]\=\)\=\)\=}' contained containedin=pythonString,pythonUniString,pythonUniRawString,pythonRawString else syn match pythonStrFormat "{\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)\=\%(\.\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\[\%(\d\+\|[^!:\}]\+\)\]\)*\%(![rsa]\)\=\%(:\%({\%(\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*\|\d\+\)}\|\%([^}]\=[<>=^]\)\=[ +-]\=#\=0\=\d*,\=\%(\.\d\+\)\=[bcdeEfFgGnosxX%]\=\)\=\)\=}" contained containedin=pythonString,pythonRawString - syn region pythonStrInterpRegion matchgroup=pythonStrFormat start="\({\)\@=^]\)\=[ +-]\=#\=0\=\d*,\=\%(\.\d\+\)\=[bcdeEfFgGnosxX%]\=\)\=\)\=\(}\)\@=^]\)\=[ +-]\=#\=0\=\d*,\=\%(\.\d\+\)\=[bcdeEfFgGnosxX%]\=\)\=\)\=}" extend contained containedin=pythonFString,pythonRawFString contains=pythonStrInterpRegion,@pythonExpression syn match pythonStrFormat "{{\|}}" contained containedin=pythonFString,pythonRawFString endif endif diff --git a/tests/test.py b/tests/test.py index 246d109..f37aedc 100644 --- a/tests/test.py +++ b/tests/test.py @@ -244,8 +244,11 @@ async def Test f"{var}...{arr[123]} normal {var['{'] // 0xff} \"xzcb\" 'xzcb' {var['}'] + 1} text" f"{expr1 if True or False else expr2} {None} wow {','.join(c.lower() for c in 'asdf')}" f"hello {expr:.2f} yes {(lambda: 0b1)():#03x} lol {var!r}" -f'brackets: {{ 1 + 2 }} and {{{{ 3 + 4 }}}}' +f'brackets: {{{ 1 + 2 }}} and {{{{ 3 + 4 }}}}' fr'this {that}' +f"{f'{1+1}'}" +'{{ }}' +f"{"{test}"}" # FIXME: syntax error that should not be highlighted # Doctests. From 2ccfb142e77c20454d0debbc5c3d2f07513bf709 Mon Sep 17 00:00:00 2001 From: fishsouprecipe Date: Fri, 25 Sep 2020 09:24:21 +0300 Subject: [PATCH 3/6] Add `mcs` to class variables Closes #74 --- README.md | 4 ++-- doc/python-syntax.txt | 4 ++-- syntax/python.vim | 2 +- tests/test.py | 2 ++ 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 922c335..382472e 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Features * New exceptions and builtins * Doctests * `@decorator` syntax - * Class variables such as `self` and `cls` + * Class variables such as `self`, `cls`, and `mcs` * Operators * Highlighting of the following errors: * Invalid symbols in source file @@ -72,7 +72,7 @@ let g:python_highlight_all = 1 | `g:python_highlight_space_errors` | Highlight trailing spaces | `0` | | `g:python_highlight_doctests` | Highlight doc-tests | `0` | | `g:python_highlight_func_calls` | Highlight functions calls | `0` | -| `g:python_highlight_class_vars` | Highlight class variables `self` and `cls` | `0` | +| `g:python_highlight_class_vars` | Highlight class variables `self`, `cls`, and `mcs` | `0` | | `g:python_highlight_operators` | Highlight all operators | `0` | | `g:python_highlight_all` | Enable all highlight options above, except for previously set. | `0` | | `g:python_highlight_file_headers_as_comments` | Highlight shebang and coding headers as comments | `0` | diff --git a/doc/python-syntax.txt b/doc/python-syntax.txt index 59eb0ef..79c569e 100644 --- a/doc/python-syntax.txt +++ b/doc/python-syntax.txt @@ -21,7 +21,7 @@ Features * New exceptions and builtins * Doctests * `@decorator` syntax - * Class variables such as `self` and `cls` + * Class variables such as `self`, `cls`, and `mcs` * Operators * Highlighting of the following errors: * Invalid symbols in source file @@ -96,7 +96,7 @@ following command to your `~/.config/nvim/init.vim` or `~/.vimrc`: > Highlight functions calls `g:python_highlight_class_vars` (default `0`) - Highlight class variables `self` and `cls` + Highlight class variables `self`, `cls`, and `mcs` `g:python_highlight_operators` (default `0`) Highlight all operators diff --git a/syntax/python.vim b/syntax/python.vim index cd26b76..3ff422f 100644 --- a/syntax/python.vim +++ b/syntax/python.vim @@ -76,7 +76,7 @@ syn keyword pythonStatement break continue del return pass yield global asse syn keyword pythonStatement raise nextgroup=pythonExClass skipwhite syn keyword pythonStatement def class nextgroup=pythonFunction skipwhite if s:Enabled('g:python_highlight_class_vars') - syn keyword pythonClassVar self cls + syn keyword pythonClassVar self cls mcs endif syn keyword pythonRepeat for while syn keyword pythonConditional if elif else diff --git a/tests/test.py b/tests/test.py index f37aedc..20555a4 100644 --- a/tests/test.py +++ b/tests/test.py @@ -11,6 +11,8 @@ with break continue del return pass raise global assert lambda yield for while if elif else import as try except finally +self cls mcs + from test import var as name raise Exception from ex From 0ad21fd728a486535ebcc8667cf77675b3f9b337 Mon Sep 17 00:00:00 2001 From: nfnty Date: Wed, 2 Dec 2020 19:57:28 +0000 Subject: [PATCH 4/6] Highlight class variables inside f-strings Fixes #56 --- syntax/python.vim | 2 +- tests/test.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/syntax/python.vim b/syntax/python.vim index 3ff422f..80135a1 100644 --- a/syntax/python.vim +++ b/syntax/python.vim @@ -103,7 +103,7 @@ else syn match pythonStatement '\' nextgroup=pythonFunction skipwhite syn match pythonStatement '\' syn match pythonStatement '\' - syn cluster pythonExpression contains=pythonStatement,pythonRepeat,pythonConditional,pythonOperator,pythonNumber,pythonHexNumber,pythonOctNumber,pythonBinNumber,pythonFloat,pythonString,pythonFString,pythonRawString,pythonRawFString,pythonBytes,pythonBoolean,pythonNone,pythonSingleton,pythonBuiltinObj,pythonBuiltinFunc,pythonBuiltinType + syn cluster pythonExpression contains=pythonStatement,pythonRepeat,pythonConditional,pythonOperator,pythonNumber,pythonHexNumber,pythonOctNumber,pythonBinNumber,pythonFloat,pythonString,pythonFString,pythonRawString,pythonRawFString,pythonBytes,pythonBoolean,pythonNone,pythonSingleton,pythonBuiltinObj,pythonBuiltinFunc,pythonBuiltinType,pythonClassVar endif diff --git a/tests/test.py b/tests/test.py index 20555a4..2880a13 100644 --- a/tests/test.py +++ b/tests/test.py @@ -251,6 +251,7 @@ async def Test f"{f'{1+1}'}" '{{ }}' f"{"{test}"}" # FIXME: syntax error that should not be highlighted +f'{self.__name__} # Doctests. From 83d4c63f772c00d8a2dfd44673d7c75999b4ef91 Mon Sep 17 00:00:00 2001 From: koka Date: Sat, 8 Jul 2017 22:52:22 +0900 Subject: [PATCH 5/6] Implement python class name highlighting Fixes #18 Closes #30 --- syntax/python.vim | 5 ++++- tests/test.py | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/syntax/python.vim b/syntax/python.vim index 80135a1..86b22c1 100644 --- a/syntax/python.vim +++ b/syntax/python.vim @@ -74,7 +74,8 @@ endif syn keyword pythonStatement break continue del return pass yield global assert lambda with syn keyword pythonStatement raise nextgroup=pythonExClass skipwhite -syn keyword pythonStatement def class nextgroup=pythonFunction skipwhite +syn keyword pythonStatement def nextgroup=pythonFunction skipwhite +syn keyword pythonStatement class nextgroup=pythonClass skipwhite if s:Enabled('g:python_highlight_class_vars') syn keyword pythonClassVar self cls mcs endif @@ -100,6 +101,7 @@ else syn keyword pythonStatement as nonlocal syn match pythonStatement '\v\.@' syn match pythonFunction '\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*' display contained + syn match pythonClass '\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*' display contained syn match pythonStatement '\' nextgroup=pythonFunction skipwhite syn match pythonStatement '\' syn match pythonStatement '\' @@ -499,6 +501,7 @@ if v:version >= 508 || !exists('did_python_syn_inits') HiLink pythonBuiltinType Structure HiLink pythonExClass Structure + HiLink pythonClass Structure HiLink pythonClassVar Identifier delcommand HiLink diff --git a/tests/test.py b/tests/test.py index 2880a13..61b91a1 100644 --- a/tests/test.py +++ b/tests/test.py @@ -26,9 +26,13 @@ def functionname test.functionname() test.functionname () class Classname +class classname +class classname_cls def функция функция() class Класс +class класс + # Keywords: Python 2 From 2cc00ba72929ea5f9456a26782db57fb4cc56a65 Mon Sep 17 00:00:00 2001 From: Joakker Date: Sun, 13 Dec 2020 23:07:57 -0300 Subject: [PATCH 6/6] Add walrus operator Closes #77 --- syntax/python.vim | 2 +- tests/test.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/syntax/python.vim b/syntax/python.vim index 86b22c1..2524aba 100644 --- a/syntax/python.vim +++ b/syntax/python.vim @@ -114,7 +114,7 @@ endif " syn keyword pythonOperator and in is not or if s:Enabled('g:python_highlight_operators') - syn match pythonOperator '\V=\|-\|+\|*\|@\|/\|%\|&\||\|^\|~\|<\|>\|!=' + syn match pythonOperator '\V=\|-\|+\|*\|@\|/\|%\|&\||\|^\|~\|<\|>\|!=\|:=' endif syn match pythonError '[$?]\|\([-+@%&|^~]\)\1\{1,}\|\([=*/<>]\)\2\{2,}\|\([+@/%&|^~<>]\)\3\@![-+*@/%&|^~<>]\|\*\*[*@/%&|^<>]\|=[*@/%&|^<>]\|-[+*@/%&|^~<]\|[]\+=\{2,}\|!\{2,}=\+' display diff --git a/tests/test.py b/tests/test.py index 61b91a1..c0f81ac 100644 --- a/tests/test.py +++ b/tests/test.py @@ -182,7 +182,7 @@ async def Test < <= == != >= > = =- =+ =~ --= += *= **= @= /= //= %= +-= += *= **= @= /= //= %= := &= |= ^= ~= <<= >>= ->