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

Heading or trailing comment in Python class/function not included in text object

Open
#25 6 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Describe the bug

If the first or last element of a Python class or function is a comment, then targeting it as a text object (with @class.outer, @function.inner, etc.) won't include the comment.

This isn't a real problem for me, just a curious edge case I noticed. If fixing this over-complicates other parts of the code or parser, feel free to disregard this and close, but on the off-chance there is a simple fix I'm missing, I thought I would be reporting it.

To Reproduce

With

treesitter.setup {
  textobjects = {
    select = {
      enable = true,
      keymaps = {
        ["ac"] = "@class.outer",
      },
    },
  },
}

and the Python snippet

class A:
    x = 1
    y = 2  # some comment

with the cursor anywhere on or before the character 2, then typing vac will select everything except the comment. If the cursor is on the comment, then typing vac won't do anything.

Expected behavior

With the cursor anywhere in the class, typing vac selects the whole class.

Output of :checkhealth nvim_treesitter

health#nvim_treesitter#check

Installation

  • OK: git executable found.
  • OK: cc executable found.

elm parser healthcheck

c parser healthcheck

  • OK: c parser found.
  • OK: highlights.scm found.
  • OK: locals.scm found.
  • OK: textobjects.scm found.
  • OK: folds.scm found.

java parser healthcheck

python parser healthcheck

  • OK: python parser found.
  • OK: highlights.scm found.
  • OK: locals.scm found.
  • OK: textobjects.scm found.
  • OK: folds.scm found.

dart parser healthcheck

lua parser healthcheck

  • OK: lua parser found.
  • OK: highlights.scm found.
  • OK: locals.scm found.
  • OK: textobjects.scm found.
  • OK: folds.scm found.

ocaml parser healthcheck

go parser healthcheck

nix parser healthcheck

yaml parser healthcheck

json parser healthcheck

jsdoc parser healthcheck

markdown parser healthcheck

julia parser healthcheck

html parser healthcheck

typescript parser healthcheck

  • OK: typescript parser found.
  • OK: highlights.scm found.
  • OK: locals.scm found.
  • OK: textobjects.scm found.
  • OK: folds.scm found.

fennel parser healthcheck

swift parser healthcheck

query parser healthcheck

cpp parser healthcheck

  • OK: cpp parser found.
  • OK: highlights.scm found.
  • OK: locals.scm found.
  • OK: textobjects.scm found.
  • OK: folds.scm found.

regex parser healthcheck

ruby parser healthcheck

vue parser healthcheck

ql parser healthcheck

scala parser healthcheck

toml parser healthcheck

rust parser healthcheck

bash parser healthcheck

ocaml_interface parser healthcheck

rst parser healthcheck

css parser healthcheck

c_sharp parser healthcheck

javascript parser healthcheck

  • OK: javascript parser found.
  • OK: highlights.scm found.
  • OK: locals.scm found.
  • OK: textobjects.scm found.
  • OK: folds.scm found.

php parser healthcheck

haskell parser healthcheck

tsx parser healthcheck

  • OK: tsx parser found.
  • OK: highlights.scm found.
  • OK: locals.scm found.
  • OK: textobjects.scm found.
  • OK: folds.scm found.

Output of nvim --version

NVIM v0.5.0-721-g3c5141d2c
Build type: RelWithDebInfo
LuaJIT 2.1.0-beta3
Compilation: /usr/bin/gcc-5 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1 -O2 -g -Og -g -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes -std=gnu99 -Wshadow -Wconversion -Wmissing-prototypes -Wvla -fstack-protector-strong -fno-common -fdiagnostics-color=auto -DINCLUDE_GENERATED_DECLARATIONS -D_GNU_SOURCE -DNVIM_MSGPACK_HAS_FLOAT32 -DNVIM_UNIBI_HAS_VAR_FROM -DMIN_LOG_LEVEL=3 -I/home/travis/build/neovim/bot-ci/build/neovim/build/config -I/home/travis/build/neovim/bot-ci/build/neovim/src -I/home/travis/build/neovim/bot-ci/build/neovim/.deps/usr/include -I/usr/include -I/home/travis/build/neovim/bot-ci/build/neovim/build/src/nvim/auto -I/home/travis/build/neovim/bot-ci/build/neovim/build/include
Compiled by travis@travis-job-5482189d-7ef7-4d79-892c-6d7ec9adbd1f

Features: +acl +iconv +tui
See ":help feature-compile"

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "
/home/travis/build/neovim/bot-ci/build/neovim/build/nvim.AppDir/usr/share/nvim
"

Run :checkhealth for more info

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 by reproducing the Python example with the provided textobjects configuration and the @class.outer selection. Inspect the Python textobject query and selection entry point to determine how leading or trailing comments are handled. Done means selecting the class includes the comment from any cursor position, with coverage for the reported case.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua, python
Domain
developer-experience, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.