mirror of
https://github.com/LazyVim/LazyVim.git
synced 2025-06-24 17:58:51 +02:00
feat(linting): ability to configure global and fallback linters (#1727)
* feat(linting): ability to configure global and fallback linters * fix: use nvim-lint's logic to get linters --------- Co-authored-by: Folke Lemaitre <folke.lemaitre@gmail.com>
This commit is contained in:
parent
f29543f2c9
commit
6e0e352fea
1 changed files with 25 additions and 1 deletions
|
@ -7,6 +7,10 @@ return {
|
||||||
events = { "BufWritePost", "BufReadPost", "InsertLeave" },
|
events = { "BufWritePost", "BufReadPost", "InsertLeave" },
|
||||||
linters_by_ft = {
|
linters_by_ft = {
|
||||||
fish = { "fish" },
|
fish = { "fish" },
|
||||||
|
-- Use the "*" filetype to run linters on all filetypes.
|
||||||
|
-- ['*'] = { 'global linter' },
|
||||||
|
-- Use the "_" filetype to run linters on filetypes that don't have other linters configured.
|
||||||
|
-- ['_'] = { 'fallback linter' },
|
||||||
},
|
},
|
||||||
-- LazyVim extension to easily override linter options
|
-- LazyVim extension to easily override linter options
|
||||||
-- or add custom linters.
|
-- or add custom linters.
|
||||||
|
@ -23,6 +27,8 @@ return {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
config = function(_, opts)
|
config = function(_, opts)
|
||||||
|
local Util = require("lazyvim.util")
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
local lint = require("lint")
|
local lint = require("lint")
|
||||||
|
@ -47,14 +53,32 @@ return {
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.lint()
|
function M.lint()
|
||||||
local names = lint.linters_by_ft[vim.bo.filetype] or {}
|
-- Use nvim-lint's logic first:
|
||||||
|
-- * checks if linters exist for the full filetype first
|
||||||
|
-- * otherwise will split filetype by "." and add all those linters
|
||||||
|
-- * this differs from conform.nvim which only uses the first filetype that has a formatter
|
||||||
|
local names = lint._resolve_linter_by_ft(vim.bo.filetype)
|
||||||
|
|
||||||
|
-- Add fallback linters.
|
||||||
|
if #names == 0 then
|
||||||
|
vim.list_extend(names, lint.linters_by_ft["_"] or {})
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Add global linters.
|
||||||
|
vim.list_extend(names, lint.linters_by_ft["*"] or {})
|
||||||
|
|
||||||
|
-- Filter out linters that don't exist or don't match the condition.
|
||||||
local ctx = { filename = vim.api.nvim_buf_get_name(0) }
|
local ctx = { filename = vim.api.nvim_buf_get_name(0) }
|
||||||
ctx.dirname = vim.fn.fnamemodify(ctx.filename, ":h")
|
ctx.dirname = vim.fn.fnamemodify(ctx.filename, ":h")
|
||||||
names = vim.tbl_filter(function(name)
|
names = vim.tbl_filter(function(name)
|
||||||
local linter = lint.linters[name]
|
local linter = lint.linters[name]
|
||||||
|
if not linter then
|
||||||
|
Util.warn("Linter not found: " .. name, { title = "nvim-lint" })
|
||||||
|
end
|
||||||
return linter and not (type(linter) == "table" and linter.condition and not linter.condition(ctx))
|
return linter and not (type(linter) == "table" and linter.condition and not linter.condition(ctx))
|
||||||
end, names)
|
end, names)
|
||||||
|
|
||||||
|
-- Run linters.
|
||||||
if #names > 0 then
|
if #names > 0 then
|
||||||
lint.try_lint(names)
|
lint.try_lint(names)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue