From 9f5a81249ce74f45c4b4b1cdba9736c1d055761c Mon Sep 17 00:00:00 2001 From: JuanZoran <1430359574@qq.com> Date: Fri, 24 Mar 2023 00:10:14 +0800 Subject: [PATCH] fix: fix wrong display size caculate remove util.display_size func --- lua/Trans/core/util.lua | 14 ------- lua/Trans/frontend/hover/init.lua | 61 +++++++++++++++++-------------- 2 files changed, 33 insertions(+), 42 deletions(-) diff --git a/lua/Trans/core/util.lua b/lua/Trans/core/util.lua index bcd99f9..903f48b 100644 --- a/lua/Trans/core/util.lua +++ b/lua/Trans/core/util.lua @@ -103,20 +103,6 @@ function M.display_width(lines) return width end ----Calculates the height and width of the text to be displayed ----@param lines string[] text to be displayed ----@param width integer width of the window ----@return { height: integer, width: integer } _ display height and width -function M.display_size(lines, width) - local ds_height, ds_width = 0, 0 - for _, line in ipairs(lines) do - local wid = line:width() - ds_height = ds_height + math.max(1, (math.ceil(wid / width))) - ds_width = math.max(wid, ds_width) - end - - return { height = ds_height, width = ds_width } -end ---Center node utility function ---@param node string -- TODO :Node diff --git a/lua/Trans/frontend/hover/init.lua b/lua/Trans/frontend/hover/init.lua index a26abaf..0b202e7 100644 --- a/lua/Trans/frontend/hover/init.lua +++ b/lua/Trans/frontend/hover/init.lua @@ -160,18 +160,18 @@ function M:defer() self.buffer:set("modifiable", false) local auto_close_events = self.opts.auto_close_events - if auto_close_events then - vim.api.nvim_create_autocmd(auto_close_events, { - callback = function(opts) - vim.defer_fn(function() - if not self.pin and vim.api.nvim_get_current_win() ~= self.window.winid then - pcall(vim.api.nvim_del_autocmd, opts.id) - self:destroy() - end - end, 0) - end, - }) - end + if not auto_close_events then return end + + vim.api.nvim_create_autocmd(auto_close_events, { + callback = function(opts) + vim.defer_fn(function() + if not self.pin and vim.api.nvim_get_current_win() ~= self.window.winid then + pcall(vim.api.nvim_del_autocmd, opts.id) + self:destroy() + end + end, 0) + end, + }) end ---Display Result in hover window @@ -186,7 +186,8 @@ function M:process(data) return end - local opts = self.opts + local opts = self.opts + local util = Trans.util local buffer = self.buffer if opts.auto_play then @@ -194,7 +195,7 @@ function M:process(data) end -- vim.pretty_print(result) - Trans.util.main_loop(function() + util.main_loop(function() if not buffer:is_valid() then buffer:init() else @@ -205,24 +206,28 @@ function M:process(data) self:load(result, name, opts.order[name]) end) - local display_size = Trans.util.display_size(buffer:lines(), opts.width) local window = self.window + local lines = buffer:lines() + + + local width = + window and window:is_valid() and + (opts.auto_resize and + math.max( + math.min(opts.width, util.display_width(lines) + opts.padding), + math.min(data.str:width(), opts.split_width) + ) + or opts.width) + or math.min(opts.width, util.display_width(lines) + opts.padding) + + local height = math.min(opts.height, util.display_height(lines, width)) + if window and window:is_valid() then - if opts.auto_resize then - display_size.width = math.max( - math.min(opts.width, display_size.width + opts.padding), - math.min(data.str:width(), opts.split_width) - ) - - else - display_size.width = nil - end - - window:resize(display_size) + window:resize { width = width, height = height } else window = self:init_window { - height = math.min(opts.height, display_size.height), - width = math.min(opts.width, display_size.width + opts.padding), + height = height, + width = width, } end