Skip to content

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

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
aalvarado1919 opened this issue Nov 16, 2023 · 7 comments
Labels
bug Something isn't working PR please nvim-tree team does not have the bandwidth to implement; a PR will be gratefully appreciated reproduced Issue confirmed

Comments

@aalvarado1919
Copy link

aalvarado1919 commented Nov 16, 2023

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>
@aalvarado1919 aalvarado1919 added the bug Something isn't working label Nov 16, 2023
@alex-courtis
Copy link
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...

@alex-courtis alex-courtis added reproduced Issue confirmed PR please nvim-tree team does not have the bandwidth to implement; a PR will be gratefully appreciated labels Nov 19, 2023
@aalvarado1919
Copy link
Author

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

@Aljendro
Copy link

Aljendro commented Dec 1, 2023

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
Copy link
Collaborator

gegoune commented Dec 1, 2023

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
Copy link
Collaborator

gegoune commented Dec 3, 2023

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
Copy link

Aljendro commented Dec 5, 2023

@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
Copy link
Contributor

zyd2001 commented Mar 30, 2024

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.

alex-courtis added a commit that referenced this issue Mar 31, 2024
* fix TextYankPost event

* Update lua/nvim-tree/actions/fs/copy-paste.lua

Co-authored-by: Alexander Courtis <alex@courtis.org>

* fix format string

* style

---------

Co-authored-by: Alexander Courtis <alex@courtis.org>
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working PR please nvim-tree team does not have the bandwidth to implement; a PR will be gratefully appreciated reproduced Issue confirmed
Projects
None yet
Development

No branches or pull requests

5 participants