@@ -54,111 +54,6 @@ local function set_close_keymaps(bufnr)
5454 vim .keymap .set (' n' , ' <Esc>' , close_hover , { buffer = bufnr , noremap = true , silent = true })
5555end
5656
57- function M .explain_error ()
58- if vim .fn .executable (rustc ) ~= 1 then
59- vim .notify (' rustc is needed to explain errors.' , vim .log .levels .ERROR )
60- return
61- end
62-
63- local diagnostics = vim
64- .iter (vim .diagnostic .get (0 , {}))
65- --- @param diagnostic vim.Diagnostic
66- :filter (function (diagnostic )
67- return diagnostic .code ~= nil
68- and diagnostic .source == ' rustc'
69- and diagnostic .severity == vim .diagnostic .severity .ERROR
70- end )
71- :totable ()
72- if # diagnostics == 0 then
73- vim .notify (' No explainable errors found.' , vim .log .levels .INFO )
74- return
75- end
76- local win_id = vim .api .nvim_get_current_win ()
77- local opts = {
78- cursor_position = vim .api .nvim_win_get_cursor (win_id ),
79- severity = vim .diagnostic .severity .ERROR ,
80- wrap = true ,
81- }
82- local found = false
83- local diagnostic
84- local pos_map = {}
85- --- @type string
86- local pos_id = ' 0'
87- repeat
88- diagnostic = vim .diagnostic .get_next (opts )
89- pos_map [pos_id ] = diagnostic
90- if diagnostic == nil then
91- break
92- end
93- found = diagnostic .code ~= nil and diagnostic .source == ' rustc'
94- local pos = { diagnostic .lnum , diagnostic .col }
95- -- check if there is an explainable error at the same location
96- if not found then
97- local cursor_diagnostics = vim .tbl_filter (function (diag )
98- return pos [1 ] == diag .lnum and pos [2 ] == diag .col
99- end , diagnostics )
100- if # cursor_diagnostics ~= 0 then
101- diagnostic = cursor_diagnostics [1 ]
102- found = true
103- break
104- end
105- end
106- pos_id = vim .inspect (pos )
107- -- diagnostics are (0,0)-indexed but cursors are (1,0)-indexed
108- opts .cursor_position = { pos [1 ] + 1 , pos [2 ] }
109- local searched_all = pos_map [pos_id ] ~= nil
110- until diagnostic == nil or found or searched_all
111- if not found then
112- -- Fall back to first diagnostic
113- diagnostic = diagnostics [1 ]
114- local pos = { diagnostic .lnum , diagnostic .col }
115- opts .cursor_position = pos
116- return
117- end
118-
119- --- @param sc vim.SystemCompleted
120- local function handler (sc )
121- if sc .code ~= 0 or not sc .stdout then
122- vim .notify (' Error calling rustc --explain' .. (sc .stderr and ' : ' .. sc .stderr or ' ' ), vim .log .levels .ERROR )
123- return
124- end
125- local output = sc .stdout :gsub (' ```' , ' ```rust' , 1 )
126- local markdown_lines = vim .lsp .util .convert_input_to_markdown_lines (output , {})
127- local float_preview_lines = vim .deepcopy (markdown_lines )
128- table.insert (float_preview_lines , 1 , ' ---' )
129- table.insert (float_preview_lines , 1 , ' 1. Open in split' )
130- vim .schedule (function ()
131- local bufnr , winnr = vim .lsp .util .open_floating_preview (
132- float_preview_lines ,
133- ' markdown' ,
134- vim .tbl_extend (' keep' , config .tools .float_win_config , {
135- focus_id = ' rustc-explain-error' ,
136- close_events = { ' CursorMoved' , ' BufHidden' , ' InsertCharPre' },
137- })
138- )
139- _window_state .float_winnr = winnr
140- set_close_keymaps (bufnr )
141- set_split_open_keymap (bufnr , winnr , function ()
142- -- set filetype to rust for syntax highlighting
143- vim .bo [_window_state .latest_scratch_buf_id ].filetype = ' rust'
144- -- write the expansion content to the buffer
145- vim .api .nvim_buf_set_lines (_window_state .latest_scratch_buf_id , 0 , 0 , false , markdown_lines )
146- end )
147-
148- if config .tools .float_win_config .auto_focus then
149- vim .api .nvim_set_current_win (winnr )
150- end
151- end )
152- end
153-
154- -- Save position in the window's jumplist
155- vim .cmd (" normal! m'" )
156- vim .api .nvim_win_set_cursor (win_id , { diagnostic .lnum + 1 , diagnostic .col })
157- -- Open folds under the cursor
158- vim .cmd (' normal! zv' )
159- vim .system ({ rustc , ' --explain' , tostring (diagnostic .code ) }, nil , vim .schedule_wrap (handler ))
160- end
161-
16257function M .explain_error_current_line ()
16358 if vim .fn .executable (rustc ) ~= 1 then
16459 vim .notify (' rustc is needed to explain errors.' , vim .log .levels .ERROR )
0 commit comments