Skip to content

Commit

Permalink
feat(config): apply float_win_config to floating windows (#133)
Browse files Browse the repository at this point in the history
Co-authored-by: saying121 <saying121@example.com>
Co-authored-by: Marc Jakobi <mrcjkb89@outlook.com>
Co-authored-by: Marc Jakobi <marc@jakobi.dev>
  • Loading branch information
4 people authored Jan 8, 2024
1 parent 708a913 commit 07f2984
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 25 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 15 additions & 6 deletions lua/rustaceanvim/commands/explain_error.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local config = require('rustaceanvim.config.internal')
local M = {}

local compat = require('rustaceanvim.compat')
Expand Down Expand Up @@ -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

Expand Down
14 changes: 10 additions & 4 deletions lua/rustaceanvim/config/check.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
7 changes: 2 additions & 5 deletions lua/rustaceanvim/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
22 changes: 14 additions & 8 deletions lua/rustaceanvim/config/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions lua/rustaceanvim/hover_actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 07f2984

Please sign in to comment.