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

Inconsistent behavior between delete and select operations for the same text-object

Open
#506 0 comments 0 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
Deleting a scala function using function.outer text object in some cases deletes also the next function, while using the same text-object to select code always highlights correctly only a single function.

To Reproduce
Given the following code:

object Outer {

  object Inner {

    override def methodA(): String =
      "some text"

    override def methodB(): String = ""

  }
}

and the cursor on methodA selecting the function outer object using:
lua require("nvim-treesitter.textobjects.select").select_textobject('@function.outer', 'textobjects', 'x')

results in:
image

However if we try to call the same object but with a delete operation it results in both methods getting deleted:
image

Expected behavior
I would expect both operators - delete and select to work in a consistent way. Especially deleting using @fuction.outer should only delete a single function.

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)
- WARNING `node` executable not found (only needed for :TSInstallFromGrammar, not required for :TSInstall)
- OK `git` executable found.
- OK `gcc` executable found. Selected from { "gcc", "cc", "gcc", "clang", "cl", "zig" }
  Version: gcc (GCC) 12.3.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.1.52",
  sysname = "Linux",
  version = "#1-NixOS SMP PREEMPT_DYNAMIC Wed Sep  6 20:27:03 UTC 2023"
} ~

Parser/Features         H L F I J
  - bash                ✓ ✓ ✓ . ✓
  - c                   ✓ ✓ ✓ ✓ ✓
  - comment             ✓ . . . .
  - devicetree          ✓ ✓ ✓ ✓ ✓
  - dockerfile          ✓ . . . ✓
  - go                  ✓ ✓ ✓ ✓ ✓
  - graphql             ✓ . . ✓ ✓
  - hocon               ✓ . . . ✓
  - html                ✓ ✓ ✓ ✓ ✓
  - java                ✓ ✓ ✓ ✓ ✓
  - javascript          ✓ ✓ ✓ ✓ ✓
  - json                ✓ ✓ ✓ ✓ .
  - kotlin              ✓ ✓ ✓ . ✓
  - lua                 ✓ ✓ ✓ ✓ ✓
  - markdown            ✓ . ✓ ✓ ✓
  - nix                 ✓ ✓ ✓ . ✓
  - python              ✓ ✓ ✓ ✓ ✓
  - query               ✓ ✓ ✓ ✓ ✓
  - rust                ✓ ✓ ✓ ✓ ✓
  - scala               ✓ ✓ ✓ . ✓
  - scheme              ✓ . ✓ . ✓
  - smithy              ✓ . . . .
  - sql                 ✓ . . ✓ ✓
  - typescript          ✓ ✓ ✓ ✓ ✓
  - vim                 ✓ ✓ ✓ . ✓
  - vimdoc              ✓ . . . ✓
  - xml                 ✓ . ✓ ✓ ✓
  - yaml                ✓ ✓ ✓ ✓ ✓

  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.9.2
Build type: Release
LuaJIT 2.1.1693350652

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "
/nix/store/1b4pbg130cdq5pjyi7vxqqrf7nqh63a5-neovim-unwrapped-0.9.2/share/nvim
"

Additional context
Add any other context about the problem here.

while trying to minimize the reproduction example I found following interesting cases:
If we remove the inner object but leave the indentation intact, both functions will be deleted:
image
but if we format the code (fix the redundant indentations) the delete operation will work correctly.
image

After compacting the first function to a single line it also works correctly:
image

It also works if the function that is being deleted is defined using braces:
image

treesitter-scala rev: 70afdd5632d57dd63a960972ab25945e353a52f6

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

Reproduce the issue with the provided Scala snippet and the Lua entry point nvim-treesitter.textobjects.select. Compare the @function.outer behavior when invoked through selection and deletion, including the indentation and brace variations described. Done means deletion removes only the selected function, consistently with selection.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua, neovim, scala
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.