fix: use nvim-lint's logic to get linters

This commit is contained in:
Folke Lemaitre 2023-10-15 07:16:57 +02:00
parent 64d3b30914
commit 815c36715e
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040

View file

@ -27,6 +27,8 @@ return {
},
},
config = function(_, opts)
local Util = require("lazyvim.util")
local M = {}
local lint = require("lint")
@ -51,18 +53,32 @@ return {
end
function M.lint()
local names = vim.tbl_flatten({ lint.linters_by_ft[vim.bo.filetype], lint.linters_by_ft["*"] }) 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
names = lint.linters_by_ft["_"] or {}
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) }
ctx.dirname = vim.fn.fnamemodify(ctx.filename, ":h")
names = vim.tbl_filter(function(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))
end, names)
-- Run linters.
if #names > 0 then
lint.try_lint(names)
end