From da80d06ecd06c45e274c4e8e10c0dcb45e62c63a Mon Sep 17 00:00:00 2001 From: ej-shafran Date: Thu, 31 Aug 2023 11:59:45 +0300 Subject: [PATCH 1/4] Add `GuardFmtNoSave` command --- lua/guard/format.lua | 14 ++++++++++---- plugin/guard.lua | 3 +++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lua/guard/format.lua b/lua/guard/format.lua index 79945613..a7d2d02d 100644 --- a/lua/guard/format.lua +++ b/lua/guard/format.lua @@ -37,7 +37,11 @@ local function restore_views(views) end end -local function update_buffer(bufnr, prev_lines, new_lines, srow) +local function update_buffer(bufnr, prev_lines, new_lines, srow, save_on_fmt) + if save_on_fmt == nil then + save_on_fmt = true + end + if not new_lines or #new_lines == 0 then return end @@ -72,7 +76,9 @@ local function update_buffer(bufnr, prev_lines, new_lines, srow) end api.nvim_buf_set_lines(bufnr, s, e, false, replacement) end - api.nvim_command('silent! noautocmd write!') + if save_on_fmt then + api.nvim_command('silent! noautocmd write!') + end local mode = api.nvim_get_mode().mode if mode == 'v' or 'V' then vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('', true, false, true), 'n', true) @@ -114,7 +120,7 @@ local function override_lsp(buf) end end -local function do_fmt(buf) +local function do_fmt(buf, save_on_fmt) 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) @@ -189,7 +195,7 @@ local function do_fmt(buf) if not api.nvim_buf_is_valid(buf) or changedtick ~= api.nvim_buf_get_changedtick(buf) then return end - update_buffer(buf, prev_lines, new_lines, srow) + update_buffer(buf, prev_lines, new_lines, srow, save_on_fmt) if reload and api.nvim_get_current_buf() == buf then vim.cmd.edit() end diff --git a/plugin/guard.lua b/plugin/guard.lua index 27860c92..f0fd2663 100644 --- a/plugin/guard.lua +++ b/plugin/guard.lua @@ -29,3 +29,6 @@ end, { nargs = '?' }) vim.api.nvim_create_user_command('GuardFmt', function() require('guard.format').do_fmt() end, { nargs = 0 }) +vim.api.nvim_create_user_command('GuardFmtNoSave', function() + require('guard.format').do_fmt(nil, false) +end, { nargs = 0 }) From b6da4a2e46f0c65b95496f727db12f581985da61 Mon Sep 17 00:00:00 2001 From: ej-shafran Date: Thu, 8 Feb 2024 19:48:24 +0200 Subject: [PATCH 2/4] Add save_on_fmt option --- lua/guard/format.lua | 14 ++++++-------- lua/guard/init.lua | 14 +++++++++----- plugin/guard.lua | 3 --- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/lua/guard/format.lua b/lua/guard/format.lua index 2d1850d5..e160e65c 100644 --- a/lua/guard/format.lua +++ b/lua/guard/format.lua @@ -36,11 +36,7 @@ local function restore_views(views) end end -local function update_buffer(bufnr, prev_lines, new_lines, save_on_fmt) - if save_on_fmt == nil then - save_on_fmt = true - end - +local function update_buffer(bufnr, prev_lines, new_lines) if not new_lines or #new_lines == 0 then return end @@ -70,7 +66,9 @@ local function update_buffer(bufnr, prev_lines, new_lines, save_on_fmt) end api.nvim_buf_set_lines(bufnr, 0, -1, false, new_lines) - api.nvim_command('silent! noautocmd write!') + if require("guard").configuration.save_on_fmt then + api.nvim_command('silent! noautocmd write!') + end restore_views(views) end @@ -112,7 +110,7 @@ local function override_lsp(buf) end end -local function do_fmt(buf, save_on_fmt) +local function do_fmt(buf) 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) @@ -193,7 +191,7 @@ local function do_fmt(buf, save_on_fmt) }) return end - update_buffer(buf, prev_lines, new_lines, save_on_fmt) + update_buffer(buf, prev_lines, new_lines) if reload and api.nvim_get_current_buf() == buf then vim.cmd.edit() end diff --git a/lua/guard/init.lua b/lua/guard/init.lua index 6a6f9510..ca91334e 100644 --- a/lua/guard/init.lua +++ b/lua/guard/init.lua @@ -2,6 +2,8 @@ local util = require('guard.util') local ft_handler = require('guard.filetype') local events = require('guard.events') +local configuration = {} + local function register_cfg_by_table(fts_with_cfg) for ft, cfg in pairs(fts_with_cfg or {}) do if not vim.tbl_isempty(cfg) then @@ -28,23 +30,24 @@ local function resolve_multi_ft() end local function setup(opt) - opt = vim.tbl_extend('force', { + configuration = vim.tbl_extend('force', { fmt_on_save = true, lsp_as_default_formatter = false, + save_on_fmt = true, }, opt or {}) - register_cfg_by_table(opt.ft) + register_cfg_by_table(configuration.ft) resolve_multi_ft() - if opt.lsp_as_default_formatter then - events.create_lspattach_autocmd(opt.fmt_on_save) + if configuration.lsp_as_default_formatter then + events.create_lspattach_autocmd(configuration.fmt_on_save) end local lint = require('guard.lint') for ft, conf in pairs(ft_handler) do local lint_events = { 'BufWritePost', 'BufEnter' } - if conf.formatter and opt.fmt_on_save then + if conf.formatter and configuration.fmt_on_save then events.watch_ft(ft) lint_events[1] = 'User GuardFmt' end @@ -63,4 +66,5 @@ end return { setup = setup, + configuration = configuration, } diff --git a/plugin/guard.lua b/plugin/guard.lua index 2a40913e..a85867b1 100644 --- a/plugin/guard.lua +++ b/plugin/guard.lua @@ -34,6 +34,3 @@ end, { nargs = '?' }) vim.api.nvim_create_user_command('GuardFmt', function() require('guard.format').do_fmt() end, { nargs = 0 }) -vim.api.nvim_create_user_command('GuardFmtNoSave', function() - require('guard.format').do_fmt(nil, false) -end, { nargs = 0 }) From bc1d04a79aebfd5162448cc7097a743009d4145c Mon Sep 17 00:00:00 2001 From: ej-shafran Date: Thu, 8 Feb 2024 20:00:10 +0200 Subject: [PATCH 3/4] Make it work... --- lua/guard/format.lua | 6 ++++-- lua/guard/init.lua | 16 +++++++++------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/lua/guard/format.lua b/lua/guard/format.lua index e160e65c..25e8e062 100644 --- a/lua/guard/format.lua +++ b/lua/guard/format.lua @@ -49,7 +49,9 @@ local function update_buffer(bufnr, prev_lines, new_lines) if #new_lines ~= #prev_lines then api.nvim_buf_set_lines(bufnr, 0, -1, false, new_lines) - api.nvim_command('silent! noautocmd write!') + if require("guard").config.opts.save_on_fmt then + api.nvim_command('silent! noautocmd write!') + end restore_views(views) return end @@ -66,7 +68,7 @@ local function update_buffer(bufnr, prev_lines, new_lines) end api.nvim_buf_set_lines(bufnr, 0, -1, false, new_lines) - if require("guard").configuration.save_on_fmt then + if require("guard").config.opts.save_on_fmt then api.nvim_command('silent! noautocmd write!') end restore_views(views) diff --git a/lua/guard/init.lua b/lua/guard/init.lua index ca91334e..2c094502 100644 --- a/lua/guard/init.lua +++ b/lua/guard/init.lua @@ -2,7 +2,9 @@ local util = require('guard.util') local ft_handler = require('guard.filetype') local events = require('guard.events') -local configuration = {} +local config = { + opts = nil +} local function register_cfg_by_table(fts_with_cfg) for ft, cfg in pairs(fts_with_cfg or {}) do @@ -30,24 +32,24 @@ local function resolve_multi_ft() end local function setup(opt) - configuration = vim.tbl_extend('force', { + config.opts = vim.tbl_extend('force', { fmt_on_save = true, lsp_as_default_formatter = false, save_on_fmt = true, }, opt or {}) - register_cfg_by_table(configuration.ft) + register_cfg_by_table(config.opts.ft) resolve_multi_ft() - if configuration.lsp_as_default_formatter then - events.create_lspattach_autocmd(configuration.fmt_on_save) + if config.opts.lsp_as_default_formatter then + events.create_lspattach_autocmd(config.opts.fmt_on_save) end local lint = require('guard.lint') for ft, conf in pairs(ft_handler) do local lint_events = { 'BufWritePost', 'BufEnter' } - if conf.formatter and configuration.fmt_on_save then + if conf.formatter and config.opts.fmt_on_save then events.watch_ft(ft) lint_events[1] = 'User GuardFmt' end @@ -66,5 +68,5 @@ end return { setup = setup, - configuration = configuration, + config = config, } From e38a532c2040442b1f65c7cf55afd58459613653 Mon Sep 17 00:00:00 2001 From: ej-shafran Date: Fri, 9 Feb 2024 12:13:12 +0200 Subject: [PATCH 4/4] Update README --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3741a273..597d56f5 100644 --- a/README.md +++ b/README.md @@ -38,10 +38,13 @@ ft('lang'):fmt('format-tool-1') -- Call setup() LAST! require('guard').setup({ - -- the only options for the setup function + -- Choose to format on every write to a buffer fmt_on_save = true, -- Use lsp if no formatter was defined for this filetype lsp_as_default_formatter = false, + -- By default, Guard writes the buffer on every format + -- You can disable this by setting: + -- save_on_fmt = false, }) ```