nvim-treesitter / nvim-treesitter/nvim-treesitter-textobjects

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

Open
#638 2 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Tree-sitter Query
Stars
2.8k
Forks
271
Avg merge
8d 8h
Merged PRs (30d)
1

Description

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

-

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the nvim-treesitter.textobjects.swap entry points, swap_next and swap_previous, and inspect how the shown @function.outer queries are selected. Reproduce the Python decorated-function case, then verify that swapping from inside the function prioritizes the decorated_definition match and works with adjacent functions.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua, python
Domain
devtools, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.