]> git.rmz.io Git - dotfiles.git/blobdiff - nvim/lua/plugins/formatter.lua
lazyvim: absorb formatting plugins
[dotfiles.git] / nvim / lua / plugins / formatter.lua
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,
+  },
+}