Skip to content

Commit

Permalink
improv: filter unsupported sources when ignoring rules
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgrieser committed Sep 21, 2023
1 parent 54241c5 commit 50a5953
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 41 deletions.
12 changes: 6 additions & 6 deletions lua/rule-breaker/ignoreRuleData.lua
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
---@class ruleIgnoreConfig
---@field comment string|string[] with %s for the rule id
---@field type "sameLine"|"nextLine"|"enclose"
---@field location "sameLine"|"nextLine"|"encloseLine"

---@type table<string, ruleIgnoreConfig>
local data = {
shellcheck = {
comment = "# shellcheck disable=%s",
type = "nextLine",
location = "nextLine",
},
selene = {
comment = "-- selene: allow(%s)",
type = "nextLine",
location = "nextLine",
},
vale = {
comment = { "<!-- vale %s = NO -->", "<!-- vale %s = YES -->" },
type = "enclose",
location = "encloseLine",
},
yamllint = {
comment = "# yamllint disable-line rule:%s",
type = "nextLine",
location = "nextLine",
},
stylelint = {
comment = "/* stylelint-disable-next-line %s */",
type = "nextLine",
location = "nextLine",
},
}

Expand Down
78 changes: 43 additions & 35 deletions lua/rule-breaker/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,6 @@ local function notify(msg, level)
vim.notify(msg, vim.log.levels[level:upper()], { title = pluginName })
end

---Selects a rule in the current line. If one rule, automatically selects it
---@param operation function(diag)
local function selectRuleInCurrentLine(operation)
local lnum = vim.api.nvim_win_get_cursor(0)[1] - 1
local curLineDiags = vim.diagnostic.get(0, { lnum = lnum })
if #curLineDiags == 0 then
notify("No diagnostics found", "warn")
elseif #curLineDiags == 1 then
operation(curLineDiags[1])
else
vim.ui.select(curLineDiags, {
prompt = "Select Rule:",
format_item = function(diag) return diag.message end,
}, function(diag)
if not diag then return end -- aborted input
operation(diag)
end)
end
end

---@param diag diagnostic
---@return boolean whether rule is valid
local function validDiagObj(diag)
Expand Down Expand Up @@ -81,14 +61,6 @@ end
---@param diag diagnostic
local function addIgnoreComment(diag)
if not validDiagObj(diag) then return end
if not ignoreRuleData[diag.source] then
notify(
("There is no ignore rule configuration for %s %s."):format(diag.source, diag.code)
.. "\nPlease make a PR to add support for it.",
"warn"
)
return
end

-- add rule id and indentation into comment
local currentIndent = vim.api.nvim_get_current_line():match("^%s*")
Expand All @@ -98,20 +70,56 @@ local function addIgnoreComment(diag)
ignoreComment[i] = currentIndent .. ignoreComment[i]:format(diag.code)
end

-- add comment
local ignoreType = ignoreRuleData[diag.source].type
if ignoreType == "nextLine" then
-- insert the comment
local ignoreLocation = ignoreRuleData[diag.source].location
if ignoreLocation == "nextLine" then
local prevLineNum = vim.api.nvim_win_get_cursor(0)[1] - 1
vim.api.nvim_buf_set_lines(0, prevLineNum, prevLineNum, false, ignoreComment)
elseif ignoreType == "sameLine" then
elseif ignoreLocation == "sameLine" then
local currentLine = vim.api.nvim_get_current_line():gsub("%s+$", "")
vim.api.nvim_set_current_line(currentLine .. " " .. ignoreComment[1])
elseif ignoreType == "enclose" then
elseif ignoreLocation == "encloseLine" then
local prevLineNum = vim.api.nvim_win_get_cursor(0)[1] - 1
local nextLineNum = vim.api.nvim_win_get_cursor(0)[1]
vim.api.nvim_buf_set_lines(0, nextLineNum, nextLineNum, false, {ignoreComment[2]})
vim.api.nvim_buf_set_lines(0, prevLineNum, prevLineNum, false, {ignoreComment[1]})
vim.api.nvim_buf_set_lines(0, nextLineNum, nextLineNum, false, { ignoreComment[2] })
vim.api.nvim_buf_set_lines(0, prevLineNum, prevLineNum, false, { ignoreComment[1] })
end
end

--------------------------------------------------------------------------------

---Selects a rule in the current line. If one rule, automatically selects it
---@param operation function(diag)
local function selectRuleInCurrentLine(operation)
local lnum = vim.api.nvim_win_get_cursor(0)[1] - 1
local curLineDiags = vim.diagnostic.get(0, { lnum = lnum })

-- filter diagnostics for which there are no ignore comments defined
if operation == addIgnoreComment then
curLineDiags = vim.tbl_filter(
function(diag) return ignoreRuleData[diag.source] ~= nil end,
curLineDiags
)
end

-- one or zero diagnostics
if #curLineDiags == 0 then
notify("No supported diagnostics found in current line.", "warn")
return
elseif #curLineDiags == 1 then
operation(curLineDiags[1])
return
end

-- select from multiple diagnostics
local title = operation == addIgnoreComment and "Ignore Rule:" or "Lookup Rule:"
vim.ui.select(curLineDiags, {
prompt = title,
format_item = function(diag) return diag.source .. ": " .. diag.code end,
}, function(diag)
if not diag then return end
operation(diag)
end)
end

--------------------------------------------------------------------------------
Expand Down

0 comments on commit 50a5953

Please sign in to comment.