Automatically register linters and formatters installed in mason.nvim
mason-bridge.nvim
mason-bridge automatically registers linters and formatters installed using mason.nvim in conform.nvim and nvim-lint.
Introduction
- Automatically generates the
formattersbyftandlintersbyfttables to be used in their respective plugins - Provides and override in case the tool name and linter name mismatch e.g. snyk and
snykiac
Requirements
- neovim
>= 0.7.0 mason.nvimconform.nvimnvim-lint
Installation
Packer
use {
"williamboman/mason.nvim",
"frostplexx/mason-bridge.nvim",
"stevearc/conform.nvim",
"mfussenegger/nvim-lint"
}
lazy.nvim
{
"williamboman/mason.nvim",
"frostplexx/mason-bridge.nvim",
"stevearc/conform.nvim",
"mfussenegger/nvim-lint"
}
Setup
It's important that you set up the plugins in the following order:
mason.nvimmason-bridgeconform.nvimand/ornvim-lint(this order doesnt matter)
require("mason").setup()
require("mason-bridge").setup()
-- After setting up mason-bridge you may set up conform.nvim and nvim-lint require("conform").setup({ formattersbyft = require("mason-bridge").get_formatters(), })
require("lint").lintersbyft = require("mason-bridge").get_linters() -- ...
Have Tools Register for Every Filetype
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.resolvelinterbyft(vim.bo.filetype) names = vim.list_extend({}, names) vim.listextend(names, lint.lintersby_ft["*"] or {})
-- try_lint() can be called in an autocommand as described in the nvim-lint README lint.try_lint(names)
Dynamically Load Linters and Formatters
Linters
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.nvimcreateautocmd({ "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, })
Formatters
We ask for the currently installed formatters in a similar way to how we ask for linters. To achieve this we need to turn formatonsave into a function that re-sets the formattersbyft for conform.nvim like shown in the example below.
local bridge = require("mason-bridge")
require("conform").setup({
formattersbyft = bridge.get_formatters(),
formatonsave = function(bufnr)
require("conform").formattersbyft = bridge.get_formatters()
return { timeoutms = 200, lspfallback = true }, on_format
end,
})
Refer to the Configuration section for information about which settings are available.
Configuration
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 = {
mylanguage = {"formatter1", "formatter_2"}
}
}
})
Default configuration
local DEFAULT_SETTINGS = {
-- A table of filetypes with the respective tool (or tools) to be used
overrides = {
formatters = {},
linters = {}
}
}
Roadmap
- [ ] Add a handler system similar to
mason-lspconfig.nvimandmason-nvim-dap.nvim - [ ] Find i better way of handling language to filetype mappings
- [x] Add vim help file