Skip to content

TextYankPost not setting v:event key regcontents (nor any other keys for TextYankPost) #2535

@aalvarado1919

Description

@aalvarado1919

Description

I have a separate plugin (neoclip), which listens to TextYankPost events.
When the handler is run in neoclip, it requires regcontents to be set. neoclip code

However, looking at the nvim-tree code
It doesn't seem to set those values that are documented for the TextYankPost event.

Neovim version

NVIM v0.9.4
Build type: Release
LuaJIT 2.1.1699801871

Operating system and version

macOS 13.6.2

Windows variant

No response

nvim-tree version

80cfead

Clean room replication

vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1

vim.cmd [[set runtimepath=$VIMRUNTIME]]
vim.cmd [[set packpath=/tmp/nvt-min/site]]
local package_root = "/tmp/nvt-min/site/pack"
local install_path = package_root .. "/packer/start/packer.nvim"
local function load_plugins()
  require("packer").startup {
    {
      "wbthomason/packer.nvim",
      "nvim-tree/nvim-tree.lua",
      "nvim-tree/nvim-web-devicons",
      -- ADD PLUGINS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
      "AckslD/nvim-neoclip.lua"

    },
    config = {
      package_root = package_root,
      compile_path = install_path .. "/plugin/packer_compiled.lua",
      display = { non_interactive = true },
    },
  }
end
if vim.fn.isdirectory(install_path) == 0 then
  print "Installing nvim-tree and dependencies."
  vim.fn.system { "git", "clone", "--depth=1", "https://github.com/wbthomason/packer.nvim", install_path }
end
load_plugins()
require("packer").sync()
vim.cmd [[autocmd User PackerComplete ++once echo "Ready!" | lua setup()]]
vim.opt.termguicolors = true
vim.opt.cursorline = true

-- MODIFY NVIM-TREE SETTINGS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
_G.setup = function()
  require("neoclip").setup({ default_register = { "\"", "+", "*" } })
  require("nvim-tree").setup {}
end

-- UNCOMMENT this block for diagnostics issues, substituting pattern and cmd as appropriate.
-- Requires diagnostics.enable = true in setup.
--[[
vim.api.nvim_create_autocmd("FileType", {
  pattern = "lua",
  callback = function()
    vim.lsp.start { cmd = { "lua-language-server" } }
  end,
})
]]

Steps to reproduce

  1. nvim -nu /tmp/nvt-min.lua
  2. :NvimTreeOpen
  3. Navigate to a file in the tree
  4. In normal mode, press 'y' to attempt to yank file
  5. Error with neoclip saying regcontents is empty

Expected behavior

Open nvim-tree, for a file in the tree, try to yank the filename with 'y'. There are no error messages that show up for handlers that read regcontents from the event.

Actual behavior

Open nvim-tree, for a file in the tree, try to yank the filename with 'y'.

Error detected while processing TextYankPost Autocommands for "*":
E5108: Error executing lua ...hare/nvim/lazy/nvim-neoclip.lua/lua/neoclip/handlers.lua:9: attempt to get length of field 'regcontents' (a nil value)
stack traceback:
        ...hare/nvim/lazy/nvim-neoclip.lua/lua/neoclip/handlers.lua:9: in function 'should_add'
        ...hare/nvim/lazy/nvim-neoclip.lua/lua/neoclip/handlers.lua:45: in function 'handle_yank_post'
        [string ":lua"]:1: in main chunk
        [C]: in function 'nvim_exec_autocmds'
        ...zy/nvim-tree.lua/lua/nvim-tree/actions/fs/copy-paste.lua:266: in function 'fn'
        ...ocal/share/nvim/lazy/nvim-tree.lua/lua/nvim-tree/api.lua:49: in function <...ocal/share/nvim/lazy/nvim-tree.lua/lua/nvim-tree/api.lua:46>

Activity

alex-courtis

alex-courtis commented on Nov 19, 2023

@alex-courtis
Member

Many thanks for the detailed investigation and report. It appears that we do need to populate all the things.

We can hardcode most of it, perhaps something like:

{
  inclusive = false,
  operator = "y",
  regcontents = { "$FOOBAR" },
  regname = "",  --unnamed
  regtype = "v", --we're not yanking the whole line
  visual = false
}

I'd be most grateful if you could submit a PR @aalvarado1919 - you have all the knowledge and a good test setup...

added
PR pleasenvim-tree team does not have the bandwidth to implement; a PR will be gratefully appreciated
on Nov 19, 2023
aalvarado1919

aalvarado1919 commented on Nov 28, 2023

@aalvarado1919
Author

Yes, I can take a look this week @alex-courtis.

Aljendro

Aljendro commented on Dec 1, 2023

@Aljendro

I don't actually know how I would set the v:event variable. Seems to be readonly and trying to pass any arguments to

vim.api.nvim_exec_autocmds("TextYankPost", {})

Did not seem to work.

I've asked a question on SO here. If I can get an answer, I will be able to create the PR

gegoune

gegoune commented on Dec 1, 2023

@gegoune
Collaborator

According to help:

TextYankPost			Just after a |yank| or |deleting| command, but not
				if the black hole register |quote_| is used nor
				for |setreg()|. Pattern must be "*".
				Sets these |v:event| keys:
				    inclusive
				    operator
				    regcontents
				    regname
				    regtype
				    visual
				The `inclusive` flag combined with the |'[|
				and |']| marks can be used to calculate the
				precise region of the operation.

				Non-recursive (event cannot trigger itself).
				Cannot change the text. |textlock|

event "sets it", not consumers.

Confirmed with:

vim.api.nvim_create_autocmd('TextYankPost', {
  callback = function()
    vim.print(vim.v.event)
  end,
})

and yanking in regular buffer, this will print:

{
  inclusive = true,
  operator = "y",
  regcontents = { "ction(event)" },
  regname = "",
  regtype = "v",
  visual = false
}

Yanking y in nvim-tree will print:

vim.empty_dict()

That explains reported error.

Issue explained by this line of help quoted above:

but not if the black hole register |quote_| is used nor for |setreg()|

and that's exactly what nvim-tree does:
https://github.com/nvim-tree/nvim-tree.lua/blob/master/lua/nvim-tree/actions/fs/copy-paste.lua#L256-L264

gegoune

gegoune commented on Dec 3, 2023

@gegoune
Collaborator

I wonder if vim.cmd.let(string:format("@+=%s", content)) would be sufficient to fix the issue, @aalvarado1919 would you mind giving it a quick test?

Aljendro

Aljendro commented on Dec 5, 2023

@Aljendro

@gegoune Had to set with

vim.cmd.let(string.format("@+=\"%s\"", content))

but that still does not fire the TextYankPost autocmd on its own

zyd2001

zyd2001 commented on Mar 30, 2024

@zyd2001
Contributor

I also encountered the same problem. I did some investigation and I believe currently there is no way to trigger TextYankPost with custom v:event without internal change of vim since both :doautocmd and nvim_exec_autocmds() just use empty v:event. I think it is reasonable to make :doautocmd accept custom v:event. I will see whether I can make an PR for this to vim.

I think currently our best solution we can have is to write the content somewhere and use vim command to do real copy then delete those text.

added a commit that references this issue on Mar 31, 2024
d8d3a15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    PR pleasenvim-tree team does not have the bandwidth to implement; a PR will be gratefully appreciatedbugSomething isn't workingreproducedIssue confirmed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @alex-courtis@Aljendro@zyd2001@gegoune@aalvarado1919

        Issue actions

          TextYankPost not setting v:event key regcontents (nor any other keys for TextYankPost) · Issue #2535 · nvim-tree/nvim-tree.lua