feat: Revamp preview window

Please see README diff for details and implications.
This commit is contained in:
hedy
2023-11-01 21:04:26 +08:00
parent a7324dd3c9
commit d78d94218a
4 changed files with 76 additions and 117 deletions

View File

@@ -34,10 +34,11 @@ Below is a list of features I've included in this fork which, at the time of
writing, has not been included upstream (in the original repo). I try my best to
keep this list up to date.
Features:
Features/Changes:
- Feat: Toggling folds (and added default keymaps for it)
(simrat39/symbols-outline.nvim#194)
- Feat: Control focus between outline and code window.
- New commands: SymbolsOutline`Focus,FocusOutline,FocusCode` (see
[commands](#commands))
@@ -45,10 +46,21 @@ Features:
- simrat39/symbols-outline.nvim#143
- simrat39/symbols-outline.nvim#174
- simrat39/symbols-outline.nvim#207
- Feat: when `auto_close=true` only auto close if `goto_location` is used (where
focus changed), and not for `focus_location` (simrat39/symbols-outline.nvim#119)
- Feat: Cursorline option for the outline window
- MAJOR: Removed hover floating window from `toggle_preview`.
- Instead, you can set `open_hover_on_preview=true` (true by default) so that
the `hover_symbol` action can be triggered when `toggle_preview`is
triggered.
- The preview window's size changed to half of neovim height (rather than a
third). This is planned to be configurable.
- The preview window is positioned to be vertically center-aligned (rather
than fixed to the top). This is planned to be configurable.
Fixes:
- Fix symbol preview (simrat39/symbols-outline.nvim#176)
@@ -60,6 +72,12 @@ Fixes:
### PRs
Key:
```
✅ = Either merged superseded
📮 = Planned for merge
```
- Open handler checks if view is not already open
(#235 by eyalz800)
@@ -107,10 +125,10 @@ Fixes:
- feat: Add window_bg_highlight to config
(#137 by Zane-)
- Added preview width and relative size
- 📮 Added preview width and relative size
(#130 by Freyskeyd)
- Improve preview, hover windows configurability and looks
- 📮 Improve preview, hover windows configurability and looks
(#128 by toppair)
- ✅ Do not close outline when focus_location occurs
@@ -128,7 +146,7 @@ Fixes:
### TODO
KEY:
Key:
```
- [ ] : Planned
- [/] : WIP
@@ -138,19 +156,24 @@ KEY:
Items will be moved to above list when complete.
- Folds
- [ ] Org-like <kbd>shift+tab</kbd> behavior: Open folds level-by-level
- [ ] Optionally not opening all child nodes when opening parent node
- `[ ]` Org-like <kbd>shift+tab</kbd> behavior: Open folds level-by-level
- `[ ]` Optionally not opening all child nodes when opening parent node
- Fold siblings and siblings of parent on startup
- Navigation
- Go to parent
- Cycle siblings
- [ ] simrat39/symbols-outline.nvim#75: Handling of the outline window when attached
- `[ ]` simrat39/symbols-outline.nvim#75: Handling of the outline window when attached
buffer is closed.
Maybe it should continue working, so that pressing enter can open a split to
the correct location, and pressing `q` can properly close the buffer.
- Preview / Hover
- `[/]` Configurable winhighlight options for preview window (like nvim-cmp)
(simrat39/symbols-outline#128)
- `[/]` Configurable width and height (simrat39/symbols-outline#130)
### Related plugins
- nvim-navic
@@ -229,16 +252,26 @@ require("symbols-outline").setup({})
## Configuration
Pass a table to the setup call above with your configuration options.
### Terminology
- **Provider**: Source of the items in the outline view. Could be LSP, CoC, etc.
- **Node**: An item in the outline view
- **Fold**: Collapse a collapsible node
- **Location**: Where in the source file a node is from
- **Preview**: Peek the location of a node in code using a floating window
- **Hover**: Cursor currently on the line of a node
- **Hover symbol**: Displaying a floating window to show symbol information
provided by provider.
- **Focus**: Which window the cursor is in
### Options
Pass a table to the setup call with your configuration options.
Default values are shown:
```lua
local opts = {
-- Whether to highlight the currently hovered symbol (high cpu usage)
highlight_hovered_item = true,
-- Whether to show outline guides
show_guides = true,
-- Automatically open preview of code on hover
auto_preview = false,
-- Where to open the split window: right/left
position = 'right',
-- Whether width is relative to existing windows
@@ -246,10 +279,21 @@ local opts = {
-- Percentage or integer of columns
width = 25,
-- Whether to highlight the currently hovered symbol (high cpu usage)
highlight_hovered_item = true,
-- Whether to show outline guides
show_guides = true,
-- Automatically open preview of code on hover
auto_preview = false,
-- Automatically open hover_symbol when opening toggle_preview (see keymaps).
-- If you disable this you can still open hover_symbol using your keymap
-- below.
open_hover_on_preview = true,
-- Behaviour changed in this fork:
-- Auto close the outline window if goto_location is triggered and not for
-- focus_location
auto_close = false,
-- Vim options for the outline window
show_numbers = false,
show_relative_numbers = false,

View File

@@ -11,6 +11,7 @@ M.defaults = {
width = 25,
auto_close = false,
auto_preview = false,
open_hover_on_preview = true,
show_numbers = false,
show_relative_numbers = false,
show_cursorline = true,

View File

@@ -1,4 +1,5 @@
local so = require 'symbols-outline'
local soconfig = require 'symbols-outline.config'
local util = vim.lsp.util
local M = {}
@@ -28,7 +29,6 @@ function M.show_hover()
---@diagnostic disable-next-line: param-type-mismatch
function(_, result, _, config)
if not (result and result.contents) then
-- return { 'No information available' }
return
end
local markdown_lines = util.convert_input_to_markdown_lines(
@@ -36,10 +36,12 @@ function M.show_hover()
)
markdown_lines = util.trim_empty_lines(markdown_lines)
if vim.tbl_isempty(markdown_lines) then
-- return { 'No information available' }
return
end
return util.open_floating_preview(markdown_lines, 'markdown', config)
-- FIXME
local bufnr, winnr = util.open_floating_preview(markdown_lines, 'markdown', config)
local winhi = 'Normal:' .. soconfig.options.preview_bg_highlight
vim.api.nvim_win_set_option(winnr, 'winhighlight', winhi)
end
)
end

View File

@@ -1,13 +1,12 @@
local so = require 'symbols-outline'
local config = require 'symbols-outline.config'
local hover = require 'symbols-outline.hover'
local M = {}
local state = {
preview_buf = nil,
preview_win = nil,
hover_buf = nil,
hover_win = nil,
}
local function is_current_win_outline()
@@ -43,8 +42,7 @@ local function get_offset()
end
local function get_height()
local uis = vim.api.nvim_list_uis()
return math.ceil(uis[1].height / 3)
return vim.api.nvim_list_uis()[1].height
end
local function get_hovered_node()
@@ -90,78 +88,13 @@ local function setup_preview_buf()
update_preview(code_buf)
end
local function get_hover_params(node, winnr)
local bufnr = vim.api.nvim_win_get_buf(winnr)
local uri = vim.uri_from_bufnr(bufnr)
return {
textDocument = { uri = uri },
position = { line = node.line, character = node.character },
bufnr = bufnr,
}
end
local function update_hover()
if not has_code_win() then
return
end
local node = get_hovered_node()
if not node then
return
end
local provider = _G._symbols_outline_current_provider
local params = get_hover_params(node, so.state.code_win)
provider.hover_info(params.bufnr, params, function(err, result)
if err then
print(vim.inspect(err))
return
end
local markdown_lines = {}
if result ~= nil then
markdown_lines = vim.lsp.util.convert_input_to_markdown_lines(
result.contents
)
end
markdown_lines = vim.lsp.util.trim_empty_lines(markdown_lines)
if vim.tbl_isempty(markdown_lines) then
markdown_lines = { 'No info available' }
end
markdown_lines = vim.lsp.util.stylize_markdown(
state.hover_buf,
markdown_lines,
{}
)
if state.hover_buf ~= nil then
vim.api.nvim_buf_set_lines(state.hover_buf, 0, -1, 0, markdown_lines)
end
end)
end
local function setup_hover_buf()
if not has_code_win() then
return
end
local code_buf = vim.api.nvim_win_get_buf(so.state.code_win)
local ft = vim.api.nvim_buf_get_option(code_buf, 'filetype')
vim.api.nvim_buf_set_option(state.hover_buf, 'syntax', ft)
vim.api.nvim_buf_set_option(state.hover_buf, 'bufhidden', 'delete')
vim.api.nvim_win_set_option(state.hover_win, 'wrap', true)
vim.api.nvim_win_set_option(state.hover_win, 'cursorline', false)
update_hover()
end
local function set_bg_hl()
local winhi = 'Normal:' .. config.options.preview_bg_highlight
vim.api.nvim_win_set_option(state.preview_win, 'winhighlight', winhi)
vim.api.nvim_win_set_option(state.hover_win, 'winhighlight', winhi)
-- vim.api.nvim_win_set_option(state.hover_win, 'winhighlight', winhi)
local winblend = config.options.winblend
vim.api.nvim_win_set_option(state.preview_win, 'winblend', winblend)
vim.api.nvim_win_set_option(state.hover_win, 'winblend', winblend)
-- vim.api.nvim_win_set_option(state.hover_win, 'winblend', winblend)
end
local function show_preview()
@@ -174,13 +107,16 @@ local function show_preview()
end,
})
local offsets = get_offset()
local height = get_height()
local winheight = math.ceil(height / 2)
state.preview_win = vim.api.nvim_open_win(state.preview_buf, false, {
relative = 'win',
width = 50,
height = get_height(),
height = winheight,
bufpos = { 0, 0 },
row = offsets[1],
col = offsets[2],
-- Position preview window middle-aligned vertically
row = math.ceil((height - winheight) / 2),
border = config.options.border,
})
setup_preview_buf()
@@ -189,40 +125,16 @@ local function show_preview()
end
end
local function show_hover()
if state.hover_win == nil and state.hover_buf == nil then
state.hover_buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_attach(state.hover_buf, false, {
on_detach = function()
state.hover_buf = nil
state.hover_win = nil
end,
})
local offsets = get_offset()
local height = get_height()
state.hover_win = vim.api.nvim_open_win(state.hover_buf, false, {
relative = 'win',
width = 50,
height = height,
bufpos = { 0, 0 },
row = offsets[1] + height + 2,
col = offsets[2],
border = config.options.border,
})
setup_hover_buf()
else
update_hover()
end
end
function M.show()
if not is_current_win_outline() or #vim.api.nvim_list_wins() < 2 then
return
end
show_preview()
show_hover()
set_bg_hl()
if config.options.open_hover_on_preview then
hover.show_hover()
end
end
function M.close()