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

TextYankPost receives visual = true for treesitter objects

Open
#240 2 comments 1 reaction 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
When executing regular yanking operations such as yaf, yaa, yif, yia etc. TextYankPost event payload receives an incorrect visual=true as opposed to visual = false, which is what is received for regular built in text objects (yiw, yap etc). Therefore when yanking the function body, the highlight is not observed, as compared to yanking the word, which does correctly highlight it. Obviously happening since the auto command configures visual = false, but the even data receives visual = true. The following observations were made - The event received by vim.highlight is as if it was done in visual mode. Excerpt from vim.highlight which demonstrates why yaf highlight does not work with the above auto command.

opts = opts or {}
  local event = opts.event or vim.v.event
  local on_macro = opts.on_macro or false
  **local on_visual = (opts.on_visual ~= false)**

  if not on_macro and vim.fn.reg_executing() ~= '' then
    return
  end
  if event.operator ~= 'y' or event.regtype == '' then
    return
  end
  **if not on_visual and event.visual then
    return
  end**

To Reproduce
Having installed and configured treesitter and treesitter text objects plugins only

local status_ok, configs = pcall(require, "nvim-treesitter.configs")
if not status_ok then
    vim.notify("Unable to load of find plugin nvim-treesitter", vim.log.levels.ERROR)
    return
end

vim.cmd [[
      augroup GENERAL
        autocmd!
        autocmd TextYankPost * lua require('vim.highlight').on_yank({timeout = 250, on_visual = false})
      augroup END
]]

configs.setup({
    textobjects = {
        select = {
            enable = true,
            keymaps = {
                ["aa"] = "@parameter.outer",
                ["ia"] = "@parameter.inner",
                ["af"] = "@function.outer",
                ["if"] = "@function.inner",
                ["ac"] = "@class.outer",
                ["ic"] = "@class.inner",
            },
        },
    }
})

Having the following (dummy) piece of code

int main(int argc, char **argv) {
  int k = 12;
  k++;
  printf("Hello, World!\n");
  printf("Hello, kval! %d\n", k);
  printf("Hello, argc! %d\n", argc);
  for (size_t i = 0; i < argc; i++) {
    printf("%s\n", argv[i]);
  }
  fflush(stdout);
  return 0;
}

Executing yaw, this is what vim.highlight reports for vim.v.event

{
  inclusive = true,
  operator = "y",
  regcontents = { " main" },
  regname = "",
  regtype = "v",
  visual = false
}

Executing yaf, this is what vim.highlight reports for vim.v.event

{
  inclusive = true,
  operator = "y",
  regcontents = { "int main(int argc, char **argv) {", "  int k = 12;", "  k++;", '  printf("Hello, World!\\n");', '  pr
intf("Hello, kval! %d\\n", k);', '  printf("Hello, argc! %d\\n", argc);', "  for (size_t i = 0; i < argc; i++) {", '
printf("%s\\n", argv[i]);', "  }", "  fflush(stdout);", "  return 0;", "}" },
  regname = "",
  regtype = "v",
  visual = true
}

Expected behavior
When yanking a custom object, the event will not mark the action/operation as if it was being done in visual mode

Output of :checkhealth nvim_treesitter

========================================================================

  • INFO: Runtime ABI version : 14
  • OK: Loaded parser for bash: ABI version 13
  • OK: Loaded parser for c: ABI version 13
  • OK: Loaded parser for cmake: ABI version 13
  • OK: Loaded parser for commonlisp: ABI version 13
  • OK: Loaded parser for dockerfile: ABI version 13
  • OK: Loaded parser for json: ABI version 13
  • OK: Loaded parser for latex: ABI version 13
  • OK: Loaded parser for lua: ABI version 13
  • OK: Loaded parser for markdown: ABI version 13
  • OK: Loaded parser for markdown_inline: ABI version 13
  • OK: Loaded parser for toml: ABI version 13
  • OK: Loaded parser for vim: ABI version 14
  • OK: Loaded parser for yaml: ABI version 13

Output of nvim --version

NVIM v0.7.2
Build type: Release
LuaJIT 2.1.0-beta3
Compiled by runner@fv-az164-457

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

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/share/nvim"

Bug was initially reported in neovim repo, but was redirected to here instead - https://github.com/neovim/neovim/issues/19687

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 issue with the nvim-treesitter textobjects select configuration and the vim.highlight.on_yank TextYankPost autocmd shown in the report. Compare vim.v.event for built-in objects such as yiw and treesitter objects such as yaf; done means custom-object yanks report visual=false and receive the configured yank highlight.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.