Skip to content

Commit

Permalink
refactor(diagnostic): Remove arguments for explainError
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Running :rustLsp explainError will now only explain
error on current line, users are suggested to use
vim.diagnostic.goto_prev() and vim.diagnostic.goto_next() for
navigation, as stated in issue
mrcjkb#603 and
mrcjkb#599
  • Loading branch information
rywng committed Dec 5, 2024
1 parent a5e00d7 commit e6d8580
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 119 deletions.
105 changes: 0 additions & 105 deletions lua/rustaceanvim/commands/diagnostic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,111 +54,6 @@ local function set_close_keymaps(bufnr)
vim.keymap.set('n', '<Esc>', close_hover, { buffer = bufnr, noremap = true, silent = true })
end

function M.explain_error()
if vim.fn.executable(rustc) ~= 1 then
vim.notify('rustc is needed to explain errors.', vim.log.levels.ERROR)
return
end

local diagnostics = vim
.iter(vim.diagnostic.get(0, {}))
---@param diagnostic vim.Diagnostic
:filter(function(diagnostic)
return diagnostic.code ~= nil
and diagnostic.source == 'rustc'
and diagnostic.severity == vim.diagnostic.severity.ERROR
end)
:totable()
if #diagnostics == 0 then
vim.notify('No explainable errors found.', vim.log.levels.INFO)
return
end
local win_id = vim.api.nvim_get_current_win()
local opts = {
cursor_position = vim.api.nvim_win_get_cursor(win_id),
severity = vim.diagnostic.severity.ERROR,
wrap = true,
}
local found = false
local diagnostic
local pos_map = {}
---@type string
local pos_id = '0'
repeat
diagnostic = vim.diagnostic.get_next(opts)
pos_map[pos_id] = diagnostic
if diagnostic == nil then
break
end
found = diagnostic.code ~= nil and diagnostic.source == 'rustc'
local pos = { diagnostic.lnum, diagnostic.col }
-- check if there is an explainable error at the same location
if not found then
local cursor_diagnostics = vim.tbl_filter(function(diag)
return pos[1] == diag.lnum and pos[2] == diag.col
end, diagnostics)
if #cursor_diagnostics ~= 0 then
diagnostic = cursor_diagnostics[1]
found = true
break
end
end
pos_id = vim.inspect(pos)
-- diagnostics are (0,0)-indexed but cursors are (1,0)-indexed
opts.cursor_position = { pos[1] + 1, pos[2] }
local searched_all = pos_map[pos_id] ~= nil
until diagnostic == nil or found or searched_all
if not found then
-- Fall back to first diagnostic
diagnostic = diagnostics[1]
local pos = { diagnostic.lnum, diagnostic.col }
opts.cursor_position = pos
return
end

---@param sc vim.SystemCompleted
local function handler(sc)
if sc.code ~= 0 or not sc.stdout then
vim.notify('Error calling rustc --explain' .. (sc.stderr and ': ' .. sc.stderr or ''), vim.log.levels.ERROR)
return
end
local output = sc.stdout:gsub('```', '```rust', 1)
local markdown_lines = vim.lsp.util.convert_input_to_markdown_lines(output, {})
local float_preview_lines = vim.deepcopy(markdown_lines)
table.insert(float_preview_lines, 1, '---')
table.insert(float_preview_lines, 1, '1. Open in split')
vim.schedule(function()
local bufnr, winnr = vim.lsp.util.open_floating_preview(
float_preview_lines,
'markdown',
vim.tbl_extend('keep', config.tools.float_win_config, {
focus_id = 'rustc-explain-error',
close_events = { 'CursorMoved', 'BufHidden', 'InsertCharPre' },
})
)
_window_state.float_winnr = winnr
set_close_keymaps(bufnr)
set_split_open_keymap(bufnr, winnr, function()
-- set filetype to rust for syntax highlighting
vim.bo[_window_state.latest_scratch_buf_id].filetype = 'rust'
-- write the expansion content to the buffer
vim.api.nvim_buf_set_lines(_window_state.latest_scratch_buf_id, 0, 0, false, markdown_lines)
end)

if config.tools.float_win_config.auto_focus then
vim.api.nvim_set_current_win(winnr)
end
end)
end

-- Save position in the window's jumplist
vim.cmd("normal! m'")
vim.api.nvim_win_set_cursor(win_id, { diagnostic.lnum + 1, diagnostic.col })
-- Open folds under the cursor
vim.cmd('normal! zv')
vim.system({ rustc, '--explain', tostring(diagnostic.code) }, nil, vim.schedule_wrap(handler))
end

function M.explain_error_current_line()
if vim.fn.executable(rustc) ~= 1 then
vim.notify('rustc is needed to explain errors.', vim.log.levels.ERROR)
Expand Down
15 changes: 1 addition & 14 deletions lua/rustaceanvim/commands/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,8 @@ local rustlsp_command_tbl = {
end,
},
explainError = {
impl = function(args)
local subcmd = args[1] or 'cycle'
if subcmd == 'cycle' then
require('rustaceanvim.commands.diagnostic').explain_error()
elseif subcmd == 'current' then
impl = function()
require('rustaceanvim.commands.diagnostic').explain_error_current_line()
else
vim.notify(
'explainError: unknown subcommand: ' .. subcmd .. " expected 'cycle' or 'current'",
vim.log.levels.ERROR
)
end
end,
complete = function()
return { 'cycle', 'current' }
end,
},
relatedDiagnostics = {
Expand Down

0 comments on commit e6d8580

Please sign in to comment.