feat: Auto-resize and re-position preview

Only for auto-preview.
This commit is contained in:
hedy
2023-11-30 10:02:01 +08:00
parent a7bc96bb06
commit 4bbecbb92e
5 changed files with 70 additions and 10 deletions

View File

@@ -91,6 +91,8 @@
rarely beneficial to show neighboring symbol locations (sometimes even same rarely beneficial to show neighboring symbol locations (sometimes even same
line!) when opening outline with the intention of getting an overall view of line!) when opening outline with the intention of getting an overall view of
the file and jumping elsewhere. the file and jumping elsewhere.
- If auto-preview is enabled the preview window will automatically resize and
reposition
### Fixes ### Fixes

View File

@@ -949,6 +949,12 @@ preview_window = {
https://github.com/hedyhli/outline.nvim/assets/50042066/a473d791-d1b9-48e9-917f-b816b564a645 https://github.com/hedyhli/outline.nvim/assets/50042066/a473d791-d1b9-48e9-917f-b816b564a645
Note that auto-resizing of the preview window is only enabled for auto-preview.
Otherwise, close and reopen the preview after resizing the code window.
https://github.com/hedyhli/outline.nvim/assets/50042066/b7f6d2b6-98b3-4557-8143-e49583e99d3b
Alternatively, if you want to automatically navigate to the corresponding code Alternatively, if you want to automatically navigate to the corresponding code
location directly and not use the preview window: location directly and not use the preview window:

View File

@@ -227,9 +227,9 @@ function M.should_include_symbol(kind, bufnr)
end end
end end
-- XXX: If the given kind is not known by outline.nvim (ie: not in -- If the given kind is not known by outline.nvim (ie: not in all_kinds),
-- all_kinds), still return true. Only exclude those symbols that were -- still return true. Only exclude those symbols that were explicitly
-- explicitly filtered out. -- filtered out.
return filter_table[kind] ~= false return filter_table[kind] ~= false
end end

View File

@@ -15,6 +15,7 @@ local Preview = {}
---@field outline_height integer ---@field outline_height integer
---@field s outline.Sidebar ---@field s outline.Sidebar
---@field conf table ---@field conf table
---@field size_augroup integer
---@class outline.LivePreview ---@class outline.LivePreview
local LivePreview = {} local LivePreview = {}
@@ -30,6 +31,7 @@ local LivePreview = {}
---@field last_node outline.FlatSymbol ---@field last_node outline.FlatSymbol
---@field initial_cursorline boolean ---@field initial_cursorline boolean
---@field conf table ---@field conf table
---@field size_augroup integer
---@param conf table ---@param conf table
function Preview:new(conf) function Preview:new(conf)
@@ -82,6 +84,30 @@ local function calc_row(self)
return vim.api.nvim_win_get_position(self.s.view.win)[1] + offset return vim.api.nvim_win_get_position(self.s.view.win)[1] + offset
end end
---@param self outline.Preview|outline.LivePreview
local function update_size(self)
if self.size_augroup and (not self.win or not vim.api.nvim_win_is_valid(self.win)) then
vim.api.nvim_del_augroup_by_id(self.size_augroup)
self.win = nil
self.buf = nil
self.size_augroup = nil
return
end
self.outline_height = vim.api.nvim_win_get_height(self.s.view.win)
self.width = self.conf.width
self.height = math.max(math.ceil(self.outline_height / 2), self.conf.min_height)
local row = calc_row(self)
local col = calc_col(self)
vim.api.nvim_win_set_config(self.win, {
height = self.height,
width = self.width,
row = row,
col = col,
relative = 'editor',
})
end
function Preview:create() function Preview:create()
self.buf = vim.api.nvim_create_buf(false, true) self.buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_attach(self.buf, false, { vim.api.nvim_buf_attach(self.buf, false, {
@@ -105,8 +131,21 @@ function Preview:create()
}) })
self:setup() self:setup()
self:update() self:update()
end
if self.conf.auto_preview then
self.size_augroup = vim.api.nvim_create_augroup('outline_' .. self.s.id .. '_preview_size', {
clear = true,
})
vim.api.nvim_create_autocmd('WinResized', {
-- XXX: Using view.win doesn't work here?
pattern = tostring(self.s.code.win),
group = self.size_augroup,
callback = function()
update_size(self)
end,
})
end
end
---Set buf & win options, and setup highlight ---Set buf & win options, and setup highlight
function Preview:setup() function Preview:setup()
@@ -131,7 +170,6 @@ function Preview:setup()
vim.api.nvim_win_set_option(self.win, 'cursorline', true) vim.api.nvim_win_set_option(self.win, 'cursorline', true)
end end
function Preview:update() function Preview:update()
local node = self.s:_current_node() local node = self.s:_current_node()
if not node then if not node then
@@ -195,6 +233,19 @@ function LivePreview:create()
focusable = false, focusable = false,
}) })
self:setup() self:setup()
if self.conf.auto_preview then
self.size_augroup = vim.api.nvim_create_augroup('outline_' .. self.s.id .. '_preview_size', {
clear = true,
})
vim.api.nvim_create_autocmd('WinResized', {
-- XXX: Using view.win doesn't work here?
pattern = tostring(self.s.code.win),
group = self.size_augroup,
callback = function()
update_size(self)
end,
})
end
end end
---Set buf & win options, and autocmds ---Set buf & win options, and autocmds
@@ -209,7 +260,7 @@ function LivePreview:setup()
callback = function() callback = function()
self.s.code.win = self.codewin self.s.code.win = self.codewin
self.win = nil self.win = nil
end end,
}) })
vim.api.nvim_create_autocmd('WinEnter', { vim.api.nvim_create_autocmd('WinEnter', {
pattern = tostring(self.win), pattern = tostring(self.win),
@@ -217,7 +268,7 @@ function LivePreview:setup()
callback = function() callback = function()
-- This doesn't work at all? -- This doesn't work at all?
vim.api.nvim_win_set_option(self.win, 'cursorline', self.initial_cursorline) vim.api.nvim_win_set_option(self.win, 'cursorline', self.initial_cursorline)
end end,
}) })
end end

