Prechádzať zdrojové kódy

refactor: more configurable format-on-save (#1937)

kylo252 3 rokov pred
rodič
commit
b0a9ee720a

+ 6 - 1
lua/lvim/config/defaults.lua

@@ -3,7 +3,12 @@ return {
   colorscheme = "onedarker",
   line_wrap_cursor_movement = true,
   transparent_window = false,
-  format_on_save = true,
+  format_on_save = {
+    ---@usage pattern string pattern used for the autocommand (Default: '*')
+    pattern = "*",
+    ---@usage timeout number timeout in ms for the format request (Default: 1000)
+    timeout = 1000,
+  },
   keys = {},
 
   builtin = {},

+ 3 - 2
lua/lvim/config/init.lua

@@ -20,7 +20,7 @@ end
 -- Define lvim global variable
 function M:init()
   if vim.tbl_isempty(lvim or {}) then
-    lvim = require "lvim.config.defaults"
+    lvim = vim.deepcopy(require "lvim.config.defaults")
     local home_dir = vim.loop.os_homedir()
     lvim.vsnip_dir = utils.join_paths(home_dir, ".config", "snippets")
     lvim.database = { save_location = utils.join_paths(home_dir, ".config", "lunarvim_db"), auto_execute = 1 }
@@ -114,7 +114,8 @@ function M:reload()
   M:load()
 
   local plugins = require "lvim.plugins"
-  utils.toggle_autoformat()
+  local autocmds = require "lvim.core.autocmds"
+  autocmds.configure_format_on_save()
   local plugin_loader = require "lvim.plugin-loader"
   plugin_loader.cache_clear()
   plugin_loader.load { plugins, lvim.plugins }

+ 55 - 0
lua/lvim/core/autocmds.lua

@@ -1,4 +1,5 @@
 local M = {}
+local Log = require "lvim.core.log"
 
 --- Load the default set of autogroups and autocommands.
 function M.load_augroups()
@@ -58,6 +59,60 @@ function M.load_augroups()
   }
 end
 
+local get_format_on_save_opts = function()
+  local defaults = require("lvim.config.defaults").format_on_save
+  -- accept a basic boolean `lvim.format_on_save=true`
+  if type(lvim.format_on_save) ~= "table" then
+    return defaults
+  end
+
+  return {
+    pattern = lvim.format_on_save.pattern or defaults.pattern,
+    timeout = lvim.format_on_save.timeout or defaults.timeout,
+  }
+end
+
+function M.enable_format_on_save(opts)
+  local fmd_cmd = string.format(":silent lua vim.lsp.buf.formatting_sync({}, %s)", opts.timeout_ms)
+  M.define_augroups {
+    format_on_save = { { "BufWritePre", opts.pattern, fmd_cmd } },
+  }
+  Log:debug "enabled format-on-save"
+end
+
+function M.disable_format_on_save()
+  M.remove_augroup "format_on_save"
+  Log:debug "disabled format-on-save"
+end
+
+function M.configure_format_on_save()
+  if lvim.format_on_save then
+    if vim.fn.exists "#format_on_save#BufWritePre" == 1 then
+      M.remove_augroup "format_on_save"
+      Log:debug "reloading format-on-save configuration"
+    end
+    local opts = get_format_on_save_opts()
+    M.enable_format_on_save(opts)
+  else
+    M.disable_format_on_save()
+  end
+end
+
+function M.toggle_format_on_save()
+  if vim.fn.exists "#format_on_save#BufWritePre" == 0 then
+    local opts = get_format_on_save_opts()
+    M.enable_format_on_save(opts)
+  else
+    M.disable_format_on_save()
+  end
+end
+
+function M.remove_augroup(name)
+  if vim.fn.exists("#" .. name) == 1 then
+    vim.cmd("au! " .. name)
+  end
+end
+
 function M.define_augroups(definitions) -- {{{1
   -- Create autocommand groups based on the passed definitions
   --

+ 1 - 0
lua/lvim/core/commands.lua

@@ -16,6 +16,7 @@ M.defaults = {
   [[ command! LvimUpdate lua require('lvim.bootstrap').update() ]],
   [[ command! LvimSyncCorePlugins lua require('lvim.plugin-loader'):sync_core_plugins() ]],
   [[ command! LvimReload lua require('lvim.config'):reload() ]],
+  [[ command! LvimToggleFormatOnSave lua require('lvim.core.autocmds').toggle_format_on_save() ]],
 }
 
 M.load = function(commands)

+ 1 - 1
lua/lvim/lsp/init.lua

@@ -171,7 +171,7 @@ function M.setup()
 
   require("lvim.lsp.null-ls").setup()
 
-  require("lvim.utils").toggle_autoformat()
+  require("lvim.core.autocmds").configure_format_on_save()
 end
 
 return M

+ 0 - 26
lua/lvim/utils/init.lua

@@ -1,5 +1,4 @@
 local utils = {}
-local Log = require "lvim.core.log"
 local uv = vim.loop
 
 -- recursive Print (structure, limit, separator)
@@ -58,31 +57,6 @@ function utils.generate_settings()
   io.close(file)
 end
 
--- autoformat
-function utils.toggle_autoformat()
-  if lvim.format_on_save then
-    require("lvim.core.autocmds").define_augroups {
-      autoformat = {
-        {
-          "BufWritePre",
-          "*",
-          ":silent lua vim.lsp.buf.formatting_sync()",
-        },
-      },
-    }
-    Log:debug "Format on save active"
-  end
-
-  if not lvim.format_on_save then
-    vim.cmd [[
-      if exists('#autoformat#BufWritePre')
-        :autocmd! autoformat
-      endif
-    ]]
-    Log:debug "Format on save off"
-  end
-end
-
 function utils.unrequire(m)
   package.loaded[m] = nil
   _G[m] = nil