From cf0eabc16cf32d69f7612d0e023ef210d84cdde6 Mon Sep 17 00:00:00 2001 From: Salomon Popp Date: Tue, 11 Mar 2025 08:48:33 +0100 Subject: 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 --- queries/python/highlights.scm | 49 +++++++++++++++++---------------- tests/query/highlights/python/fields.py | 5 ++++ 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 -- cgit v1.2.3-70-g09d2