chore: refine creating floating win (#182)

This commit is contained in:
yetone 2024-08-24 02:33:35 +08:00 committed by GitHub
parent 4693da8757
commit f2173c9a3c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 16 deletions

View File

@ -24,13 +24,13 @@ setmetatable(FloatingWindow, {
end, end,
}) })
---@class opts ---@class FloatingWindowOptions
---@field enter? boolean ---@field enter? boolean
---@field buf_options? table<string, any> ---@field buf_options? table<string, any>
---@field win_options? table<string, any> ---@field win_options? table<string, any>
---@field float_options? table<string, any> ---@field float_options? table<string, any>
---@param opts opts ---@param opts FloatingWindowOptions
---@return FloatingWindow ---@return FloatingWindow
function FloatingWindow.new(opts) function FloatingWindow.new(opts)
local instance = setmetatable({}, FloatingWindow) local instance = setmetatable({}, FloatingWindow)
@ -47,7 +47,7 @@ function FloatingWindow.new(opts)
end end
---@param split_winid integer ---@param split_winid integer
---@param opts opts ---@param opts FloatingWindowOptions
---@return FloatingWindow ---@return FloatingWindow
function FloatingWindow.from_split_win(split_winid, opts) function FloatingWindow.from_split_win(split_winid, opts)
local split_win_width = api.nvim_win_get_width(split_winid) local split_win_width = api.nvim_win_get_width(split_winid)

View File

@ -1307,15 +1307,22 @@ function Sidebar:get_selected_code_size()
return selected_code_size return selected_code_size
end end
local function create_floating_window_for_split(split_winid, buf_opts, win_opts, float_opts) ---@class CreateFloatingWindowForSplitOptions
local win_opts_ = vim.tbl_deep_extend("force", get_win_options(), win_opts or {}) ---@field split_winid integer | nil
---@field buf_opts table | nil
---@field win_opts table | nil
---@field float_opts table | nil
local buf_opts_ = vim.tbl_deep_extend("force", buf_options, buf_opts or {}) ---@param opts CreateFloatingWindowForSplitOptions
local function create_floating_window_for_split(opts)
local win_opts_ = vim.tbl_deep_extend("force", get_win_options(), opts.win_opts or {})
local floating_win = FloatingWindow.from_split_win(split_winid, { local buf_opts_ = vim.tbl_deep_extend("force", buf_options, opts.buf_opts or {})
local floating_win = FloatingWindow.from_split_win(opts.split_winid, {
buf_options = buf_opts_, buf_options = buf_opts_,
win_options = win_opts_, win_options = win_opts_,
float_options = float_opts, float_options = opts.float_opts,
}) })
return floating_win return floating_win
@ -1339,20 +1346,19 @@ function Sidebar:render()
self.result_container:mount() self.result_container:mount()
self.result = create_floating_window_for_split( self.result = create_floating_window_for_split({
self.result_container.winid, split_winid = self.result_container.winid,
{ buf_opts = {
modifiable = false, modifiable = false,
swapfile = false, swapfile = false,
buftype = "nofile", buftype = "nofile",
bufhidden = "wipe", bufhidden = "wipe",
filetype = "Avante", filetype = "Avante",
}, },
nil, float_opts = {
{
height = math.max(0, sidebar_height - selected_code_size - 9), height = math.max(0, sidebar_height - selected_code_size - 9),
} },
) })
self.result:on(event.BufWinEnter, function() self.result:on(event.BufWinEnter, function()
xpcall(function() xpcall(function()
@ -1411,7 +1417,7 @@ function Sidebar:render()
}, },
}) })
self.selected_code_container:mount() self.selected_code_container:mount()
self.selected_code = create_floating_window_for_split(self.selected_code_container.winid) self.selected_code = create_floating_window_for_split({ split_winid = self.selected_code_container.winid })
self.selected_code:mount() self.selected_code:mount()
end end