Lua Neovim插件在文本更改时加载`nvim_buf_clear_namespace`。

huangapple go评论63阅读模式
英文:

Lua neovim plugin load nvim_buf_clear_namespace when textchange

问题

I started using neovim recently and for a learning Example i tried to create a basic plugin to update packages for node modules inside the package.json

The idea is when the text changes or when trying to undo i clear the buffer.

Calling this function : lua Clear_highlights() do the job but it's seems when the text changes or undo seems not triggering the function Clear_highlights() am for sure missing something if someone can help me catch this will be awesome.

Thank you.

line 20 : Github repo

-- Clear buffer
Clear_highlights = function()
vim.api.nvim_buf_clear_namespace(0, require("nodePackageCheck").Config.get_namespace_id(), 0, -1)
end

vim.cmd([[autocmd! TextChanged,TextChangedI, BufWritePost * lua Clear_highlights()]])

英文:

I started using neovim recently and for a learning Example i tried to create a basic plugin to update packages for node modules inside the package.json

The idea is when the text changes or when trying to undo i clear the buffer.

Calling this function : lua Clear_highlights() do the job but it's seems when the text changes or undo seems not triggering the function Clear_highlights() am for sure missing something if someone can help me catch this will be awesome.

Thank you.

line 20 : Github repo

-- Clear buffer
Clear_highlights = function()
	vim.api.nvim_buf_clear_namespace(0, require("nodePackageCheck").Config.get_namespace_id(), 0, -1)
end

vim.cmd([[autocmd! TextChanged,TextChangedI, BufWritePost  * lua Clear_highlights()]])

答案1

得分: 1

vim.cmd([[autocmd! TextChanged,TextChangedI, BufWritePost * lua Clear_highlights()]])

  • autocmd! 用于删除处理程序/组,而不是定义它们。删除感叹号 (!)。
  • 随机的空格会混淆 vim 的解析器:在这里它将 BufWritePost 视为“模式”,* 为处理程序(命令)的起始。

尝试这个改写:

vim.cmd([[autocmd TextChanged,TextChangedI,BufWritePost * lua Clear_highlights()]])

英文:
vim.cmd([[autocmd! TextChanged,TextChangedI, BufWritePost  * lua Clear_highlights()]])
  • autocmd! is for deleting handlers/groups, not defining them. Remove the bang (!).
  • Random whitespace confuses vim's parser: here it treats BufWritePost as the "pattern", and * as the start of the handler (command).

Try this instead:

vim.cmd([[autocmd TextChanged,TextChangedI,BufWritePost * lua Clear_highlights()]])

答案2

得分: 0

我的最终解决方案是在普通模式下重新映射 undo 并调用 Clear_highlights()

vim.api.nvim_set_keymap("n", "u", ":undo<CR>:lua Clear_highlights()<CR>", { noremap = true, silent = true })
英文:

My final solution was a remapping undo in normal mode and call Clear_highlights() with.

vim.api.nvim_set_keymap(&quot;n&quot;, &quot;u&quot;, &quot;:undo&lt;CR&gt;:lua Clear_highlights()&lt;CR&gt;&quot;, { noremap = true, silent = true })

huangapple
  • 本文由 发表于 2023年6月5日 03:59:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402186.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定