Skip to content

Commit 969888d

Browse files
committed
feat: add cycle_prev to explainError and renderDiagnostic
1 parent d197f49 commit 969888d

File tree

5 files changed

+44
-16
lines changed

5 files changed

+44
-16
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,11 +448,12 @@ vim.keymap.set(
448448
over error diagnostics (if they have an error code).
449449

450450
```vim
451-
:RustLsp explainError {cycle?|current?}
451+
:RustLsp explainError {cycle?|cycle_prev?|current?}
452452
```
453453
```lua
454454
vim.cmd.RustLsp('explainError') -- default to 'cycle'
455455
vim.cmd.RustLsp({ 'explainError', 'cycle' })
456+
vim.cmd.RustLsp({ 'explainError', 'cycle_prev' })
456457
vim.cmd.RustLsp({ 'explainError', 'current' })
457458
```
458459

@@ -461,6 +462,10 @@ vim.keymap.set(
461462
`explainError` will cycle diagnostics,
462463
starting at the cursor position,
463464
until it can find a diagnostic with an error code.
465+
466+
- If called with `cycle_prev`:
467+
Like `vim.diagnostic.goto_prev`,
468+
searches backwards for a diagnostic with an error code.
464469

465470
- If called with `current`:
466471
Searches for diagnostics only in the
@@ -482,11 +487,12 @@ vim.keymap.set(
482487
together.
483488

484489
```vim
485-
:RustLsp renderDiagnostic {cycle?|current?}
490+
:RustLsp renderDiagnostic {cycle?|cycle_prev?|current?}
486491
```
487492
```lua
488493
vim.cmd.RustLsp('renderDiagnostic') -- defaults to 'cycle'
489494
vim.cmd.RustLsp({ 'renderDiagnostic', 'cycle' })
495+
vim.cmd.RustLsp({ 'renderDiagnostic', 'cycle_prev' })
490496
vim.cmd.RustLsp({ 'renderDiagnostic', 'current' })
491497
```
492498

@@ -495,6 +501,10 @@ vim.keymap.set(
495501
`renderDiagnostic` will cycle diagnostics,
496502
starting at the cursor position,
497503
until it can find a diagnostic with rendered data.
504+
505+
- If called with `cycle_prev`:
506+
Like `vim.diagnostic.goto_prev`,
507+
searches backwards for a diagnostic with rendered data.
498508

499509
- If called with `current`:
500510
Searches for diagnostics only in the

doc/rustaceanvim.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,28 @@ It accepts the following subcommands:
6262

6363
For example, if you set the keymap: `vim.keymap.set('n', '<space>a', '<Plug>RustHoverAction')`,
6464
you can invoke the third hover action with `3<space>a`.
65-
'explainError {cycle?|current?}' - Display a hover window with explanations form the Rust error index.
65+
'explainError {cycle?|cycle_prev?|current?}' - Display a hover window with explanations form the Rust error index.
6666
- If called with |cycle| or no args:
6767
Like |vim.diagnostic.goto_next|,
6868
|explainError| will cycle diagnostics,
6969
starting at the cursor position,
7070
until it can find a diagnostic with an error code.
71+
- If called with |cycle_prev|:
72+
Like |vim.diagnostic.goto_prev|,
73+
searches backwards for a diagnostic with an error code.
7174
- If called with |current|:
7275
Searches for diagnostics only in the
7376
current cursor line.
74-
'renderDiagnostic {cycle?|current?}' - Display a hover window with the rendered diagnostic,
77+
'renderDiagnostic {cycle?|cycle_prev?|current?}' - Display a hover window with the rendered diagnostic,
7578
as displayed during |cargo build|.
7679
- If called with |cycle| or no args:
7780
Like |vim.diagnostic.goto_next|,
7881
|renderDiagnostic| will cycle diagnostics,
7982
starting at the cursor position,
8083
until it can find a diagnostic with rendered data.
84+
- If called with |cycle_prev|:
85+
Like |vim.diagnostic.goto_prev|,
86+
searches backwards for a diagnostic with rendered data.
8187
- If called with |current|:
8288
Searches for diagnostics only in the
8389
current cursor line.

lua/rustaceanvim/commands/diagnostic.lua

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ local function set_close_keymaps(bufnr)
5454
vim.keymap.set('n', '<Esc>', close_hover, { buffer = bufnr, noremap = true, silent = true })
5555
end
5656

57-
function M.explain_error()
57+
---@param cycle_diagnostic (fun(opts?: vim.diagnostic.JumpOpts): vim.Diagnostic?)
58+
function M.explain_error(cycle_diagnostic)
5859
if vim.fn.executable(rustc) ~= 1 then
5960
vim.notify('rustc is needed to explain errors.', vim.log.levels.ERROR)
6061
return
@@ -86,7 +87,7 @@ function M.explain_error()
8687
---@type string
8788
local pos_id = '0'
8889
repeat
89-
diagnostic = vim.diagnostic.get_next(opts)
90+
diagnostic = cycle_diagnostic(opts)
9091
pos_map[pos_id] = diagnostic
9192
if diagnostic == nil then
9293
break
@@ -299,7 +300,8 @@ local function render_ansi_code_diagnostic(rendered_diagnostic)
299300
end)
300301
end
301302

302-
function M.render_diagnostic()
303+
---@param cycle_diagnostic (fun(opts?: vim.diagnostic.JumpOpts): vim.Diagnostic?)
304+
function M.render_diagnostic(cycle_diagnostic)
303305
local diagnostics = vim
304306
.iter(vim.diagnostic.get(0, {}))
305307
---@param diagnostic vim.Diagnostic
@@ -323,7 +325,7 @@ function M.render_diagnostic()
323325
---@type string
324326
local pos_id = '0'
325327
repeat
326-
diagnostic = vim.diagnostic.get_next(opts)
328+
diagnostic = cycle_diagnostic(opts)
327329
pos_map[pos_id] = diagnostic
328330
if diagnostic == nil then
329331
break

lua/rustaceanvim/commands/init.lua

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,20 @@ local rustlsp_command_tbl = {
6363
impl = function(args)
6464
local subcmd = args[1] or 'cycle'
6565
if subcmd == 'cycle' then
66-
require('rustaceanvim.commands.diagnostic').explain_error()
66+
require('rustaceanvim.commands.diagnostic').explain_error(vim.diagnostic.get_next)
67+
elseif subcmd == 'cycle_prev' then
68+
require('rustaceanvim.commands.diagnostic').explain_error(vim.diagnostic.get_prev)
6769
elseif subcmd == 'current' then
6870
require('rustaceanvim.commands.diagnostic').explain_error_current_line()
6971
else
7072
vim.notify(
71-
'explainError: unknown subcommand: ' .. subcmd .. " expected 'cycle' or 'current'",
73+
'explainError: unknown subcommand: ' .. subcmd .. " expected 'cycle', 'cycle_prev', or 'current'",
7274
vim.log.levels.ERROR
7375
)
7476
end
7577
end,
7678
complete = function()
77-
return { 'cycle', 'current' }
79+
return { 'cycle', 'cycle_prev', 'current' }
7880
end,
7981
},
8082
relatedDiagnostics = {
@@ -86,18 +88,20 @@ local rustlsp_command_tbl = {
8688
impl = function(args)
8789
local subcmd = args[1] or 'cycle'
8890
if subcmd == 'cycle' then
89-
require('rustaceanvim.commands.diagnostic').render_diagnostic()
91+
require('rustaceanvim.commands.diagnostic').render_diagnostic(vim.diagnostic.get_next)
92+
elseif subcmd == 'cycle_prev' then
93+
require('rustaceanvim.commands.diagnostic').render_diagnostic(vim.diagnostic.get_prev)
9094
elseif subcmd == 'current' then
9195
require('rustaceanvim.commands.diagnostic').render_diagnostic_current_line()
9296
else
9397
vim.notify(
94-
'renderDiagnostic: unknown subcommand: ' .. subcmd .. " expected 'cycle' or 'current'",
98+
'renderDiagnostic: unknown subcommand: ' .. subcmd .. " expected 'cycle', 'cycle_prev', or 'current'",
9599
vim.log.levels.ERROR
96100
)
97101
end
98102
end,
99103
complete = function()
100-
return { 'cycle', 'current' }
104+
return { 'cycle', 'cycle_prev', 'current' }
101105
end,
102106
},
103107
rebuildProcMacros = {

lua/rustaceanvim/init.lua

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,28 @@
5555
---
5656
--- For example, if you set the keymap: `vim.keymap.set('n', '<space>a', '<Plug>RustHoverAction')`,
5757
--- you can invoke the third hover action with `3<space>a`.
58-
--- 'explainError {cycle?|current?}' - Display a hover window with explanations form the Rust error index.
58+
--- 'explainError {cycle?|cycle_prev?|current?}' - Display a hover window with explanations form the Rust error index.
5959
--- - If called with |cycle| or no args:
6060
--- Like |vim.diagnostic.goto_next|,
6161
--- |explainError| will cycle diagnostics,
6262
--- starting at the cursor position,
6363
--- until it can find a diagnostic with an error code.
64+
--- - If called with |cycle_prev|:
65+
--- Like |vim.diagnostic.goto_prev|,
66+
--- searches backwards for a diagnostic with an error code.
6467
--- - If called with |current|:
6568
--- Searches for diagnostics only in the
6669
--- current cursor line.
67-
--- 'renderDiagnostic {cycle?|current?}' - Display a hover window with the rendered diagnostic,
70+
--- 'renderDiagnostic {cycle?|cycle_prev?|current?}' - Display a hover window with the rendered diagnostic,
6871
--- as displayed during |cargo build|.
6972
--- - If called with |cycle| or no args:
7073
--- Like |vim.diagnostic.goto_next|,
7174
--- |renderDiagnostic| will cycle diagnostics,
7275
--- starting at the cursor position,
7376
--- until it can find a diagnostic with rendered data.
77+
--- - If called with |cycle_prev|:
78+
--- Like |vim.diagnostic.goto_prev|,
79+
--- searches backwards for a diagnostic with rendered data.
7480
--- - If called with |current|:
7581
--- Searches for diagnostics only in the
7682
--- current cursor line.

0 commit comments

Comments
 (0)