Skip to content

Commit

Permalink
fixed dynamic popup window sizing
Browse files Browse the repository at this point in the history
  • Loading branch information
huynle committed Sep 27, 2024
1 parent 1c24844 commit 8906536
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 15 deletions.
1 change: 1 addition & 0 deletions lua/ogpt/common/popup.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function Popup:init(options, edgy)
options = options or {}
self.edgy = false
self.init_update = false
self.parent_winid = nil
if options.edgy and options.border or edgy then
self.edgy = true
options.border = nil
Expand Down
2 changes: 1 addition & 1 deletion lua/ogpt/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ function M.defaults()
relative = "editor",
size = {
width = "40%",
height = 10,
height = 5,
},
padding = { 1, 1, 1, 1 },
enter = true,
Expand Down
7 changes: 6 additions & 1 deletion lua/ogpt/flows/actions/popup/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function PopupAction:init(name, opts)
self.ui = opts.ui or {}
self.cur_win = vim.api.nvim_get_current_win()
self.output_panel = PopupWindow(popup_options)
self.output_panel.parent_winid = self.cur_win
self.spinner = Spinner:new(function(state) end)

self:update_variables()
Expand All @@ -47,7 +48,6 @@ end

function PopupAction:run()
-- self.stop = false
local response = Response(self.provider)
local params = self:get_params()
local _, start_row, start_col, end_row, end_col = self:get_visual_selection()
local opts = {
Expand All @@ -68,6 +68,11 @@ function PopupAction:run()
-- self.stop = true
end,
}
local response = Response(self.provider, {
on_post_render = function(response)
self.output_panel:update_popup_size(opts)
end,
})

if self.strategy == STRATEGY_DISPLAY then
self:set_loading(true)
Expand Down
16 changes: 7 additions & 9 deletions lua/ogpt/flows/actions/popup/window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ function PopupWindow:update_popup_size(opts)
end
opts = vim.tbl_extend("force", self.options, opts or {})
opts.lines = vim.api.nvim_buf_get_lines(self.bufnr, 0, -1, false)
local ui_opts = self:calculate_size(opts)
self:update_layout(ui_opts)
if self.options.dynamic_resize then
opts = self:calculate_size(opts)
end
self:update_layout(opts)
end

function PopupWindow:calculate_size(opts)
Expand Down Expand Up @@ -49,10 +51,6 @@ function PopupWindow:calculate_size(opts)
ui_h = math.min(ui_h, max_h)
ui_h = math.max(ui_h, 1)

-- use opts
ui_w = opts.ui_w or ui_w
ui_h = opts.ui_h or ui_h

-- build ui opts
local ui_opts = vim.tbl_deep_extend("keep", {
size = {
Expand Down Expand Up @@ -95,9 +93,9 @@ function PopupWindow:mount(opts)
if self.options.dynamic_resize then
-- set the event
-- https://github.com/MunifTanjim/nui.nvim/blob/main/lua/nui/popup/README.md#popupupdate_layout
self:on(event.CursorMoved, function()
self:update_popup_size(opts)
end)
-- self:on(event.CursorMoved, function()
-- self:update_popup_size(opts)
-- end)
else
opts = {
cur_win = opts.cur_win,
Expand Down
7 changes: 3 additions & 4 deletions lua/ogpt/flows/chat/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,12 @@ function Chat:addAnswerPartial(response)
for i, line in ipairs(lines) do
local currentLine = vim.api.nvim_buf_get_lines(buffer, -2, -1, false)[1]
vim.api.nvim_buf_set_lines(buffer, -2, -1, false, { currentLine .. line })

local last_line_num = vim.api.nvim_buf_line_count(buffer)
Signs.set_for_lines(self.chat_window.bufnr, start_line, last_line_num - 1, "chat")
if i == length and i > 1 then
vim.api.nvim_buf_set_lines(buffer, -1, -1, false, { "" })
end
if self:is_buf_visiable() then
if self:is_buf_visible() then
vim.api.nvim_win_set_cursor(win, { last_line_num, 0 })
end
end
Expand Down Expand Up @@ -555,7 +554,7 @@ function Chat:is_buf_exists()
return vim.fn.bufexists(self.chat_window.bufnr) == 1
end

function Chat:is_buf_visiable()
function Chat:is_buf_visible()
-- Get all windows in the current tab
local wins = vim.api.nvim_tabpage_list_wins(0)
-- Traverse the window list to determine whether the buffer of chat_window is visible in the window
Expand Down Expand Up @@ -585,7 +584,7 @@ function Chat:add_highlight(hl_group, line, col_start, col_end)
end

function Chat:set_cursor(pos)
if self:is_buf_visiable() then
if self:is_buf_visible() then
pcall(vim.api.nvim_win_set_cursor, self.chat_window.winid, pos)
end
end
Expand Down
6 changes: 6 additions & 0 deletions lua/ogpt/response.lua
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,17 @@ function Response:pop_content()
if content[2] == "END" and (content[1] and content[1] == "") then
content[1] = self:get_processed_text()
end
if self.events.on_pop_content then
self.events.on_pop_content()
end
return content
end

function Response:render()
self.partial_result_cb(self)
if self.events.on_post_render then
self.events.on_post_render(self)
end
end

function Response:pop_chunk()
Expand Down

0 comments on commit 8906536

Please sign in to comment.