Skip to content

Commit

Permalink
fix(renderDiagnostic): hover closes immediately if auto_focus disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Jan 28, 2024
1 parent d433164 commit d0cec19
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [4.0.3] - 2024-01-28

### Fixed

- `renderDiagnostic`: Window closes immediately if `auto_focus`
is disabled [[#193](https://github.com/mrcjkb/rustaceanvim/issues/193)].
- `explainError`/`renderDiagnostic`: Fall back to first
detected diagnostic if none is found close to the cursor.

## [4.0.2] - 2024-01-27

### Fixed
Expand Down
43 changes: 26 additions & 17 deletions lua/rustaceanvim/commands/diagnostic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ function M.explain_error()
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

Expand Down Expand Up @@ -181,7 +185,9 @@ function M.render_diagnostic()
local searched_all = pos_map[pos_id] ~= nil
until diagnostic == nil or rendered_diagnostic ~= nil or searched_all
if not rendered_diagnostic then
return
-- No diagnostics found. Fall back to first result from filter,
rendered_diagnostic = get_rendered_diagnostic(diagnostics[1])
---@cast rendered_diagnostic string
end

-- Save position in the window's jumplist
Expand All @@ -194,22 +200,25 @@ function M.render_diagnostic()
local float_preview_lines = vim.deepcopy(markdown_lines)
table.insert(float_preview_lines, 1, '---')
table.insert(float_preview_lines, 1, '1. Open in split')
close_hover()
local bufnr, winnr = vim.lsp.util.open_floating_preview(
float_preview_lines,
'markdown',
vim.tbl_extend('keep', config.tools.float_win_config, {
focus = false,
focusable = true,
focus_id = 'ra-render-diagnostic',
close_events = { 'CursorMoved', 'BufHidden', 'InsertCharPre' },
})
)
_window_state.winnr = winnr
set_open_split_keymap(bufnr, winnr, markdown_lines)
if config.tools.float_win_config.auto_focus then
vim.api.nvim_set_current_win(winnr)
end
vim.schedule(function()
close_hover()
local bufnr, winnr = vim.lsp.util.open_floating_preview(
float_preview_lines,
'markdown',
vim.tbl_extend('keep', config.tools.float_win_config, {
focus = false,
focusable = true,
focus_id = 'ra-render-diagnostic',
close_events = { 'CursorMoved', 'BufHidden', 'InsertCharPre' },
})
)
_window_state.float_winnr = winnr
set_close_keymaps(bufnr)
set_open_split_keymap(bufnr, winnr, markdown_lines)
if config.tools.float_win_config.auto_focus then
vim.api.nvim_set_current_win(winnr)
end
end)
end

return M

0 comments on commit d0cec19

Please sign in to comment.