Skip to content

Commit

Permalink
fix: properly fix generic linters (#138)
Browse files Browse the repository at this point in the history
* fix: generic linters

* docs: add instructions for setting generic linters
  • Loading branch information
xiaoshihou514 authored Jan 13, 2024
1 parent 383883a commit d478286
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ Register multiple filetypes to a single linter or formatter:
ft('typescript,javascript,typescriptreact'):fmt('prettier')
```

Lint all your files with `codespell`

```lua
-- NB: this does not work with formatters
ft('*'):lint('codespell')
```

### Custom Configuration

Easily setup your custom tool if not in the defaults or you do not want guard-collection bundled:
Expand Down
7 changes: 7 additions & 0 deletions doc/guard.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ Register multiple filetypes to a single linter or formatter:
ft('typescript,javascript,typescriptreact'):fmt('prettier')
<

Lint all your files with `codespell`

>lua
-- NB: this does not work with formatters
ft('*'):lint('codespell')
<


CUSTOM CONFIGURATION ~

Expand Down
26 changes: 20 additions & 6 deletions lua/guard/lint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,31 @@ local group = require('guard.events').group

local function do_lint(buf)
buf = buf or api.nvim_get_current_buf()
local buf_config = ft_handler[vim.bo[buf].filetype] or ft_handler['*']
if not buf_config then
return
local linters, generic_linters

local generic_config = ft_handler['*']
local buf_config = ft_handler[vim.bo[buf].filetype]

if generic_config and generic_config.linter then
generic_linters = generic_config.linter
end

if not buf_config or not buf_config.linter then
-- pre: do_lint only triggers inside autocmds, which ensures generic_config and buf_config are not *both* nil
linters = generic_linters
else
-- buf_config exists, we want both
linters = vim.deepcopy(buf_config.linter)
if generic_linters then
vim.list_extend(linters, generic_linters)
end
end
local linters = buf_config.linter
local fname = vim.fn.fnameescape(api.nvim_buf_get_name(buf))
local prev_lines = get_prev_lines(buf, 0, -1)
vd.reset(ns, buf)

coroutine.resume(coroutine.create(function()
local results
local results = {}

for _, lint in ipairs(linters) do
lint = vim.deepcopy(lint)
Expand All @@ -29,7 +43,7 @@ local function do_lint(buf)
lint.lines = prev_lines
local data = spawn(lint)
if #data > 0 then
results = lint.parse(data, buf)
vim.list_extend(results, lint.parse(data, buf))
end
end

Expand Down

0 comments on commit d478286

Please sign in to comment.