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 `<leader>vc`.

AutoFormat on saving remains disabled by default but can be enabled with
:FormatEnable (and disabled again with :FormatDisable) or quickly through
`<localleader>lL`.

Manual formatting works like before with `<localleader>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.
This commit is contained in:
Marty Oehme 2023-12-08 16:59:09 +01:00
parent 1def627edd
commit 293dc8a467
Signed by: Marty
GPG key ID: EDBF2ED917B2EF6A
8 changed files with 81 additions and 48 deletions

View file

@ -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

View file

@ -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" },

View file

@ -178,11 +178,6 @@ map("n", "<localleader>q", "gqap", { silent = true, desc = "Format current parag
map("x", "<localleader>q", "gq", { silent = true, desc = "Format {motion}" })
map("n", "<localleader>Q", "vapJgqap", { silent = true, desc = "Unformat then format paragraph" })
-- FORMAT code with
-- PLUGIN: formatter.nvim
map("n", "<localleader>f", ":FormatLock<cr>")
map("n", "<localleader>F", ":FormatWriteLock<cr>")
-- 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(
'<cmd>lua require("colorizer").attach_to_buffer(0, {mode = "background"} )<cr>',
{ silent = true, desc = "colorize background" }
)
map("n", "<leader>sa", "<cmd>FormatOnSave<cr>", { silent = true, desc = "toggle format on save" })

View file

@ -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

View file

@ -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 = "?" }
)

View file

@ -1 +0,0 @@
require("personal.format_on_save_toggle")

View file

@ -100,7 +100,6 @@ local function on_attach(client, bufnr)
{ buffer = bufnr, desc = "Implementation" }
)
end
map("n", "<localleader>ll", "<cmd>lua vim.lsp.buf.format()<CR>", { buffer = bufnr, desc = "Format document" })
map("n", "K", "<cmd>lua vim.lsp.buf.hover()<cr>", { buffer = bufnr, desc = "Hover definition" })
map("n", "<localleader>lc", "<cmd>lua vim.lsp.buf.declaration()<cr>", { 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

View file

@ -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 = {
{
"<localleader>ll",
function()
require("conform").format({ async = true, lsp_fallback = true })
end,
desc = "Format buffer",
},
{
"<localleader>lL",
function()
vim.g.disable_autoformat = not vim.g.disable_autoformat
end,
desc = "Toggle AutoFormat",
},
{
"<leader>vf",
":ConformInfo<cr>",
desc = "ConformInfo",
},
},
},
-- completion setup
{
"hrsh7th/nvim-cmp",