View File

@@ -237,8 +237,8 @@ end
---Set hide_cursor depending on whether cursorline is 'focus_in_outline' ---Set hide_cursor depending on whether cursorline is 'focus_in_outline'
function Sidebar:update_cursor_style() function Sidebar:update_cursor_style()
local cl = cfg.o.outline_window.show_cursorline local cl = cfg.o.outline_window.show_cursorline
-- XXX: Still 'hide' cursor if show_cursorline set to false, because we've -- Still 'hide' cursor if show_cursorline set to false, because we've already
-- already warned the user during setup. -- warned the user during setup.
local hide_cursor = type(cl) ~= 'string' local hide_cursor = type(cl) ~= 'string'
if cl == 'focus_in_outline' or cl == 'focus_in_code' then if cl == 'focus_in_outline' or cl == 'focus_in_code' then
@@ -342,6 +342,7 @@ function Sidebar:__refresh()
end end
-- stylua: ignore start -- stylua: ignore start
-- TODO: Is this still needed?
function Sidebar:_refresh() function Sidebar:_refresh()
(utils.debounce(function() self:__refresh() end, 100))() (utils.debounce(function() self:__refresh() end, 100))()
end end
@@ -731,7 +732,7 @@ function Sidebar:build_outline(find_node)
node.line == hovered_line node.line == hovered_line
or (hovered_line >= node.range_start and hovered_line <= node.range_end) or (hovered_line >= node.range_start and hovered_line <= node.range_end)
then then
-- XXX: not setting for children, but it works because when unfold is called -- Not setting for children, but it works because when unfold is called
-- this function is called again anyway. -- this function is called again anyway.
node.hovered = true node.hovered = true
table.insert(self.hovered, node) table.insert(self.hovered, node)