I’m using NvChad and in ~/.config/nvim/lua/core/mappings.lua there is a keybinding for LSP code action:

  ["<leader>ca"] = {
    function()
      vim.lsp.buf.code_action()
      -- TODO: write buffer to file
    end,
    "LSP code action",
},

this keybinding applies the code action, but does not write to file. I want to write changes to file as soon as I’ve applied the code action.

How can I use the documentation at https://neovim.io/doc/ to find the correct function? I’ve tried looking for a write() function but I could not find anything I can call from lua.

  • ash@lemmy.fmhy.ml
    link
    fedilink
    English
    arrow-up
    1
    ·
    1 year ago

    You can wrap the call in pcall, which is a lua builtin for catching errors, which would suppress the error and let you know if the command failed.

    You could for example do:

    local ok, res = pcall(vim.cmd.write)
    if not ok
    then
        vim.notify('write failed with: ' .. res)
    end
    

    There are both lua and vim functions for writing to files but I recommend to not use them in this scenario, they write to the file directly and dont trigger autocommands.

    I understand your frustration with no consitent error reporting and clear api, but I guess that’s the consequence of the entire history of vi and vim and trying to be backwards compatible.