Last active
April 23, 2022 10:47
-
-
Save artemave/078b1dcc7c21c62403394979260954ca to your computer and use it in GitHub Desktop.
Insert `rubocop:disable`/`rubocop:enable` comments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>', {}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This works when rubocop messages are coming via https://github.com/jose-elias-alvarez/null-ls.nvim