aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSalomon Popp <hi@salomonpopp.me>2025-03-11 08:48:33 +0100
committerGitHub <noreply@github.com>2025-03-11 08:48:33 +0100
commitcf0eabc16cf32d69f7612d0e023ef210d84cdde6 (patch)
tree56676a0665c38c737100d80d4bcfff7b3f283c32
parentfeat(bibtex): highlight URLs and DOIs (#7725) (diff)
downloadnvim-treesitter-cf0eabc16cf32d69f7612d0e023ef210d84cdde6.tar
nvim-treesitter-cf0eabc16cf32d69f7612d0e023ef210d84cdde6.tar.gz
nvim-treesitter-cf0eabc16cf32d69f7612d0e023ef210d84cdde6.tar.bz2
nvim-treesitter-cf0eabc16cf32d69f7612d0e023ef210d84cdde6.tar.lz
nvim-treesitter-cf0eabc16cf32d69f7612d0e023ef210d84cdde6.tar.xz
nvim-treesitter-cf0eabc16cf32d69f7612d0e023ef210d84cdde6.tar.zst
nvim-treesitter-cf0eabc16cf32d69f7612d0e023ef210d84cdde6.zip
fix(python): don't highlight attribute name as builtin (#7712)
* fix(python): don't highlight attribute name as builtin * fix(python): highlight `@variable.member` correctly for member access context * test: add test * test: expand test * docs: update comment * refactor: change order, remove priority override * style: remove extra newline
-rw-r--r--queries/python/highlights.scm49
-rw-r--r--tests/query/highlights/python/fields.py5
2 files changed, 30 insertions, 24 deletions
diff --git a/queries/python/highlights.scm b/queries/python/highlights.scm
index 77edc7960..d0759e26b 100644
--- a/queries/python/highlights.scm
+++ b/queries/python/highlights.scm
@@ -23,10 +23,6 @@
"_" @character.special ; match wildcard
-((attribute
- attribute: (identifier) @variable.member)
- (#lua-match? @variable.member "^[%l_].*$"))
-
((assignment
left: (identifier) @type.definition
(type
@@ -395,6 +391,31 @@
(ellipsis)
] @punctuation.delimiter
+((identifier) @type.builtin
+ (#any-of? @type.builtin
+ ; https://docs.python.org/3/library/exceptions.html
+ "BaseException" "Exception" "ArithmeticError" "BufferError" "LookupError" "AssertionError"
+ "AttributeError" "EOFError" "FloatingPointError" "GeneratorExit" "ImportError"
+ "ModuleNotFoundError" "IndexError" "KeyError" "KeyboardInterrupt" "MemoryError" "NameError"
+ "NotImplementedError" "OSError" "OverflowError" "RecursionError" "ReferenceError" "RuntimeError"
+ "StopIteration" "StopAsyncIteration" "SyntaxError" "IndentationError" "TabError" "SystemError"
+ "SystemExit" "TypeError" "UnboundLocalError" "UnicodeError" "UnicodeEncodeError"
+ "UnicodeDecodeError" "UnicodeTranslateError" "ValueError" "ZeroDivisionError" "EnvironmentError"
+ "IOError" "WindowsError" "BlockingIOError" "ChildProcessError" "ConnectionError"
+ "BrokenPipeError" "ConnectionAbortedError" "ConnectionRefusedError" "ConnectionResetError"
+ "FileExistsError" "FileNotFoundError" "InterruptedError" "IsADirectoryError"
+ "NotADirectoryError" "PermissionError" "ProcessLookupError" "TimeoutError" "Warning"
+ "UserWarning" "DeprecationWarning" "PendingDeprecationWarning" "SyntaxWarning" "RuntimeWarning"
+ "FutureWarning" "ImportWarning" "UnicodeWarning" "BytesWarning" "ResourceWarning"
+ ; https://docs.python.org/3/library/stdtypes.html
+ "bool" "int" "float" "complex" "list" "tuple" "range" "str" "bytes" "bytearray" "memoryview"
+ "set" "frozenset" "dict" "type" "object"))
+
+; After @type.builtin bacause builtins (such as `type`) are valid as attribute name
+((attribute
+ attribute: (identifier) @variable.member)
+ (#lua-match? @variable.member "^[%l_].*$"))
+
; Class definitions
(class_definition
name: (identifier) @type)
@@ -429,26 +450,6 @@
name: (identifier) @constructor)))
(#any-of? @constructor "__new__" "__init__"))
-((identifier) @type.builtin
- (#any-of? @type.builtin
- ; https://docs.python.org/3/library/exceptions.html
- "BaseException" "Exception" "ArithmeticError" "BufferError" "LookupError" "AssertionError"
- "AttributeError" "EOFError" "FloatingPointError" "GeneratorExit" "ImportError"
- "ModuleNotFoundError" "IndexError" "KeyError" "KeyboardInterrupt" "MemoryError" "NameError"
- "NotImplementedError" "OSError" "OverflowError" "RecursionError" "ReferenceError" "RuntimeError"
- "StopIteration" "StopAsyncIteration" "SyntaxError" "IndentationError" "TabError" "SystemError"
- "SystemExit" "TypeError" "UnboundLocalError" "UnicodeError" "UnicodeEncodeError"
- "UnicodeDecodeError" "UnicodeTranslateError" "ValueError" "ZeroDivisionError" "EnvironmentError"
- "IOError" "WindowsError" "BlockingIOError" "ChildProcessError" "ConnectionError"
- "BrokenPipeError" "ConnectionAbortedError" "ConnectionRefusedError" "ConnectionResetError"
- "FileExistsError" "FileNotFoundError" "InterruptedError" "IsADirectoryError"
- "NotADirectoryError" "PermissionError" "ProcessLookupError" "TimeoutError" "Warning"
- "UserWarning" "DeprecationWarning" "PendingDeprecationWarning" "SyntaxWarning" "RuntimeWarning"
- "FutureWarning" "ImportWarning" "UnicodeWarning" "BytesWarning" "ResourceWarning"
- ; https://docs.python.org/3/library/stdtypes.html
- "bool" "int" "float" "complex" "list" "tuple" "range" "str" "bytes" "bytearray" "memoryview"
- "set" "frozenset" "dict" "type" "object"))
-
; Regex from the `re` module
(call
function: (attribute
diff --git a/tests/query/highlights/python/fields.py b/tests/query/highlights/python/fields.py
index deb280622..cea9eccb7 100644
--- a/tests/query/highlights/python/fields.py
+++ b/tests/query/highlights/python/fields.py
@@ -1,9 +1,14 @@
class Fields:
+ type: str
+# ^^^^ @variable.member
+
def __init__(self, fields: list[int]) -> None:
# ^^^ @type.builtin
# ^^^^ @constant.builtin
self.fields = fields
# ^^^^^^ @variable.member
+ self.type = "foo"
+# ^^^^ @variable.member
self.__dunderfield__ = None
# ^^^^^^^^^^^^^^^ @variable.member
self._FunKyFielD = 0