mason-bridge
automatically registers linters and formatters installed using
mason.nvim
in conform.nvim
and nvim-lint
.
- Automatically generates the
formatters_by_ft
andlinters_by_ft
tables to be used in their respective plugins - Provides and override in case the tool name and linter name mismatch e.g. snyk and
snyk_iac
- neovim
>= 0.7.0
mason.nvim
conform.nvim
nvim-lint
use {
"williamboman/mason.nvim",
"frostplexx/mason-bridge.nvim",
"stevearc/conform.nvim",
"mfussenegger/nvim-lint"
}
{
"williamboman/mason.nvim",
"frostplexx/mason-bridge.nvim",
"stevearc/conform.nvim",
"mfussenegger/nvim-lint"
}
It's important that you set up the plugins in the following order:
mason.nvim
mason-bridge
conform.nvim
and/ornvim-lint
(this order doesnt matter)
Pay extra attention to this if you lazy-load plugins, or somehow "chain" the loading of plugins via your plugin manager.
require("mason").setup()
require("mason-bridge").setup()
-- After setting up mason-bridge you may set up conform.nvim and nvim-lint
require("conform").setup({
formatters_by_ft = require("mason-bridge").get_formatters(),
})
require("lint").linters_by_ft = require("mason-bridge").get_linters()
-- ...
Some tools like for example codespell
do not have a language specified because they are to be used on every filetype / language.
mason-bridge
returns these tools with a *
as the placeholder for the language.
To correctly register correctly in nvim-lint you need to modify your nvim-lint
config like this:
local names = lint._resolve_linter_by_ft(vim.bo.filetype)
names = vim.list_extend({}, names)
vim.list_extend(names, lint.linters_by_ft["*"] or {})
-- try_lint() can be called in an autocommand as described in the nvim-lint README
lint.try_lint(names)
You can have nvim-lint
dynamically load linters so you dont have to restart nvim after installing or uninstalling a tool. To achive this you need to update the autcommand from the nvim-lint README like this:
local bridge = require("mason-bridge")
vim.api.nvim_create_autocmd({ "BufWritePost" },{
callback = function()
-- get the linters from mason
local linters = bridge.get_linters()
-- filter for linters for the current filetype
local names = linters[vim.bo.filetype] or {}
-- Create a copy of the names table to avoid modifying the original.
names = vim.list_extend({}, names)
-- insert the linters that have ["*"] as filetype into the names table
vim.list_extend(names, linters["*"])
-- apply those linters to the current buffer
lint.try_lint(names)
end,
})
We ask for the currently installed formatters in a similar way to how we ask for linters. To achieve this we need to turn format_on_save
into a function that re-sets the formatters_by_ft
for conform.nvim
like shown in the example below.
local bridge = require("mason-bridge")
require("conform").setup({
formatters_by_ft = bridge.get_formatters(),
format_on_save = function(bufnr)
require("conform").formatters_by_ft = bridge.get_formatters()
return { timeout_ms = 200, lsp_fallback = true }, on_format
end,
})
Refer to the Configuration section for information about which settings are available.
You may optionally configure certain behavior of mason-bridge.nvim
when calling the .setup()
function. Refer to
the default configuration for a list of all available settings.
Example:
require("mason-bridge").setup({
overrides = {
linters = {
javascript = { "snyk_iac" },
docker = { "snyk_iac" }
},
formatters = {
my_language = {"formatter_1", "formatter_2"}
}
}
})
local DEFAULT_SETTINGS = {
-- A table of filetypes with the respective tool (or tools) to be used
overrides = {
formatters = {},
linters = {}
}
}
- Add a handler system similar to
mason-lspconfig.nvim
andmason-nvim-dap.nvim
- Find i better way of handling language to filetype mappings
- Add vim help file