]> git.rmz.io Git - dotfiles.git/commitdiff
lazyvim: absorb formatting plugins
authorSamir Benmendil <me@rmz.io>
Sun, 9 Feb 2025 22:30:41 +0000 (22:30 +0000)
committerSamir Benmendil <me@rmz.io>
Sun, 2 Mar 2025 16:05:27 +0000 (16:05 +0000)
nvim/lua/config/keymaps.lua
nvim/lua/plugins/formatter.lua [new file with mode: 0644]
nvim/lua/rmz/util/lsp.lua

index 9b33bbf0924f356f5bd2f7c19b282e0a86d5d5f6..a0285ced9cff778e74b0c92b5981ed34c09dfe99 100644 (file)
@@ -75,7 +75,6 @@ vim.keymap.set("n", "[w", diagnostic_goto(false, "WARN"), { desc = "Prev Warning
 -- stylua: ignore start
 
 -- toggle options
 -- stylua: ignore start
 
 -- toggle options
-vim.keymap.set("n", "<leader>uf", Util.format.toggle, { desc = "Toggle format on Save" })
 vim.keymap.set("n", "<leader>ud", Snacks.toggle.diagnostics, { desc = "Toggle Diagnostics" })
 local conceallevel = vim.o.conceallevel > 0 and vim.o.conceallevel or 3
 vim.keymap.set("n", "<leader>uc", function() Snacks.toggle("conceallevel", false, {0, conceallevel}) end, { desc = "Toggle Conceal" })
 vim.keymap.set("n", "<leader>ud", Snacks.toggle.diagnostics, { desc = "Toggle Diagnostics" })
 local conceallevel = vim.o.conceallevel > 0 and vim.o.conceallevel or 3
 vim.keymap.set("n", "<leader>uc", function() Snacks.toggle("conceallevel", false, {0, conceallevel}) end, { desc = "Toggle Conceal" })
diff --git a/nvim/lua/plugins/formatter.lua b/nvim/lua/plugins/formatter.lua
new file mode 100644 (file)
index 0000000..2dd44ab
--- /dev/null
@@ -0,0 +1,47 @@
+--- TODO: add toggle for format on save
+--- https://github.com/stevearc/conform.nvim/blob/master/doc/recipes.md#command-to-toggle-format-on-save
+--- previous keymap was <leader>uf
+
+---@type LazySpec
+return {
+  { "stevearc/conform.nvim",
+    dependencies = { "mason.nvim" },
+    lazy = true,
+    cmd = "ConformInfo",
+    keys = {
+      {"<leader>cf", function() require("conform").format({ async = true }) end, mode = "", desc = "Format buffer" },
+      {
+        "<leader>cF",
+        function()
+          require("conform").format({ formatters = { "injected" }, timeout_ms = 3000 })
+        end,
+        mode = { "n", "v" },
+        desc = "Format Injected Langs",
+      },
+    },
+    ---@module "conform"
+    ---@type conform.setupOpts
+    opts = {
+      default_format_opts = {
+        timeout_ms = 3000,
+        async = false, -- not recommended to change
+        quiet = false, -- not recommended to change
+        lsp_format = "fallback", -- not recommended to change
+      },
+      formatters_by_ft = {
+        lua = { "stylua" },
+        fish = { "fish_indent" },
+        sh = { "shfmt" },
+      },
+      -- The options you set here will be merged with the builtin formatters.
+      -- You can also define any custom formatters here.
+      ---@type table<string, conform.FormatterConfigOverride|fun(bufnr: integer): nil|conform.FormatterConfigOverride>
+      formatters = {
+        injected = { options = { ignore_errors = true } },
+      },
+    },
+    init = function ()
+      vim.opt.formatexpr = "v:lua.require'conform'.formatexpr()"
+    end,
+  },
+}
index 7424803bf5523069febcd53acc9420f3badf3af5..662781cfebeafd9d711a33b2fc8195380f27dca2 100644 (file)
@@ -189,58 +189,6 @@ function M.disable(server, cond)
   end)
 end
 
   end)
 end
 
----@param opts? LazyFormatter| {filter?: (string|lsp.Client.filter)}
-function M.formatter(opts)
-  opts = opts or {}
-  local filter = opts.filter or {}
-  filter = type(filter) == "string" and { name = filter } or filter
-  ---@cast filter lsp.Client.filter
-  ---@type LazyFormatter
-  local ret = {
-    name = "LSP",
-    primary = true,
-    priority = 1,
-    format = function(buf)
-      M.format(LazyVim.merge({}, filter, { bufnr = buf }))
-    end,
-    sources = function(buf)
-      local clients = M.get_clients(LazyVim.merge({}, filter, { bufnr = buf }))
-      ---@param client vim.lsp.Client
-      local ret = vim.tbl_filter(function(client)
-        return client.supports_method("textDocument/formatting")
-          or client.supports_method("textDocument/rangeFormatting")
-      end, clients)
-      ---@param client vim.lsp.Client
-      return vim.tbl_map(function(client)
-        return client.name
-      end, ret)
-    end,
-  }
-  return LazyVim.merge(ret, opts) --[[@as LazyFormatter]]
-end
-
----@alias lsp.Client.format {timeout_ms?: number, format_options?: table} | lsp.Client.filter
-
----@param opts? lsp.Client.format
-function M.format(opts)
-  opts = vim.tbl_deep_extend(
-    "force",
-    {},
-    opts or {},
-    LazyVim.opts("nvim-lspconfig").format or {},
-    LazyVim.opts("conform.nvim").format or {}
-  )
-  local ok, conform = pcall(require, "conform")
-  -- use conform for formatting with LSP when available,
-  -- since it has better format diffing
-  if ok then
-    opts.formatters = {}
-    conform.format(opts)
-  else
-    vim.lsp.buf.format(opts)
-  end
-end
-
 M.action = setmetatable({}, {
   __index = function(_, action)
     return function()
 M.action = setmetatable({}, {
   __index = function(_, action)
     return function()