From: Stefan Huber Date: Thu, 17 Apr 2025 09:35:07 +0000 (+0200) Subject: plugins: Replace formatter by conform on nvim-0.10+ X-Git-Url: https://git.sthu.org/?a=commitdiff_plain;h=06e3ad85c0ea4da5094ddf4e50b2cd0b4333f1cf;p=vimconf.git plugins: Replace formatter by conform on nvim-0.10+ --- diff --git a/plugins-full.vim.example b/plugins-full.vim.example index 8b35765..fbf28d0 100644 --- a/plugins-full.vim.example +++ b/plugins-full.vim.example @@ -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',},