♻️ refactor(lint): improve linting execution and filetype handling

This commit is contained in:
sankalp committed 2025-07-25 15:00:02 +05:45
1 parent 41110a74c8
commit 8870bf6f99
1 file changed
+9 -12
+9 -12
View File
@@ -1,6 +1,5 @@
local lint = require('lint')
-- Filetype to linters mapping
lint.linters_by_ft = {
javascript = { 'eslint_d' },
typescript = { 'eslint_d' },
@@ -14,35 +13,33 @@ lint.linters_by_ft = {
yaml = { 'actionlint' },
}
local function lint(bufnr)
local linters =
vim.list_extend({ 'codespell', 'editorconfig-checker' }, require('lint')._resolve_linter_by_ft(vim.bo.filetype))
local function run_lint(bufnr)
bufnr = bufnr or 0
local ft = vim.bo[bufnr].filetype
local ctx = { filename = vim.api.nvim_buf_get_name(bufnr or 0) }
local linters = vim.list_extend({ 'codespell', 'editorconfig-checker' }, require('lint')._resolve_linter_by_ft(ft))
local ctx = { filename = vim.api.nvim_buf_get_name(bufnr) }
ctx.dirname = vim.fn.fnamemodify(ctx.filename, ':h')
linters = vim.tbl_filter(function(name)
local linter = require('lint').linters[name]
local linter = lint.linters[name]
return vim.fn.executable(linter.cmd) == 1
---@diagnostic disable-next-line: undefined-field
and not (type(linter) == 'table' and linter.condition and not linter.condition(ctx))
end, linters)
if #linters > 0 then
require('lint').try_lint(linters)
lint.try_lint(linters)
end
end
lint()
local timer = assert(vim.uv.new_timer())
vim.api.nvim_create_autocmd({ 'BufReadPost', 'BufWritePost', 'InsertLeave' }, {
callback = function(args)
timer:start(100, 0, function()
timer:stop()
vim.schedule(function()
lint(args.buf)
run_lint(args.buf)
end)
end)
end,