-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
110 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
local M = {} | ||
|
||
local rustc = 'rustc' | ||
|
||
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.tbl_filter(function(diagnostic) | ||
return diagnostic.code ~= nil and diagnostic.source == 'rustc' | ||
end, vim.diagnostic.get(0, {})) | ||
if #diagnostics == 0 then | ||
vim.notify('No explainnable 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 = {} | ||
local pos_id = 1 | ||
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 } | ||
pos_id = pos[1] + pos[2] | ||
opts.cursor_position = pos | ||
local searched_all = pos_map[pos_id] ~= nil | ||
until diagnostic == nil or found or searched_all | ||
if not found then | ||
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, {}) | ||
vim.schedule(function() | ||
vim.lsp.util.open_floating_preview(markdown_lines, 'markdown', { | ||
focus = false, | ||
focusable = true, | ||
focus_id = 'rustc-explain-error', | ||
close_events = { 'CursorMoved', 'BufHidden', 'InsertCharPre' }, | ||
}) | ||
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', diagnostic.code }, nil, vim.schedule_wrap(handler)) | ||
end | ||
|
||
return M.explain_error |
This file contains 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
This file contains 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
This file contains 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