From: Rob B Date: Mon, 26 Jan 2026 18:52:04 +0000 (+0000) Subject: runtime(python): Highlight built-in constants in Python X-Git-Tag: v9.1.2111~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=632fd8bb968b029d0a7c40a00101008ea5200b91;p=thirdparty%2Fvim.git runtime(python): Highlight built-in constants in Python Also add syntax tests for those newly constants. closes: #17788 closes: #18922 Signed-off-by: Rob B Signed-off-by: Christian Brabandt --- diff --git a/runtime/doc/syntax.txt b/runtime/doc/syntax.txt index 501dd94578..cc41a774a2 100644 --- a/runtime/doc/syntax.txt +++ b/runtime/doc/syntax.txt @@ -1,4 +1,4 @@ -*syntax.txt* For Vim version 9.1. Last change: 2026 Jan 25 +*syntax.txt* For Vim version 9.1. Last change: 2026 Jan 26 VIM REFERENCE MANUAL by Bram Moolenaar @@ -3223,7 +3223,7 @@ Pascal. Use this if you don't use assembly and Pascal: > PYTHON *python.vim* *ft-python-syntax* -There are six options to control Python syntax highlighting. +There are seven options to control Python syntax highlighting. For highlighted numbers: > :let python_no_number_highlight = 1 @@ -3243,10 +3243,13 @@ The first option implies the second one. For highlighted trailing whitespace and mix of spaces and tabs: > :let python_space_error_highlight = 1 +For highlighted built-in constants distinguished from other keywords: + :let python_constant_highlight = 1 + If you want all possible Python highlighting: > :let python_highlight_all = 1 -This has the same effect as setting python_space_error_highlight and -unsetting all the other ones. +This has the same effect as setting python_space_error_highlight, +python_constant_highlight and unsetting all the other ones. If you use Python 2 or straddling code (Python 2 and 3 compatible), you can enforce the use of an older syntax file with support for diff --git a/runtime/syntax/python.vim b/runtime/syntax/python.vim index 301fb84802..456a9825a2 100644 --- a/runtime/syntax/python.vim +++ b/runtime/syntax/python.vim @@ -4,6 +4,7 @@ " Last Change: 2025 Sep 08 " 2025 Sep 25 by Vim Project: fix wrong type highlighting #18394 " 2025 Dec 03 by Vim Project: highlight t-strings #18679 +" 2026 Jan 26 by Vim Project: highlight constants #18922 " Credits: Neil Schemenauer " Dmitry Vasiliev " Rob B @@ -34,6 +35,7 @@ " let python_no_exception_highlight = 1 " let python_no_number_highlight = 1 " let python_space_error_highlight = 1 +" let python_constant_highlight = 1 " " All the options above can be switched on together. " @@ -85,6 +87,7 @@ if exists("python_highlight_all") unlet python_no_number_highlight endif let python_space_error_highlight = 1 + let python_constant_highlight = 1 endif " Keep Python keywords in alphabetical order inside groups for easy @@ -97,7 +100,8 @@ endif " " python3 -c 'import keyword, pprint; pprint.pprint(keyword.kwlist + keyword.softkwlist, compact=True)' " -syn keyword pythonStatement False None True +syn keyword pythonBoolean False True +syn keyword pythonConstant None syn keyword pythonStatement as assert break continue del global syn keyword pythonStatement lambda nonlocal pass return with yield syn keyword pythonStatement class nextgroup=pythonClass skipwhite @@ -296,8 +300,8 @@ endif if !exists("python_no_builtin_highlight") " built-in constants " 'False', 'True', and 'None' are also reserved words in Python 3 - syn keyword pythonBuiltin False True None - syn keyword pythonBuiltin NotImplemented Ellipsis __debug__ + syn keyword pythonBoolean False True + syn keyword pythonConstant None NotImplemented Ellipsis __debug__ " constants added by the `site` module syn keyword pythonBuiltin quit exit copyright credits license " built-in functions @@ -391,6 +395,8 @@ endif syn sync match pythonSync grouphere NONE "^\%(def\|class\|async\s\+def\)\s\+\h\w*\s*[(:]" " The default highlight links. Can be overridden later. +hi def link pythonBoolean Statement +hi def link pythonConstant Statement hi def link pythonStatement Statement hi def link pythonConditional Conditional hi def link pythonRepeat Repeat @@ -421,6 +427,8 @@ if !exists("python_no_number_highlight") hi def link pythonNumber Number endif if !exists("python_no_builtin_highlight") + hi! def link pythonBoolean Function + hi! def link pythonConstant Function hi def link pythonBuiltin Function hi def link pythonEllipsis pythonBuiltin endif @@ -434,6 +442,10 @@ if !exists("python_no_doctest_highlight") hi def link pythonDoctest Special hi def link pythonDoctestValue Define endif +if exists("python_constant_highlight") + hi! def link pythonBoolean Boolean + hi! def link pythonConstant Constant +endif let b:current_syntax = "python" diff --git a/runtime/syntax/testdir/dumps/python_constants_builtin_default_00.dump b/runtime/syntax/testdir/dumps/python_constants_builtin_default_00.dump new file mode 100644 index 0000000000..bbee183dfc --- /dev/null +++ b/runtime/syntax/testdir/dumps/python_constants_builtin_default_00.dump @@ -0,0 +1,20 @@ +>#+0#0000e05#ffffff0| |B|u|i|l|t|-|i|n| |C|o|n|s|t|a|n|t|s| +0#0000000&@54 +|#+0#0000e05&| |h|t@1|p|s|:|/@1|d|o|c|s|.|p|y|t|h|o|n|.|o|r|g|/|3|/|l|i|b|r|a|r|y|/|c|o|n|s|t|a|n|t|s|.|h|t|m|l| +0#0000000&@24 +@75 +|#+0#0000e05&| |V|I|M|_|T|E|S|T|_|S|E|T|U|P| |u|n|l|e|t| |g|:|p|y|t|h|o|n|_|c|o|n|s|t|a|n|t|_|h|i|g|h|l|i|g|h|t| +0#0000000&@24 +|#+0#0000e05&| |V|I|M|_|T|E|S|T|_|S|E|T|U|P| |u|n|l|e|t| |g|:|p|y|t|h|o|n|_|n|o|_|b|u|i|l|t|i|n|_|h|i|g|h|l|i|g|h|t| +0#0000000&@22 +|t|e|s|t| |=| |F+0#00e0e07&|a|l|s|e| +0#0000000&@62 +|t|e|s|t| |=| |T+0#00e0e07&|r|u|e| +0#0000000&@63 +|t|e|s|t| |=| |N+0#00e0e07&|o|n|e| +0#0000000&@63 +|t|e|s|t| |=| |N+0#00e0e07&|o|t|I|m|p|l|e|m|e|n|t|e|d| +0#0000000&@53 +|t|e|s|t| |=| |E+0#00e0e07&|l@1|i|p|s|i|s| +0#0000000&@59 +|t|e|s|t| |=| |_+0#00e0e07&@1|d|e|b|u|g|_@1| +0#0000000&@58 +|~+0#4040ff13&| @73 +|~| @73 +|~| @73 +|~| @73 +|~| @73 +|~| @73 +|~| @73 +|~| @73 +| +0#0000000&@56|1|,|1| @10|A|l@1| diff --git a/runtime/syntax/testdir/dumps/python_constants_builtin_highlight_00.dump b/runtime/syntax/testdir/dumps/python_constants_builtin_highlight_00.dump new file mode 100644 index 0000000000..206f673afb --- /dev/null +++ b/runtime/syntax/testdir/dumps/python_constants_builtin_highlight_00.dump @@ -0,0 +1,20 @@ +>#+0#0000e05#ffffff0| |B|u|i|l|t|-|i|n| |C|o|n|s|t|a|n|t|s| +0#0000000&@54 +|#+0#0000e05&| |h|t@1|p|s|:|/@1|d|o|c|s|.|p|y|t|h|o|n|.|o|r|g|/|3|/|l|i|b|r|a|r|y|/|c|o|n|s|t|a|n|t|s|.|h|t|m|l| +0#0000000&@24 +@75 +|#+0#0000e05&| |V|I|M|_|T|E|S|T|_|S|E|T|U|P| |l|e|t| |g|:|p|y|t|h|o|n|_|c|o|n|s|t|a|n|t|_|h|i|g|h|l|i|g|h|t| |=| |1| +0#0000000&@22 +|#+0#0000e05&| |V|I|M|_|T|E|S|T|_|S|E|T|U|P| |u|n|l|e|t| |g|:|p|y|t|h|o|n|_|n|o|_|b|u|i|l|t|i|n|_|h|i|g|h|l|i|g|h|t| +0#0000000&@22 +|t|e|s|t| |=| |F+0#e000002&|a|l|s|e| +0#0000000&@62 +|t|e|s|t| |=| |T+0#e000002&|r|u|e| +0#0000000&@63 +|t|e|s|t| |=| |N+0#e000002&|o|n|e| +0#0000000&@63 +|t|e|s|t| |=| |N+0#e000002&|o|t|I|m|p|l|e|m|e|n|t|e|d| +0#0000000&@53 +|t|e|s|t| |=| |E+0#e000002&|l@1|i|p|s|i|s| +0#0000000&@59 +|t|e|s|t| |=| |_+0#e000002&@1|d|e|b|u|g|_@1| +0#0000000&@58 +|~+0#4040ff13&| @73 +|~| @73 +|~| @73 +|~| @73 +|~| @73 +|~| @73 +|~| @73 +|~| @73 +| +0#0000000&@56|1|,|1| @10|A|l@1| diff --git a/runtime/syntax/testdir/dumps/python_constants_keyword_default_00.dump b/runtime/syntax/testdir/dumps/python_constants_keyword_default_00.dump new file mode 100644 index 0000000000..941e69e97b --- /dev/null +++ b/runtime/syntax/testdir/dumps/python_constants_keyword_default_00.dump @@ -0,0 +1,20 @@ +>#+0#0000e05#ffffff0| |B|u|i|l|t|-|i|n| |C|o|n|s|t|a|n|t|s| +0#0000000&@54 +|#+0#0000e05&| |h|t@1|p|s|:|/@1|d|o|c|s|.|p|y|t|h|o|n|.|o|r|g|/|3|/|l|i|b|r|a|r|y|/|c|o|n|s|t|a|n|t|s|.|h|t|m|l| +0#0000000&@24 +@75 +|#+0#0000e05&| |V|I|M|_|T|E|S|T|_|S|E|T|U|P| |u|n|l|e|t| |g|:|p|y|t|h|o|n|_|c|o|n|s|t|a|n|t|_|h|i|g|h|l|i|g|h|t| +0#0000000&@24 +|#+0#0000e05&| |V|I|M|_|T|E|S|T|_|S|E|T|U|P| |l|e|t| |g|:|p|y|t|h|o|n|_|n|o|_|b|u|i|l|t|i|n|_|h|i|g|h|l|i|g|h|t| |=| |1| +0#0000000&@20 +|t|e|s|t| |=| |F+0#af5f00255&|a|l|s|e| +0#0000000&@62 +|t|e|s|t| |=| |T+0#af5f00255&|r|u|e| +0#0000000&@63 +|t|e|s|t| |=| |N+0#af5f00255&|o|n|e| +0#0000000&@63 +|t|e|s|t| |=| |N|o|t|I|m|p|l|e|m|e|n|t|e|d| @53 +|t|e|s|t| |=| |E|l@1|i|p|s|i|s| @59 +|t|e|s|t| |=| |_@1|d|e|b|u|g|_@1| @58 +|~+0#4040ff13&| @73 +|~| @73 +|~| @73 +|~| @73 +|~| @73 +|~| @73 +|~| @73 +|~| @73 +| +0#0000000&@56|1|,|1| @10|A|l@1| diff --git a/runtime/syntax/testdir/dumps/python_constants_keyword_highlight_00.dump b/runtime/syntax/testdir/dumps/python_constants_keyword_highlight_00.dump new file mode 100644 index 0000000000..b73c6ae028 --- /dev/null +++ b/runtime/syntax/testdir/dumps/python_constants_keyword_highlight_00.dump @@ -0,0 +1,20 @@ +>#+0#0000e05#ffffff0| |B|u|i|l|t|-|i|n| |C|o|n|s|t|a|n|t|s| +0#0000000&@54 +|#+0#0000e05&| |h|t@1|p|s|:|/@1|d|o|c|s|.|p|y|t|h|o|n|.|o|r|g|/|3|/|l|i|b|r|a|r|y|/|c|o|n|s|t|a|n|t|s|.|h|t|m|l| +0#0000000&@24 +@75 +|#+0#0000e05&| |V|I|M|_|T|E|S|T|_|S|E|T|U|P| |l|e|t| |g|:|p|y|t|h|o|n|_|c|o|n|s|t|a|n|t|_|h|i|g|h|l|i|g|h|t| |=| |1| +0#0000000&@22 +|#+0#0000e05&| |V|I|M|_|T|E|S|T|_|S|E|T|U|P| |l|e|t| |g|:|p|y|t|h|o|n|_|n|o|_|b|u|i|l|t|i|n|_|h|i|g|h|l|i|g|h|t| |=| |1| +0#0000000&@20 +|t|e|s|t| |=| |F+0#e000002&|a|l|s|e| +0#0000000&@62 +|t|e|s|t| |=| |T+0#e000002&|r|u|e| +0#0000000&@63 +|t|e|s|t| |=| |N+0#e000002&|o|n|e| +0#0000000&@63 +|t|e|s|t| |=| |N|o|t|I|m|p|l|e|m|e|n|t|e|d| @53 +|t|e|s|t| |=| |E|l@1|i|p|s|i|s| @59 +|t|e|s|t| |=| |_@1|d|e|b|u|g|_@1| @58 +|~+0#4040ff13&| @73 +|~| @73 +|~| @73 +|~| @73 +|~| @73 +|~| @73 +|~| @73 +|~| @73 +| +0#0000000&@56|1|,|1| @10|A|l@1| diff --git a/runtime/syntax/testdir/input/python_constants_builtin_default.py b/runtime/syntax/testdir/input/python_constants_builtin_default.py new file mode 100644 index 0000000000..d55e656328 --- /dev/null +++ b/runtime/syntax/testdir/input/python_constants_builtin_default.py @@ -0,0 +1,11 @@ +# Built-in Constants +# https://docs.python.org/3/library/constants.html + +# VIM_TEST_SETUP unlet g:python_constant_highlight +# VIM_TEST_SETUP unlet g:python_no_builtin_highlight +test = False +test = True +test = None +test = NotImplemented +test = Ellipsis +test = __debug__ diff --git a/runtime/syntax/testdir/input/python_constants_builtin_highlight.py b/runtime/syntax/testdir/input/python_constants_builtin_highlight.py new file mode 100644 index 0000000000..e092b186e4 --- /dev/null +++ b/runtime/syntax/testdir/input/python_constants_builtin_highlight.py @@ -0,0 +1,11 @@ +# Built-in Constants +# https://docs.python.org/3/library/constants.html + +# VIM_TEST_SETUP let g:python_constant_highlight = 1 +# VIM_TEST_SETUP unlet g:python_no_builtin_highlight +test = False +test = True +test = None +test = NotImplemented +test = Ellipsis +test = __debug__ diff --git a/runtime/syntax/testdir/input/python_constants_keyword_default.py b/runtime/syntax/testdir/input/python_constants_keyword_default.py new file mode 100644 index 0000000000..7b3d9c9c63 --- /dev/null +++ b/runtime/syntax/testdir/input/python_constants_keyword_default.py @@ -0,0 +1,11 @@ +# Built-in Constants +# https://docs.python.org/3/library/constants.html + +# VIM_TEST_SETUP unlet g:python_constant_highlight +# VIM_TEST_SETUP let g:python_no_builtin_highlight = 1 +test = False +test = True +test = None +test = NotImplemented +test = Ellipsis +test = __debug__ diff --git a/runtime/syntax/testdir/input/python_constants_keyword_highlight.py b/runtime/syntax/testdir/input/python_constants_keyword_highlight.py new file mode 100644 index 0000000000..60f3fb227d --- /dev/null +++ b/runtime/syntax/testdir/input/python_constants_keyword_highlight.py @@ -0,0 +1,11 @@ +# Built-in Constants +# https://docs.python.org/3/library/constants.html + +# VIM_TEST_SETUP let g:python_constant_highlight = 1 +# VIM_TEST_SETUP let g:python_no_builtin_highlight = 1 +test = False +test = True +test = None +test = NotImplemented +test = Ellipsis +test = __debug__