Skip to content

Commit c4fbfd8

Browse files
committed
Allow requesting try_lint() to only run linters supporting stdin
When hooking linting to an event like InsertLeave, running linters that only work with content that already made to the storage does not make sense. Enhance try_lint() to accept an optional boolean flag stdin_only in its set of options. When this flag is present and set to "true" try_lint() will skip all linters that do not advertise stdin support (via linter.stdin).
1 parent bcd1a44 commit c4fbfd8

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

doc/lint.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ M.try_lint({names?}, {opts?}) *lint.try_lint*
3232

3333

3434
Parameters: ~
35-
{names?} (string|string[]) name of the linter
36-
{opts?} ({cwd?:string,ignore_errors?:boolean}) options
35+
{names?} (string|string[]) name of the linter
36+
{opts?} ({cwd?:string,ignore_errors?:boolean,stdin_only?:boolean}) options
3737

3838

3939
M.get_namespace({name}) *lint.get_namespace*

lua/lint.lua

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ M.linters_by_ft = {
5454
--- If no names are given, it runs the linters configured in `linters_by_ft`
5555
---
5656
---@param names? string|string[] name of the linter
57-
---@param opts? {cwd?: string, ignore_errors?: boolean} options
57+
---@param opts? {cwd?: string, ignore_errors?: boolean, stdin_only?: boolean} options
5858
function M.try_lint(names, opts)
5959
assert(
6060
vim.diagnostic,
@@ -82,16 +82,21 @@ function M.try_lint(names, opts)
8282
local running_procs = running_procs_by_buf[bufnr] or {}
8383
for _, linter_name in pairs(names) do
8484
local linter = lookup_linter(linter_name)
85-
local proc = running_procs[linter.name]
86-
if proc then
87-
proc:cancel()
88-
end
89-
running_procs[linter.name] = nil
90-
local ok, lintproc_or_error = pcall(M.lint, linter, opts)
91-
if ok then
92-
running_procs[linter.name] = lintproc_or_error
93-
elseif not opts.ignore_errors then
94-
notify(lintproc_or_error --[[@as string]], vim.log.levels.WARN)
85+
if not opts.stdin_only or linter.stdin then
86+
local proc = running_procs[linter.name]
87+
if proc then
88+
proc:cancel()
89+
end
90+
running_procs[linter.name] = nil
91+
local ok, lintproc_or_error = pcall(M.lint, linter, {
92+
cwd = opts.cwd,
93+
ignore_errors = opts.ignore_errors,
94+
})
95+
if ok then
96+
running_procs[linter.name] = lintproc_or_error
97+
elseif not opts.ignore_errors then
98+
notify(lintproc_or_error --[[@as string]], vim.log.levels.WARN)
99+
end
95100
end
96101
end
97102
running_procs_by_buf[bufnr] = running_procs

0 commit comments

Comments
 (0)