Search code examples
luaneovimneovim-plugin

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()]])

Solution

  • 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()]])