From c699aaba24efd909ab648d8175c3cf38b8e7e5c3 Mon Sep 17 00:00:00 2001 From: JuanZoran <1430359574@qq.com> Date: Wed, 15 Mar 2023 14:30:01 +0800 Subject: [PATCH] fix: fix cacha miss --- lua/Trans/core/buffer.lua | 20 ++++++++++---------- lua/Trans/core/conf.lua | 2 +- lua/Trans/core/data.lua | 8 ++++---- lua/Trans/frontend/hover/init.lua | 12 +++++++++--- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/lua/Trans/core/buffer.lua b/lua/Trans/core/buffer.lua index b9e85f2..24f3ecd 100644 --- a/lua/Trans/core/buffer.lua +++ b/lua/Trans/core/buffer.lua @@ -112,11 +112,8 @@ end function buffer:setline(nodes, one_index) self:set('modifiable', true) - - ---@diagnostic disable-next-line: cast-local-type, param-type-mismatch one_index = one_index or self:line_count() + 1 if one_index == 2 and self[1] == '' then one_index = 1 end - ---@cast one_index integer if type(nodes) == 'string' then fn.setbufline(self.bufnr, one_index, nodes) @@ -167,18 +164,21 @@ buffer.__newindex = function(self, key, nodes) end end +---Init buffer with bufnr +---@param bufnr? integer buffer handle +function buffer:init(bufnr) + self.bufnr = bufnr or api.nvim_create_buf(false, false) + self:set('modifiable', false) + self:set('filetype', 'Trans') + self:set('buftype', 'nofile') +end ---@nodiscard ---TransBuffer constructor ---@return TransBuffer function buffer.new() - local new_buf = setmetatable({ - bufnr = api.nvim_create_buf(false, false), - }, buffer) - - new_buf:set('modifiable', false) - new_buf:set('filetype', 'Trans') - new_buf:set('buftype', 'nofile') + local new_buf = setmetatable({}, buffer) + new_buf:init() return new_buf end diff --git a/lua/Trans/core/conf.lua b/lua/Trans/core/conf.lua index 63b2040..36b4650 100644 --- a/lua/Trans/core/conf.lua +++ b/lua/Trans/core/conf.lua @@ -59,7 +59,7 @@ return { ---@type string -- TODO :support replace with {{special word}} fallback_message = '翻译超时或没有找到相关的翻译', auto_resize = true, - padding = 10, -- only work when auto_resize is true + padding = 10, -- padding for hover window width keymaps = { play = '_', pageup = '[[', diff --git a/lua/Trans/core/data.lua b/lua/Trans/core/data.lua index e82e2ff..5fad39d 100644 --- a/lua/Trans/core/data.lua +++ b/lua/Trans/core/data.lua @@ -64,11 +64,11 @@ end ---@return TransResult? function M:get_available_result() local result = self.result - local backend = self.backends - for _, name in ipairs(backend) do - if result[name] then - return result[name] + for _, backend in ipairs(self.backends) do + if result[backend.name] then + ---@diagnostic disable-next-line: return-type-mismatch + return result[backend.name] end end end diff --git a/lua/Trans/frontend/hover/init.lua b/lua/Trans/frontend/hover/init.lua index 18e0a5e..517ed59 100644 --- a/lua/Trans/frontend/hover/init.lua +++ b/lua/Trans/frontend/hover/init.lua @@ -137,6 +137,8 @@ function M:process(_, result) -- local it, t, f = node.item, node.text, node.format -- self.buffer:setline(it('hello', 'MoreMsg')) local opts = self.opts + if not self.buffer:is_valid() then self.buffer:init() end + for _, field in ipairs(opts.order) do if result[field] then @@ -147,13 +149,17 @@ function M:process(_, result) local window = self.window local display_size = Trans.util.display_size(self.buffer:lines(), opts.width) if window and window:is_valid() then - ---@diagnostic disable-next-line: assign-type-mismatch - display_size.width = opts.auto_resize and math.min(opts.width, display_size.width + opts.padding) or nil + if opts.auto_resize then + display_size.width = math.min(opts.width, display_size.width + opts.padding) + else + display_size.width = nil + end window:resize(display_size) + else window = self:init_window { height = math.min(opts.height, display_size.height), - width = math.min(opts.width, display_size.width), + width = math.min(opts.width, display_size.width + opts.padding), } end