diff --git a/lua/guard/format.lua b/lua/guard/format.lua index e1bf7a06..890300a2 100644 --- a/lua/guard/format.lua +++ b/lua/guard/format.lua @@ -6,6 +6,8 @@ local get_prev_lines = require('guard.util').get_prev_lines local filetype = require('guard.filetype') local util = require('guard.util') +local is_formatting = false + local function ignored(buf, patterns) local fname = api.nvim_buf_get_name(buf) if #fname == 0 then @@ -66,9 +68,12 @@ local function update_buffer(bufnr, new_lines, srow, erow) end local function do_fmt(buf) + is_formatting = true + buf = buf or api.nvim_get_current_buf() if not filetype[vim.bo[buf].filetype] then vim.notify('[Guard] missing config for filetype ' .. vim.bo[buf].filetype, vim.log.levels.ERROR) + is_formatting = false return end local srow = 0 @@ -116,6 +121,7 @@ local function do_fmt(buf) elseif config.fn then config.fn() if i == #fmt_configs then + is_formatting = false return end new_lines = table.concat(get_prev_lines(buf, srow, erow), '') @@ -125,10 +131,14 @@ local function do_fmt(buf) end vim.schedule(function() + is_formatting = false + if not api.nvim_buf_is_valid(buf) or changedtick ~= api.nvim_buf_get_changedtick(buf) then return end + update_buffer(buf, new_lines, srow, erow) + if reload and api.nvim_get_current_buf() == buf then vim.cmd.edit() end @@ -136,6 +146,11 @@ local function do_fmt(buf) end)) end +local function is_formatting_fn() + return is_formatting +end + return { do_fmt = do_fmt, + is_formatting = is_formatting_fn }