Skip to content

Instantly share code, notes, and snippets.

@b0o
Created December 1, 2021 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b0o/35dc5d8d9729393e61ca11d2831fdab6 to your computer and use it in GitHub Desktop.
Save b0o/35dc5d8d9729393e61ca11d2831fdab6 to your computer and use it in GitHub Desktop.
local shebang_regex = vim.regex([[^#!]])
local blank_or_comment_line_regex = vim.regex([[^\s*\(#.*\)\?$]])
local shellcheck_disable_regex = vim.regex([[^\s*#\s*shellcheck\s\+disable=\(\(SC\)\?\d\+\)\(,\(SC\)\?\d\+\)*\s*$]])
local shellcheck_disable_pattern = "^%s*#%s*shellcheck%s+disable=([^%s]*)%s*$"
-- Searches a region of the buffer `bufnr` for a ShellCheck disable directive.
-- The search proceeds from `row_start` to `row_end`.
-- If `row_start` is greater than `row_end`, the region is effectively searched in reverse.
-- A table representing first directive that's found is returned, otherwise nil.
local find_disable_directive = function(bufnr, row_start, row_end)
local region_start, region_end
local idx_start, idx_end, step
if row_start > row_end then
region_start = row_end
region_end = row_start
idx_start = row_start - row_end
idx_end = 1
step = -1
else
region_start = row_start
region_end = row_end
idx_start = 1
idx_end = row_end - row_start
step = 1
end
local lines = vim.api.nvim_buf_get_lines(bufnr, region_start, region_end, false)
for i = idx_start, idx_end, step do
local line = lines[i]
if line == nil then
return
end
if shellcheck_disable_regex:match_str(line) then
return {
codes = line:match(shellcheck_disable_pattern),
row = i + (step == 1 and row_start or row_end),
}
end
if not blank_or_comment_line_regex:match_str(line) then
return
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment