]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
runtime(python): Highlight built-in constants in Python
authorRob B <github@0x7e.net>
Mon, 26 Jan 2026 18:52:04 +0000 (18:52 +0000)
committerChristian Brabandt <cb@256bit.org>
Mon, 26 Jan 2026 18:52:04 +0000 (18:52 +0000)
Also add syntax tests for those newly constants.

closes: #17788
closes: #18922

Signed-off-by: Rob B <github@0x7e.net>
Signed-off-by: Christian Brabandt <cb@256bit.org>
runtime/doc/syntax.txt
runtime/syntax/python.vim
runtime/syntax/testdir/dumps/python_constants_builtin_default_00.dump [new file with mode: 0644]
runtime/syntax/testdir/dumps/python_constants_builtin_highlight_00.dump [new file with mode: 0644]
runtime/syntax/testdir/dumps/python_constants_keyword_default_00.dump [new file with mode: 0644]
runtime/syntax/testdir/dumps/python_constants_keyword_highlight_00.dump [new file with mode: 0644]
runtime/syntax/testdir/input/python_constants_builtin_default.py [new file with mode: 0644]
runtime/syntax/testdir/input/python_constants_builtin_highlight.py [new file with mode: 0644]
runtime/syntax/testdir/input/python_constants_keyword_default.py [new file with mode: 0644]
runtime/syntax/testdir/input/python_constants_keyword_highlight.py [new file with mode: 0644]

index 501dd9457877389a69e27c5bb21038d6cdc5653b..cc41a774a28e122be14b4655289c4b76566bc707 100644 (file)
@@ -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
index 301fb84802df2a22c23af695b717ba6e3e1f42f9..456a9825a23544985c94e8ff27517e0baf71a5f7 100644 (file)
@@ -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 <nas@python.ca>
 "              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 (file)
index 0000000..bbee183
--- /dev/null
@@ -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 (file)
index 0000000..206f673
--- /dev/null
@@ -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 (file)
index 0000000..941e69e
--- /dev/null
@@ -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 (file)
index 0000000..b73c6ae
--- /dev/null
@@ -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 (file)
index 0000000..d55e656
--- /dev/null
@@ -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 (file)
index 0000000..e092b18
--- /dev/null
@@ -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 (file)
index 0000000..7b3d9c9
--- /dev/null
@@ -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 (file)
index 0000000..60f3fb2
--- /dev/null
@@ -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__