A Neovim plugin to register specific code actions to be run automatically on file save, based on file patterns.
Using lazy.nvim:
{
"fnune/codeactions-on-save.nvim",
config = function()
local cos = require("codeactions-on-save")
cos.register({ "*.py" }, { "source.organizeImports" })
cos.register({ "*.ts", "*.tsx" }, { "source.organizeImports.biome", "source.fixAll" })
end
}The default timeout for code actions is 100ms. You can pass anything else if necessary, in ms:
cos.register({ "*.ts" }, { "source.organizeImports" }, 3000)If you already have a BufWritePre autocmd (for formatting, custom logic, etc.)
and want to run code actions inline rather than via a separate autocmd, call
apply directly:
vim.api.nvim_create_autocmd("BufWritePre", {
pattern = { "*.ts" },
callback = function(args)
require("codeactions-on-save").apply(
{ "source.organizeImports", "source.fixAll" },
args.buf,
3000
)
-- ... other on-save work
end,
})apply(kinds, buf, timeout_ms?) is the same mechanism register uses
internally; the timeout defaults to 100ms.
To find out the action kind of the action you want to run, use this while inside a buffer with an LSP client attached:
:lua require("codeactions-on-save").inspect()