| skipped 26 lines |
27 | 27 | | or linter |
28 | 28 | | end |
29 | 29 | | |
30 | | - | local function try_lint() |
31 | | - | local names = lint._resolve_linter_by_ft(vim.bo.filetype) |
32 | | - | |
33 | | - | -- Add fallback linters and global linters. |
34 | | - | if #names == 0 then names = lint.linters_by_ft["_"] or {} end |
35 | | - | vim.list_extend(names, lint.linters_by_ft["*"] or {}) |
36 | | - | |
37 | | - | -- Filter out linters that don't exist or don't match the condition. |
38 | | - | local ctx = { filename = vim.api.nvim_buf_get_name(0) } |
39 | | - | ctx.dirname = vim.fn.fnamemodify(ctx.filename, ":h") |
40 | | - | names = vim.tbl_filter(function(name) |
| 30 | + | local valid_linters = function(ctx, linters) |
| 31 | + | if not linters then return {} end |
| 32 | + | return vim.tbl_filter(function(name) |
41 | 33 | | local linter = lint.linters[name] |
42 | 34 | | return linter |
43 | 35 | | and vim.fn.executable(linter.cmd) == 1 |
44 | 36 | | and not (type(linter) == "table" and linter.condition and not linter.condition(ctx)) |
45 | | - | end, names) |
| 37 | + | end, linters) |
| 38 | + | end |
46 | 39 | | |
47 | | - | lint.try_lint(names) |
| 40 | + | local orig_resolve_linter_by_ft = lint._resolve_linter_by_ft |
| 41 | + | lint._resolve_linter_by_ft = function(...) |
| 42 | + | local ctx = { filename = vim.api.nvim_buf_get_name(0) } |
| 43 | + | ctx.dirname = vim.fn.fnamemodify(ctx.filename, ":h") |
| 44 | + | |
| 45 | + | local linters = valid_linters(ctx, orig_resolve_linter_by_ft(...)) |
| 46 | + | if not linters[1] then linters = valid_linters(ctx, lint.linters_by_ft["_"]) end -- fallback |
| 47 | + | require("astrocore").list_insert_unique(linters, valid_linters(ctx, lint.linters_by_ft["*"])) -- global |
| 48 | + | |
| 49 | + | return linters |
48 | 50 | | end |
49 | 51 | | |
50 | | - | try_lint() -- start linter immediately |
| 52 | + | lint.try_lint() -- start linter immediately |
51 | 53 | | local timer = vim.loop.new_timer() |
52 | 54 | | vim.api.nvim_create_autocmd({ "BufWritePost", "BufReadPost", "InsertLeave", "TextChanged" }, { |
53 | 55 | | group = vim.api.nvim_create_augroup("auto_lint", { clear = true }), |
| skipped 1 lines |
55 | 57 | | callback = function() |
56 | 58 | | timer:start(100, 0, function() |
57 | 59 | | timer:stop() |
58 | | - | vim.schedule(try_lint) |
| 60 | + | vim.schedule(lint.try_lint) |
59 | 61 | | end) |
60 | 62 | | end, |
61 | 63 | | }) |
| skipped 3 lines |