]> git.sthu.org Git - vimconf.git/commitdiff
plugins: Replace formatter by conform on nvim-0.10+
authorStefan Huber <shuber@sthu.org>
Thu, 17 Apr 2025 09:35:07 +0000 (11:35 +0200)
committerStefan Huber <shuber@sthu.org>
Thu, 17 Apr 2025 09:40:59 +0000 (11:40 +0200)
plugins-full.vim.example

index 8b3576572066f73fd862a5bc6d56a81f591c2072..fbf28d02bd1a4cdc55ed22fbd318595765610404 100644 (file)
@@ -19,7 +19,13 @@ if has('nvim-0.8')
     Plug 'neovim/nvim-lspconfig'
     Plug 'williamboman/mason-lspconfig.nvim'
     Plug 'mfussenegger/nvim-lint'
-    Plug 'mhartington/formatter.nvim'
+
+    if has('nvim-0.10')
+        Plug 'stevearc/conform.nvim'
+        Plug 'shuber2/mason-conform', {'branch': 'fix-autoinstall-registrywarning'}
+    else
+        Plug 'mhartington/formatter.nvim'
+    endif
 endif
 
 
@@ -276,43 +282,69 @@ EOF
                 -- end
             }
 
-            local formatter = require("formatter")
-
-            -- Provides the Format, FormatWrite, FormatLock, and FormatWriteLock commands
-            require("formatter").setup {
-              -- Enable or disable logging
-              logging = true,
-              -- Set the log level
-              log_level = vim.log.levels.WARN,
-              -- All formatter configurations are opt-in
-              filetype = {
-                bib = {
-                  function()
-                    return {
-                      exe = "bibtex-tidy"
-                    }
-                    end
+            -- Conform requires nvim-0.10
+            if vim.fn.has('nvim-0.10') == 1 then
+                require("mason-conform").setup({})
+                require("conform").setup({
+                  formatters_by_ft = {
+                    bib = { "bibtex-tidy"},
                   },
-                c = { require("formatter.filetypes.c").clangformat },
-                cpp = { require("formatter.filetypes.cpp").clangformat },
-                css = require('formatter.filetypes.css').prettier,
-                html = require('formatter.filetypes.html').prettier,
-                java = { require("formatter.filetypes.java").clangformat },
-                javascript = require('formatter.filetypes.javascript').prettier,
-                json = require('formatter.filetypes.json').jq,
-                python = { require("formatter.filetypes.python").autopep8 },
-                xml = { require("formatter.filetypes.xml").xmllint },
-                lua = { require("formatter.filetypes.lua").stylua },
-
-                -- Use the special "*" filetype for defining formatter configurations on
-                -- any filetype
-                ["*"] = {
-                  -- "formatter.filetypes.any" defines default configurations for any
-                  -- filetype
-                  require("formatter.filetypes.any").remove_trailing_whitespace
+                })
+
+                -- https://github.com/stevearc/conform.nvim/blob/master/doc/recipes.md#format-command
+                vim.api.nvim_create_user_command("Format",
+                  function(args)
+                    local range = nil
+                    if args.count ~= -1 then
+                      local end_line = vim.api.nvim_buf_get_lines(0, args.line2 - 1, args.line2, true)[1]
+                      range = {
+                          start = { args.line1, 0 },
+                          ["end"] = { args.line2, end_line:len() },
+                      }
+                    end
+                    require("conform").format({ async = true, lsp_format = "fallback", range = range })
+                  end,
+                  { range = true }
+                )
+            else
+                local formatter = require("formatter")
+
+                -- Provides the Format, FormatWrite, FormatLock, and FormatWriteLock commands
+                require("formatter").setup {
+                  -- Enable or disable logging
+                  logging = true,
+                  -- Set the log level
+                  log_level = vim.log.levels.WARN,
+                  -- All formatter configurations are opt-in
+                  filetype = {
+                    bib = {
+                      function()
+                        return {
+                          exe = "bibtex-tidy"
+                        }
+                        end
+                      },
+                    c = { require("formatter.filetypes.c").clangformat },
+                    cpp = { require("formatter.filetypes.cpp").clangformat },
+                    css = require('formatter.filetypes.css').prettier,
+                    html = require('formatter.filetypes.html').prettier,
+                    java = { require("formatter.filetypes.java").clangformat },
+                    javascript = require('formatter.filetypes.javascript').prettier,
+                    json = require('formatter.filetypes.json').jq,
+                    python = { require("formatter.filetypes.python").autopep8 },
+                    xml = { require("formatter.filetypes.xml").xmllint },
+                    lua = { require("formatter.filetypes.lua").stylua },
+
+                    -- Use the special "*" filetype for defining formatter configurations on
+                    -- any filetype
+                    ["*"] = {
+                      -- "formatter.filetypes.any" defines default configurations for any
+                      -- filetype
+                      require("formatter.filetypes.any").remove_trailing_whitespace
+                    }
+                  }
                 }
-              }
-            }
+            end
 
             require('lint').linters_by_ft = {
                 gitcommit = {'gitlint',},