Open ecerulm opened 1 month ago
For a workaround I'm using
require("formatter").setup({
...
})
OLD_FORMAT = require("formatter.format").format
require("formatter.format").format = function(args, mods, start_line, end_line, opts)
-- print("my formatter",vim.api.nvim_get_option_value('modified', {buf = 0})) -- 0 means the current buffer
if vim.api.nvim_get_option_value("modified", { buf = 0 }) then
vim.api.nvim_err_writeln("Attempt to :Format an modified buffer, save first")
return false
end
return OLD_FORMAT(args, mods, start_line, end_line, opts)
end
which replaces the formatter.format.format()
provided by formatter.nvim source code with a wrapper that check if the current buffer (0
) is modified or not.
If you have a buffer with changes, and you haven't saved them yet and perform a ':Format' or ':FormatWrite' the formatting is applied to the file contents not the buffer contents so you will lose your changes.
:Format
should refuse (by default) to run if the buffer has the'modified'
flag , to prevent data loss.