Skip to content

Commit

Permalink
Fixes #1 (configurable code field)
Browse files Browse the repository at this point in the history
  • Loading branch information
amadanmath committed May 4, 2024
1 parent d7bb3e5 commit cdf2236
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,17 @@ The default options are:
The `ignores` option lists the supported filetypes. Each value is a table:

```lua
[filetype] = { type, prefix, suffix, joiner }
[filetype] = { type, prefix, suffix, joiner, [options] }
```

If `suffix` is `nil`, the default is `''` (empty string).
If `joiner` is `nil`, the default is `', '` (comma and space).

Optional key-value fields can be specified, too. Currently only one is
supported: `field`. By default, `field = 'code'`, which means the contents
of `diagnostic.code` will be used in the annotation. Some diagnostic
sources use a different field (e.g. `field = 'source'` may be necessary).

If `type` is `'endline'`, the annotation will be appended to the current line.
For example, in Python, invoking `diag_ignore.nvim` on

Expand Down
7 changes: 6 additions & 1 deletion doc/diag_ignore.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,17 @@ The default options are:
The `ignores` option lists the supported filetypes. Each value is a table:

>lua
[filetype] = { type, prefix, suffix, joiner }
[filetype] = { type, prefix, suffix, joiner, [options] }
<

If `suffix` is `nil`, the default is `''` (empty string).
If `joiner` is `nil`, the default is `', '` (comma and space).

Optional key-value fields can be specified, too. Currently only one is
supported: `field`. By default, `field = 'code'`, which means the contents
of `diagnostic.code` will be used in the annotation. Some diagnostic
sources use a different field (e.g. `field = 'source'` may be necessary).

If `type` is `'endline'`, the annotation will be appended to the current line.
For example, in Python, invoking `diag_ignore.nvim` on

Expand Down
22 changes: 18 additions & 4 deletions lua/diag_ignore/init.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
---@class diag_ignore.UserConfig
---@field ignores table<string, string[]>
---@field ignores table<string, diag_ignore.IgnoreSpec> | nil

---@class diag_ignore.IgnoreSpec
---@field [1] string
---@field [2] string
---@field [3] string | nil
---@field [4] string | nil
---@field field string | nil
---@field nojoin boolean | nil

local M = {}

Expand All @@ -19,13 +27,15 @@ M.diag_ignore = function()

local lnum = vim.api.nvim_win_get_cursor(0)[1] - 1
local diags = vim.diagnostic.get(0, { lnum = lnum })
vim.print(diags)
local field = ignore.field or 'code'
if diags then
vim.ui.select(
diags,
{
prompt = "Ignore diagnostic:",
format_item = function(diag)
local codestr = diag.code and (" [" .. diag.code .. "]") or ""
local codestr = diag[field] and (" [" .. diag[field] .. "]") or ""
return diag.message .. codestr
end,
},
Expand Down Expand Up @@ -77,7 +87,7 @@ M.diag_ignore = function()
end
local ignorestr = string.sub(line, pto + 1, sfrom - 1)
local types = ignorestr == "" and {} or vim.split(ignorestr, joiner)
table.insert(types, choice.code)
table.insert(types, choice[field])
ignorestr = table.concat(types, joiner)
vim.api.nvim_buf_set_text(0, lnum, pto, lnum, sfrom - 1, { ignorestr })
end
Expand All @@ -87,6 +97,7 @@ end

local function is_type_valid(spec)
return spec == 'prevline'
or spec == 'prevlines'
or spec == 'endline'
end

Expand All @@ -101,10 +112,13 @@ M.setup = function(user_config)
local validators = {}
for ft, ignore in pairs(M.config.ignores) do
if ignore then
validators[('ignores.%s[1] (type)'):format(ft)] = { ignore[1], is_type_valid, '"prevline" or "endline"' }
validators[('ignores.%s[1] (type)'):format(ft)] = {
ignore[1], is_type_valid, '"prevline", "prevlines" or "endline"'
}
validators[('ignores.%s[2] (prefix)'):format(ft)] = { ignore[2], 'string' }
validators[('ignores.%s[3] (suffix)'):format(ft)] = { ignore[3], { 'string', 'nil' } }
validators[('ignores.%s[4] (joiner)'):format(ft)] = { ignore[4], { 'string', 'nil' } }
validators[('ignores.%s.field'):format(ft)] = { ignore.field, { 'string', 'nil' } }
end
end
vim.validate(validators)
Expand Down

0 comments on commit cdf2236

Please sign in to comment.