nvim-treesitter-textobjects icon indicating copy to clipboard operation
nvim-treesitter-textobjects copied to clipboard

python class decorators are mistaken as startpoint for undecorated classes defined after it

Open midgethetree opened this issue 6 months ago • 4 comments

Describe the bug When multiple classes are defined in a single python file, and one or more classes are decorated with dataclasses.dataclass, this plugin mistakes that line as the start point for non-dataclasses defined after it and before any other dataclass.

To Reproduce Steps to reproduce the behavior:

  1. Copy the following example code into a new python file:
import dataclasses


@dataclasses.dataclass
class SomeDataClass:
    name: str


class SomeClass:
    def __init__(self):
        pass


class SomeOtherClass:
    def __init__(self):
        pass

2a. Place cursor somewhere inside SomeClass 2b. Callrequire("nvim-treesitter-textobjects.select").select_textobject("@class.inner", "textobjects") directly or via keybind 2c. Observe that the contents of SomeDataClass are selected instead of SomeClass

3a. Place cursor somewhere inside SomeClass 3b. Callrequire("nvim-treesitter-textobjects.select").select_textobject("@class.outer", "textobjects") directly or via keybind 3c. Observe that both SomeClass and SomeDataClass are selected instead of just SomeClass

4a. Place cursor somewhere inside SomeOtherClass 4b. Callrequire("nvim-treesitter-textobjects.select").select_textobject("@class.inner", "textobjects") directly or via keybind 4c. Observe that the contents of SomeDataClass are selected instead of SomeOtherClass

5a. Place cursor somewhere inside SomeOtherClass 5b. Callrequire("nvim-treesitter-textobjects.select").select_textobject("@class.outer", "textobjects") directly or via keybind 5c. Observe that all three classes are selected instead of just SomeOtherClass

Expected behavior When selecting @class.inner or @class.outer, the class the cursor is inside of should be selected, not the previous dataclass and not multiple classes.

Output of :checkhealth nvim-treesitter

Requirements ~

  • ✅ OK Neovim was compiled with tree-sitter runtime ABI version 15 (required >=13).
  • ✅ OK tree-sitter 0.25.7 (/usr/bin/tree-sitter)
  • ✅ OK node 24.4.0 (/usr/bin/node)
  • ✅ OK tar 1.35.0 (/usr/bin/tar)
  • ✅ OK curl 8.14.1 (/usr/bin/curl) curl 8.14.1 (x86_64-pc-linux-gnu) libcurl/8.14.1 OpenSSL/3.5.1 zlib/1.3.1 brotli/1.1.0 zstd/1.5.7 libidn2/2.3.7 libpsl/0.21.5 libssh2/1.11.1 nghttp2/1.66.0 nghttp3/1.10.1 Release-Date: 2025-06-04 Protocols: dict file ftp ftps gopher gophers http https imap imaps ipfs ipns mqtt pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp ws wss Features: alt-svc AsynchDNS brotli GSS-API HSTS HTTP2 HTTP3 HTTPS-proxy IDN IPv6 Kerberos Largefile libz NTLM PSL SPNEGO SSL threadsafe TLS-SRP UnixSockets zstd

OS Info ~

  • version: #1 ZEN SMP PREEMPT_DYNAMIC Thu, 10 Jul 2025 17:10:03 +0000
  • sysname: Linux
  • release: 6.15.6-zen1-1-zen
  • machine: x86_64

Install directory for parsers and queries ~

  • ~/.local/share/nvim/site/
  • ✅ OK is writable.
  • ✅ OK is in runtimepath.

Installed languages H L F I J ~

  • bash ✓ ✓ ✓ . ✓

  • c_sharp ✓ ✓ ✓ . ✓

  • css ✓ . ✓ ✓ ✓

  • ecma

  • gdscript ✓ ✓ ✓ ✓ ✓

  • gdshader ✓ . . . ✓

  • git_config ✓ . ✓ . ✓

  • git_rebase ✓ . . . ✓

  • gitattributes ✓ ✓ . . ✓

  • gitcommit ✓ . . . ✓

  • gitignore ✓ . . . ✓

  • godot_resource ✓ ✓ ✓ . ✓

  • html ✓ ✓ ✓ ✓ ✓

  • html_tags

  • ini ✓ . ✓ . ✓

  • javascript ✓ ✓ ✓ ✓ ✓

  • json ✓ ✓ ✓ ✓ .

  • jsonc ✓ ✓ ✓ ✓ ✓

  • jsx

  • python ✓ ✓ ✓ ✓ ✓

  • todotxt ✓ . . . .

  • toml ✓ ✓ ✓ ✓ ✓

  • yaml ✓ ✓ ✓ ✓ ✓

    Legend: H[ighlight], L[ocals], F[olds], I[ndents], In[J]ections ~

Output of nvim --version

NVIM v0.11.3
Build type: RelWithDebInfo
LuaJIT 2.1.1748459687
Run "nvim -V1 -v" for more info

Additional context Add any other context about the problem here.

midgethetree avatar Jul 16 '25 23:07 midgethetree

It seems to be any kind of class decorator:

from typing import final

@final
class A:
    x: int

class B:
    y: int

Here it selects both A and B.

tmke8 avatar Aug 01 '25 16:08 tmke8

I've tried out the updated query found in #782 and that does resolve the issue, but apparently it hasn't been merged yet because it targets the main branch instead of master even though this is a bug with the main branch.

midgethetree avatar Aug 01 '25 17:08 midgethetree

@midgethetree The bug in the main branch should be fixed by fixing the lua code, not the queries. Unfortunately the development of the main branch is quite slow at the moment..

kiyoon avatar Aug 04 '25 06:08 kiyoon

Would this be the cause of an issue I've got where ]c and [c will only jump between decorated functions?

rob-levy-minimum avatar Sep 24 '25 13:09 rob-levy-minimum