diff --git a/CHANGELOG.md b/CHANGELOG.md index 61030ed5..4a1988be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,17 @@ 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). +## [Unreleased] + +### Added + +- Config: `tools.float_win_config` for all floating Windows + created by this plugin. + Moved `border`, `max_width`, `max_height`, `auto_focus` + from `hover_actions` to `float_win_config`. + The `hover_actions` window options are still applied + if they exist, so as not to break compatibility. + ## [3.12.2] - 2024-01-07 ### Fixed diff --git a/lua/rustaceanvim/commands/explain_error.lua b/lua/rustaceanvim/commands/explain_error.lua index eed513be..c4aff7da 100644 --- a/lua/rustaceanvim/commands/explain_error.lua +++ b/lua/rustaceanvim/commands/explain_error.lua @@ -1,3 +1,4 @@ +local config = require('rustaceanvim.config.internal') local M = {} local compat = require('rustaceanvim.compat') @@ -52,12 +53,20 @@ function M.explain_error() 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' }, - }) + local _, winnr = vim.lsp.util.open_floating_preview( + markdown_lines, + 'markdown', + vim.tbl_extend('keep', config.tools.float_win_config, { + focus = false, + focusable = true, + focus_id = 'rustc-explain-error', + close_events = { 'CursorMoved', 'BufHidden', 'InsertCharPre' }, + }) + ) + + if config.tools.float_win_config.auto_focus then + vim.api.nvim_set_current_win(winnr) + end end) end diff --git a/lua/rustaceanvim/config/check.lua b/lua/rustaceanvim/config/check.lua index 55946e31..1a9290aa 100644 --- a/lua/rustaceanvim/config/check.lua +++ b/lua/rustaceanvim/config/check.lua @@ -50,15 +50,21 @@ function M.validate(cfg) end local hover_actions = tools.hover_actions ok, err = validate('tools.hover_actions', { - auto_focus = { hover_actions.auto_focus, 'boolean' }, - border = { hover_actions.border, 'table' }, - max_height = { hover_actions.max_height, 'number', true }, - max_width = { hover_actions.max_width, 'number', true }, replace_builtin_hover = { hover_actions.replace_builtin_hover, 'boolean' }, }) if not ok then return false, err end + local float_win_config = tools.float_win_config + ok, err = validate('tools.float_win_config', { + border = { float_win_config.border, { 'table', 'string' } }, + max_height = { float_win_config.max_height, 'number', true }, + max_width = { float_win_config.max_width, 'number', true }, + auto_focus = { float_win_config.auto_focus, 'boolean' }, + }) + if not ok then + return false, err + end ok, err = validate('tools', { executor = { tools.executor, { 'table', 'string' } }, on_initialized = { tools.on_initialized, 'function', true }, diff --git a/lua/rustaceanvim/config/init.lua b/lua/rustaceanvim/config/init.lua index 36007f63..6bd97725 100644 --- a/lua/rustaceanvim/config/init.lua +++ b/lua/rustaceanvim/config/init.lua @@ -59,15 +59,12 @@ vim.g.rustaceanvim = vim.g.rustaceanvim ---@field on_initialized? fun(health:RustAnalyzerInitializedStatus) Function that is invoked when the LSP server has finished initializing ---@field reload_workspace_from_cargo_toml? boolean Automatically call `RustReloadWorkspace` when writing to a Cargo.toml file ---@field hover_actions? RustaceanHoverActionsOpts Options for hover actions +---@field float_win_config? table Options applied to floating windows. See |api-win_config|. ---@field create_graph? RustaceanCrateGraphConfig Options for showing the crate graph based on graphviz and the dot ---@field open_url? fun(url:string):nil If set, overrides how to open URLs ---@class RustaceanHoverActionsOpts ----@field replace_builtin_hover? boolean Whether to replace Neovim's built-in `vim.lsp.buf.hover` ----@field border? string[][] See `vim.api.nvim_open_win` ----@field max_width? integer | nil Maximum width of the hover window (`nil` means no max.) ----@field max_height? integer | nil Maximum height of the hover window (`nil` means no max.) ----@field auto_focus? boolean Whether to automatically focus the hover action window +---@field replace_builtin_hover? boolean Whether to replace Neovim's built-in `vim.lsp.buf.hover` with hover actions. Default: `true` ---@alias lsp_server_health_status 'ok' | 'warning' | 'error' diff --git a/lua/rustaceanvim/config/internal.lua b/lua/rustaceanvim/config/internal.lua index a2089204..713881c9 100644 --- a/lua/rustaceanvim/config/internal.lua +++ b/lua/rustaceanvim/config/internal.lua @@ -42,13 +42,20 @@ local RustaceanDefaultConfig = { ---@class RustaceanHoverActionsConfig hover_actions = { - --- whether to replace Neovim's built-in `vim.lsp.buf.hover` + --- whether to replace Neovim's built-in `vim.lsp.buf.hover`. + --- default: true ---@type boolean replace_builtin_hover = true, + }, + + --- options same as lsp hover + ---@see vim.lsp.util.open_floating_preview + ---@type table Options applied to floating windows. + float_win_config = { - -- the border that is used for the hover window + -- the border that is used for floating windows ---@see vim.api.nvim_open_win() - ---@type string[][] + ---@type string[][] | string border = { { '╭', 'FloatBorder' }, { '─', 'FloatBorder' }, @@ -58,17 +65,17 @@ local RustaceanDefaultConfig = { { '─', 'FloatBorder' }, { '╰', 'FloatBorder' }, { '│', 'FloatBorder' }, - }, + }, -- maybe: 'double', 'rounded', 'shadow', 'single', - --- maximal width of the hover window. Nil means no max. + --- maximal width of floating windows. Nil means no max. ---@type integer | nil max_width = nil, - --- maximal height of the hover window. Nil means no max. + --- maximal height of floating windows. Nil means no max. ---@type integer | nil max_height = nil, - --- whether the hover action window gets automatically focused + --- whether the window gets automatically focused --- default: false ---@type boolean auto_focus = false, @@ -301,7 +308,6 @@ local RustaceanDefaultConfig = { }, was_g_rustaceanvim_sourced = vim.g.rustaceanvim ~= nil, } - local rustaceanvim = vim.g.rustaceanvim or {} local opts = type(rustaceanvim) == 'function' and rustaceanvim() or rustaceanvim if opts.tools and opts.tools.executor and type(opts.tools.executor) == 'string' then diff --git a/lua/rustaceanvim/hover_actions.lua b/lua/rustaceanvim/hover_actions.lua index db7a5488..25138eef 100644 --- a/lua/rustaceanvim/hover_actions.lua +++ b/lua/rustaceanvim/hover_actions.lua @@ -78,17 +78,20 @@ function M.handler(_, result, ctx) return end + -- NOTE: This is for backward compatibility + local win_opt = vim.tbl_deep_extend('force', config.tools.float_win_config, config.tools.hover_actions) + local bufnr, winnr = lsp_util.open_floating_preview( markdown_lines, 'markdown', - vim.tbl_extend('keep', config.tools.hover_actions, { + vim.tbl_extend('keep', win_opt, { focusable = true, focus_id = 'rust-analyzer-hover-actions', close_events = { 'CursorMoved', 'BufHidden', 'InsertCharPre' }, }) ) - if config.tools.hover_actions.auto_focus then + if win_opt.auto_focus then vim.api.nvim_set_current_win(winnr) end