From 293dc8a467fe4522908cfb7238bb000476b552ce Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Fri, 8 Dec 2023 16:59:09 +0100 Subject: [PATCH] nvim: Add conform.nvim formatter In the process of moving away from null-ls, added formatting with the help of conform.nvim. Brings one new command, :ConformInfo which can also be reached via `vc`. AutoFormat on saving remains disabled by default but can be enabled with :FormatEnable (and disabled again with :FormatDisable) or quickly through `lL`. Manual formatting works like before with `ll`. Uses the formatters set in the plugin (similar setup to null-ls before) but automatically falls back to lsp formatters if it does not have its own and lsp has one enabled. --- nvim/.config/nvim/init.lua | 1 - nvim/.config/nvim/lazy-lock.json | 1 + nvim/.config/nvim/lua/core/mappings.lua | 7 -- nvim/.config/nvim/lua/core/settings.lua | 2 +- .../lua/personal/format_on_save_toggle.lua | 33 -------- nvim/.config/nvim/lua/personal/init.lua | 1 - nvim/.config/nvim/lua/plugins/config/lsp.lua | 5 -- nvim/.config/nvim/lua/plugins/ide.lua | 79 +++++++++++++++++++ 8 files changed, 81 insertions(+), 48 deletions(-) delete mode 100644 nvim/.config/nvim/lua/personal/format_on_save_toggle.lua delete mode 100644 nvim/.config/nvim/lua/personal/init.lua diff --git a/nvim/.config/nvim/init.lua b/nvim/.config/nvim/init.lua index 7abf033..7525255 100644 --- a/nvim/.config/nvim/init.lua +++ b/nvim/.config/nvim/init.lua @@ -10,7 +10,6 @@ for _, source in ipairs({ "core.autocmds", "core.mappings", "core.look", - "personal", }) do local status_ok, fault = pcall(require, source) if not status_ok then diff --git a/nvim/.config/nvim/lazy-lock.json b/nvim/.config/nvim/lazy-lock.json index ec26a1b..bdef787 100644 --- a/nvim/.config/nvim/lazy-lock.json +++ b/nvim/.config/nvim/lazy-lock.json @@ -23,6 +23,7 @@ "cmp-treesitter": { "branch": "master", "commit": "b8bc760dfcc624edd5454f0982b63786a822eed9" }, "cmp_luasnip": { "branch": "master", "commit": "05a9ab28b53f71d1aece421ef32fee2cb857a843" }, "completion-vcard": { "branch": "master", "commit": "2220fd517a985ececed1adcf0e5be8f2815564c7" }, + "conform.nvim": { "branch": "master", "commit": "ce95e6b2a072d81fd312ff213f241cacab1eccef" }, "dial.nvim": { "branch": "master", "commit": "54b503f906bc9e5ab85288414840a1b86d40769f" }, "dressing.nvim": { "branch": "master", "commit": "8b7ae53d7f04f33be3439a441db8071c96092d19" }, "fidget.nvim": { "branch": "main", "commit": "0ba1e16d07627532b6cae915cc992ecac249fb97" }, diff --git a/nvim/.config/nvim/lua/core/mappings.lua b/nvim/.config/nvim/lua/core/mappings.lua index 6278326..755321d 100644 --- a/nvim/.config/nvim/lua/core/mappings.lua +++ b/nvim/.config/nvim/lua/core/mappings.lua @@ -178,11 +178,6 @@ map("n", "q", "gqap", { silent = true, desc = "Format current parag map("x", "q", "gq", { silent = true, desc = "Format {motion}" }) map("n", "Q", "vapJgqap", { silent = true, desc = "Unformat then format paragraph" }) --- FORMAT code with --- PLUGIN: formatter.nvim -map("n", "f", ":FormatLock") -map("n", "F", ":FormatWriteLock") - -- SPELL CHECKING -- Move to the prev/next spelling error with [S ]S -- Move to the prev/next spelling error or suggestion with [s ]s @@ -280,5 +275,3 @@ map( 'lua require("colorizer").attach_to_buffer(0, {mode = "background"} )', { silent = true, desc = "colorize background" } ) - -map("n", "sa", "FormatOnSave", { silent = true, desc = "toggle format on save" }) diff --git a/nvim/.config/nvim/lua/core/settings.lua b/nvim/.config/nvim/lua/core/settings.lua index 64d5047..3686e89 100644 --- a/nvim/.config/nvim/lua/core/settings.lua +++ b/nvim/.config/nvim/lua/core/settings.lua @@ -84,7 +84,7 @@ local globals = { maplocalleader = ",", tex_flavor = "latex", - format_on_save = false, -- from personal toggle function + disable_autoformat = true, -- only format files manually } for o, v in pairs(globals) do diff --git a/nvim/.config/nvim/lua/personal/format_on_save_toggle.lua b/nvim/.config/nvim/lua/personal/format_on_save_toggle.lua deleted file mode 100644 index 04b7c9d..0000000 --- a/nvim/.config/nvim/lua/personal/format_on_save_toggle.lua +++ /dev/null @@ -1,33 +0,0 @@ -local function stop_formatting() - vim.api.nvim_del_augroup_by_name("LspFormat") -end - -local function start_formatting() - for _, client in pairs(vim.lsp.get_active_clients()) do - require("lsp-setup.utils").format_on_save(client) - end -end - -local function _toggle(opts) - if opts.args == "" then - vim.g.format_on_save = not vim.g.format_on_save - elseif opts.args == "on" or opts.args == "1" then - vim.g.format_on_save = true - elseif opts.args == "off" or opts.args == "0" then - vim.g.format_on_save = false - else - vim.notify("Please provide arguments 'on' or 'off' or non arguments to toggle.") - end - - if vim.g.format_on_save == true then - start_formatting() - else - stop_formatting() - end -end - -vim.api.nvim_create_user_command( - "FormatOnSave", - _toggle, - { desc = "toggle automatically formatting on save", nargs = "?" } -) diff --git a/nvim/.config/nvim/lua/personal/init.lua b/nvim/.config/nvim/lua/personal/init.lua deleted file mode 100644 index 8c2b568..0000000 --- a/nvim/.config/nvim/lua/personal/init.lua +++ /dev/null @@ -1 +0,0 @@ -require("personal.format_on_save_toggle") diff --git a/nvim/.config/nvim/lua/plugins/config/lsp.lua b/nvim/.config/nvim/lua/plugins/config/lsp.lua index d46c0e0..bc9fe9a 100644 --- a/nvim/.config/nvim/lua/plugins/config/lsp.lua +++ b/nvim/.config/nvim/lua/plugins/config/lsp.lua @@ -100,7 +100,6 @@ local function on_attach(client, bufnr) { buffer = bufnr, desc = "Implementation" } ) end - map("n", "ll", "lua vim.lsp.buf.format()", { buffer = bufnr, desc = "Format document" }) map("n", "K", "lua vim.lsp.buf.hover()", { buffer = bufnr, desc = "Hover definition" }) map("n", "lc", "lua vim.lsp.buf.declaration()", { buffer = bufnr, desc = "Declaration" }) map( @@ -116,10 +115,6 @@ local function on_attach(client, bufnr) vim.diagnostic.disable(0) end end, { buffer = bufnr, desc = "Disable buffer diagnostics" }) - - if vim.g.format_on_save then - require("lsp-setup.utils").format_on_save(client) - end end -- Display diagnostics as virtual text only if not in insert mode diff --git a/nvim/.config/nvim/lua/plugins/ide.lua b/nvim/.config/nvim/lua/plugins/ide.lua index 061f2ab..cf8acd6 100644 --- a/nvim/.config/nvim/lua/plugins/ide.lua +++ b/nvim/.config/nvim/lua/plugins/ide.lua @@ -66,6 +66,85 @@ return { require("plugins.config.lsp") end, }, + + + -- formatting setup + { + "stevearc/conform.nvim", + config = function() + require("conform").setup({ + lsp_fallback = true, + format_after_save = function(bufnr) + if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then + return + end + return { lsp_fallback = true } + end, + formatters_by_ft = { + angular = { { "prettierd", "prettier" } }, + astro = { { "prettierd", "prettier" } }, + bash = { "shfmt" }, + bib = { "bibtex-tidy" }, + javascript = { { "prettierd", "prettier" } }, + javascriptreact = { { "prettierd", "prettier" } }, + json = { "jq" }, + lua = { "stylua" }, + python = { "ruff_fix", "ruff_format" }, + sh = { "shfmt" }, + typescript = { { "prettierd", "prettier" } }, + vue = { { "prettierd", "prettier" } }, + zsh = { "shfmt" }, + }, + formatters = { + -- enable python isort functionality + ruff_fix = { + prepend_args = { "--select", "I" }, + }, + }, + }) + vim.api.nvim_create_user_command("FormatDisable", function(args) + if args.bang then + -- FormatDisable! will disable formatting just for this buffer + vim.b.disable_autoformat = true + else + vim.g.disable_autoformat = true + end + end, { + desc = "Disable formatting on save", + bang = true, + }) + vim.api.nvim_create_user_command("FormatEnable", function() + vim.b.disable_autoformat = false + vim.g.disable_autoformat = false + end, { + desc = "Enable formatting on save", + }) + end, + cmd = { "ConformInfo" }, + event = { "BufReadPre" }, + keys = { + { + "ll", + function() + require("conform").format({ async = true, lsp_fallback = true }) + end, + desc = "Format buffer", + }, + { + "lL", + function() + vim.g.disable_autoformat = not vim.g.disable_autoformat + end, + desc = "Toggle AutoFormat", + }, + { + "vf", + ":ConformInfo", + desc = "ConformInfo", + }, + }, + }, + -- completion setup { "hrsh7th/nvim-cmp",