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

@ -42,8 +42,9 @@
"lualine.nvim": { "branch": "master", "commit": "0a5a66803c7407767b799067986b4dc3036e1983" }, "lualine.nvim": { "branch": "master", "commit": "0a5a66803c7407767b799067986b4dc3036e1983" },
"luarocks.nvim": { "branch": "main", "commit": "1db9093915eb16ba2473cfb8d343ace5ee04130a" }, "luarocks.nvim": { "branch": "main", "commit": "1db9093915eb16ba2473cfb8d343ace5ee04130a" },
"markmap.nvim": { "branch": "main", "commit": "5fb6755cf5434511cc23a4936c9eb76b9142fba5" }, "markmap.nvim": { "branch": "main", "commit": "5fb6755cf5434511cc23a4936c9eb76b9142fba5" },
"mason-conform.nvim": { "branch": "main", "commit": "abce2be529f3b4b336c56d0ba6336a9144e0fee6" },
"mason-lspconfig.nvim": { "branch": "main", "commit": "9ae570e206360e47d30b4c35a4550c165f4ea7b7" }, "mason-lspconfig.nvim": { "branch": "main", "commit": "9ae570e206360e47d30b4c35a4550c165f4ea7b7" },
"mason-tool-installer.nvim": { "branch": "main", "commit": "c5e07b8ff54187716334d585db34282e46fa2932" }, "mason-nvim-lint": { "branch": "main", "commit": "637a5b8f1b454753ec70289c4996d88a50808642" },
"mason.nvim": { "branch": "main", "commit": "c43eeb5614a09dc17c03a7fb49de2e05de203924" }, "mason.nvim": { "branch": "main", "commit": "c43eeb5614a09dc17c03a7fb49de2e05de203924" },
"mdeval.nvim": { "branch": "master", "commit": "2c32e2f3e7d8f222e7a4724989f218d036e1081d" }, "mdeval.nvim": { "branch": "master", "commit": "2c32e2f3e7d8f222e7a4724989f218d036e1081d" },
"mini.nvim": { "branch": "main", "commit": "19e1584124cda35388d4fdb911eab7124014e541" }, "mini.nvim": { "branch": "main", "commit": "19e1584124cda35388d4fdb911eab7124014e541" },

View file

@ -43,17 +43,6 @@ local servers = {
yamlls = {}, yamlls = {},
} }
-- TODO installed for conform/nvim-lint so should be sourced from there not here
local to_mason =
---@diagnostic disable-next-line:deprecated
{ "stylua", "shellcheck", "shfmt", "markdownlint", "bibtex-tidy", "jq", "prettier", "ruff", unpack(servers) }
require("mason-tool-installer").setup({
-- a list of all tools you want to ensure are installed upon
-- start
ensure_installed = to_mason,
start_delay = 3000,
})
local function on_attach(_, bufnr) local function on_attach(_, bufnr)
local map = vim.keymap.set local map = vim.keymap.set
map("n", "[d", "<cmd>lua vim.diagnostic.goto_prev()<cr>", { buffer = bufnr, desc = "Previous diagnostic" }) map("n", "[d", "<cmd>lua vim.diagnostic.goto_prev()<cr>", { buffer = bufnr, desc = "Previous diagnostic" })
@ -177,7 +166,7 @@ lspconfig.basedpyright.setup({
client.config.settings.python.pythonPath = python_path client.config.settings.python.pythonPath = python_path
end, end,
settings = { settings = {
-- disable imports and linting since, we use ruff for that -- disable imports and linting since, we use ruff for that
pyright = { pyright = {
disableOrganizeImports = true, disableOrganizeImports = true,
}, },

View file

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