From d0cec198269c35485b12d9dd908c25e45a79231d Mon Sep 17 00:00:00 2001 From: Marc Jakobi Date: Sun, 28 Jan 2024 21:42:49 +0100 Subject: [PATCH] fix(renderDiagnostic): hover closes immediately if `auto_focus` disabled --- CHANGELOG.md | 9 +++++ lua/rustaceanvim/commands/diagnostic.lua | 43 ++++++++++++++---------- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cd421503..b0993d99 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lua/rustaceanvim/commands/diagnostic.lua b/lua/rustaceanvim/commands/diagnostic.lua index b77e53a3..10c5c202 100644 --- a/lua/rustaceanvim/commands/diagnostic.lua +++ b/lua/rustaceanvim/commands/diagnostic.lua @@ -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 @@ -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 @@ -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