nvim: Automatically install linters and formatters

Switch out mason-tool-installer for mason-conform.nvim and
mason-nvim-lint as respective wrappers for automatically installing
formatters and linters.

Follows the same principle as mason-lspconfig.nvim (in fact, the repos
are mostly based on the same code) and apply the concept to the other
tools: Whatever is enabled in the respective plugins (lspconfig,
nvim-lint and conform.nvim) will automatically be installed by mason.

This is really neat and basically takes care of me ever having to
interact much with Mason itself or manually set up the tools to be
installed. All I have to make sure is that they're updated once in a
while.
This commit is contained in:
Marty Oehme 2024-06-17 14:00:00 +02:00
parent b2dfdfd5ff
commit b4a9b5179f
Signed by: Marty
GPG key ID: EDBF2ED917B2EF6A
3 changed files with 84 additions and 82 deletions

View file

@ -96,7 +96,6 @@ return {
"williamboman/mason-lspconfig.nvim",
cmd = { "LspInstall", "LspUninstall" },
},
{ "WhoIsSethDaniel/mason-tool-installer.nvim" },
},
event = { "BufReadPost", "BufNewFile", "BufWritePre" },
config = function()
@ -104,7 +103,7 @@ return {
end,
keys = { { "<leader>vs", ":LspInfo<cr>", desc = "LspInfo" } },
},
-- very very pretty lsp 'peek' menus
-- pretty lsp 'peek' menus
{
"DNLHC/glance.nvim",
opts = { border = { enable = true }, theme = { enable = true, mode = "auto" } },
@ -113,85 +112,98 @@ return {
-- linting setup
{
"mfussenegger/nvim-lint",
config = function()
require("lint").linters_by_ft = linters
vim.api.nvim_create_autocmd({ "BufWritePost", "InsertLeave" }, {
callback = function()
if not vim.g.disable_autolint then
require("lint").try_lint()
end
"rshkarin/mason-nvim-lint",
dependencies = {
{
"mfussenegger/nvim-lint",
config = function()
require("lint").linters_by_ft = linters
vim.api.nvim_create_autocmd({ "BufWritePost", "InsertLeave" }, {
callback = function()
if not vim.g.disable_autolint then
require("lint").try_lint()
end
end,
})
end,
})
end,
dependencies = { "williamboman/mason.nvim" },
},
},
event = { "BufReadPost", "BufNewFile", "BufWritePre" },
opts = {},
},
-- 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 }
"zapling/mason-conform.nvim",
dependencies = {
{
"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 = formatters,
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,
formatters_by_ft = formatters,
formatters = {
-- enable python isort functionality
ruff_fix = {
prepend_args = { "--select", "I" },
cmd = { "ConformInfo" },
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",
},
},
})
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 = { "BufReadPost", "BufNewFile", "BufWritePre" },
keys = {
{
"<localleader>ll",
function()
require("conform").format({ async = true, lsp_fallback = true })
init = function()
-- If you want the formatexpr, here is the place to set it
vim.o.formatexpr = "v:lua.require'conform'.formatexpr()"
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",
},
},
init = function()
-- If you want the formatexpr, here is the place to set it
vim.o.formatexpr = "v:lua.require'conform'.formatexpr()"
end,
event = { "BufReadPost", "BufNewFile", "BufWritePre" },
opts = {},
},
-- useful quickfix-like buffer