Skip to content

Instantly share code, notes, and snippets.

@artemave
Last active April 23, 2022 10:47
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 artemave/078b1dcc7c21c62403394979260954ca to your computer and use it in GitHub Desktop.
Save artemave/078b1dcc7c21c62403394979260954ca to your computer and use it in GitHub Desktop.
Insert `rubocop:disable`/`rubocop:enable` comments
RD = RD or {}
function RD.join(tbl, sep)
local ret = ''
for i, v in pairs(tbl) do
ret = ret .. v .. sep
end
return ret:sub(1, -#sep - 1)
end
function RD.map(tbl, f)
local t = {}
for k,v in pairs(tbl) do
t[k] = f(v)
end
return t
end
function RD.rubocop_disable()
local current_lnum = vim.api.nvim_win_get_cursor(0)[1]
local current_line = vim.api.nvim_get_current_line()
local diagnostics = vim.diagnostic.get(0, { lnum = current_lnum - 1 })
local diagnostic = diagnostics[1]
if diagnostic and diagnostic.source == 'rubocop' then
local code = RD.join(RD.map(diagnostics, function(d) return d.code end), ', ')
if diagnostic.lnum == diagnostic.end_lnum then
local new_text = current_line .. ' # rubocop:disable ' .. code
vim.api.nvim_set_current_line(new_text)
else
local indent = tonumber(vim.call('indent', current_lnum))
local padding = string.rep(' ', indent)
local enable_text = padding .. '# rubocop:enable ' .. code
vim.api.nvim_buf_set_lines(0, diagnostic.end_lnum + 1, diagnostic.end_lnum + 1, false, {enable_text})
local disable_text = padding .. '# rubocop:disable ' .. code
vim.api.nvim_buf_set_lines(0, diagnostic.lnum, diagnostic.lnum, false, {disable_text})
end
end
end
vim.api.nvim_set_keymap('n', '<space>x', '<cmd>lua RD.rubocop_disable()<CR>', {})
@artemave
Copy link
Author

This works when rubocop messages are coming via https://github.com/jose-elias-alvarez/null-ls.nvim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment