nvim-treesitter / nvim-treesitter-textobjects

Apache License 2.0
2.16k stars 194 forks source link

Swap @function.outer doesn't work from within decorated functions in Python #638

Open Hubro opened 4 months ago

Hubro commented 4 months ago

Describe the bug

Swapping @function.outer in Python doesn't work when inside a decorated function. Take the example:

def test():
    print("This is a test")

@some_decorator
def test2():
    print("This is a test")

def test3():
    print("This is a test")

When in either of the un-decorated functions, swapping with the decorated function works just fine. When in the decorated function, nothing happens when you try to swap, unless the cursor is on the decorator line.

Given the queries:

(function_definition
  body: (block)? @function.inner) @function.outer

(decorated_definition
  (function_definition)) @function.outer

My guess is that this happens because when inside the decorated function, the non-decorated @function.outer match is the closest match and is selected for swapping, but since it's inside a (decorated_definition ..., it doesn't have any siblings to swap with:

image

So nothing happens.

To Reproduce Steps to reproduce the behavior:

(See above)

I'm using these functions:

local ts_swap = require("nvim-treesitter.textobjects.swap")

ts_swap.swap_next("@function.outer")
ts_swap.swap_previous("@function.outer")

Expected behavior

If the current cursor position matches both a function_definition and a decorated_definition, the latter should have higher priority.

Output of :checkhealth nvim-treesitter

nvim-treesitter: require("nvim-treesitter.health").check() Installation ~ - WARNING `tree-sitter` executable not found (parser generator, only needed for :TSInstallFromGrammar, not required for :TSInstall) - OK `node` found v20.11.0 (only needed for :TSInstallFromGrammar) - OK `git` executable found. - OK `cc` executable found. Selected from { vim.NIL, "cc", "gcc", "clang", "cl", "zig" } Version: gcc (GCC) 13.2.0 - OK Neovim was compiled with tree-sitter runtime ABI version 14 (required >=13). Parsers must be compatible with runtime ABI. OS Info: { machine = "x86_64", release = "6.9.3", sysname = "Linux", version = "#1-NixOS SMP PREEMPT_DYNAMIC Thu May 30 07:45:04 UTC 2024" } ~ Parser/Features H L F I J - bash ✓ ✓ ✓ . ✓ - c ✓ ✓ ✓ ✓ ✓ - css ✓ . ✓ ✓ ✓ - diff ✓ . . . . - dockerfile ✓ . . . ✓ - git_config ✓ . ✓ . ✓ - git_rebase ✓ . . . ✓ - gitcommit ✓ . . . ✓ - gitignore ✓ . . . ✓ - hyprlang ✓ . ✓ ✓ ✓ - ini ✓ . ✓ . ✓ - javascript ✓ ✓ ✓ ✓ ✓ - json ✓ ✓ ✓ ✓ . - just ✓ ✓ ✓ ✓ ✓ - lua ✓ ✓ ✓ ✓ ✓ - make ✓ . ✓ . ✓ - markdown ✓ . ✓ ✓ ✓ - markdown_inline ✓ . . . ✓ - nix ✓ ✓ ✓ . ✓ - python ✓ ✓ ✓ ✓ ✓ - query ✓ ✓ ✓ ✓ ✓ - requirements ✓ . . . ✓ - robot ✓ . ✓ ✓ ✓ - sql ✓ . . ✓ ✓ - ssh_config ✓ ✓ ✓ ✓ ✓ - tmux ✓ . . . ✓ - toml ✓ ✓ ✓ ✓ ✓ - tsx ✓ ✓ ✓ ✓ ✓ - typescript ✓ ✓ ✓ ✓ ✓ - vim ✓ ✓ ✓ . ✓ - vimdoc ✓ . . . ✓ - xml ✓ ✓ ✓ ✓ ✓ - yaml ✓ ✓ ✓ ✓ ✓ - yang ✓ . ✓ ✓ ✓ Legend: H[ighlight], L[ocals], F[olds], I[ndents], In[j]ections +) multiple parsers found, only one will be used x) errors found in the query, try to run :TSUpdate {lang} ~

Output of nvim --version

NVIM v0.10.0-dev-2295+g672556525
Build type: RelWithDebInfo
LuaJIT 2.1.1707061634
Run "nvim -V1 -v" for more info```

Additional context

-

clason commented 2 months ago

648 is merged; please check if the issue still occurs.

Hubro commented 2 months ago

@clason It seems to only have gotten worse. Now decorated functions can't be moved in any circumstances. Given this test file:

def test1():
    pass

@foo
def test2():
    pass

@foo
def test3():
    pass

def test4():
    pass

def test5():
    pass

And the commands ts_swap.swap_next("@function.outer") and ts_swap.swap_previous("@function.outer"), the decorated functions no longer register as functions at all. The normal functions can be moved up and down, but jump right past the decorated functions. The decorated functions can't be moved whether the cursor is placed in the decorator or in the function.