fix: fix cacha miss

This commit is contained in:
JuanZoran 2023-03-15 14:30:01 +08:00
parent fff23d4f41
commit c699aaba24
4 changed files with 24 additions and 18 deletions

View File

@ -112,11 +112,8 @@ end
function buffer:setline(nodes, one_index) function buffer:setline(nodes, one_index)
self:set('modifiable', true) self:set('modifiable', true)
---@diagnostic disable-next-line: cast-local-type, param-type-mismatch
one_index = one_index or self:line_count() + 1 one_index = one_index or self:line_count() + 1
if one_index == 2 and self[1] == '' then one_index = 1 end if one_index == 2 and self[1] == '' then one_index = 1 end
---@cast one_index integer
if type(nodes) == 'string' then if type(nodes) == 'string' then
fn.setbufline(self.bufnr, one_index, nodes) fn.setbufline(self.bufnr, one_index, nodes)
@ -167,18 +164,21 @@ buffer.__newindex = function(self, key, nodes)
end end
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 ---@nodiscard
---TransBuffer constructor ---TransBuffer constructor
---@return TransBuffer ---@return TransBuffer
function buffer.new() function buffer.new()
local new_buf = setmetatable({ local new_buf = setmetatable({}, buffer)
bufnr = api.nvim_create_buf(false, false), new_buf:init()
}, buffer)
new_buf:set('modifiable', false)
new_buf:set('filetype', 'Trans')
new_buf:set('buftype', 'nofile')
return new_buf return new_buf
end end

View File

@ -59,7 +59,7 @@ return {
---@type string -- TODO :support replace with {{special word}} ---@type string -- TODO :support replace with {{special word}}
fallback_message = '翻译超时或没有找到相关的翻译', fallback_message = '翻译超时或没有找到相关的翻译',
auto_resize = true, auto_resize = true,
padding = 10, -- only work when auto_resize is true padding = 10, -- padding for hover window width
keymaps = { keymaps = {
play = '_', play = '_',
pageup = '[[', pageup = '[[',

View File

@ -64,11 +64,11 @@ end
---@return TransResult? ---@return TransResult?
function M:get_available_result() function M:get_available_result()
local result = self.result local result = self.result
local backend = self.backends
for _, name in ipairs(backend) do for _, backend in ipairs(self.backends) do
if result[name] then if result[backend.name] then
return result[name] ---@diagnostic disable-next-line: return-type-mismatch
return result[backend.name]
end end
end end
end end

View File

@ -137,6 +137,8 @@ function M:process(_, result)
-- local it, t, f = node.item, node.text, node.format -- local it, t, f = node.item, node.text, node.format
-- self.buffer:setline(it('hello', 'MoreMsg')) -- self.buffer:setline(it('hello', 'MoreMsg'))
local opts = self.opts local opts = self.opts
if not self.buffer:is_valid() then self.buffer:init() end
for _, field in ipairs(opts.order) do for _, field in ipairs(opts.order) do
if result[field] then if result[field] then
@ -147,13 +149,17 @@ function M:process(_, result)
local window = self.window local window = self.window
local display_size = Trans.util.display_size(self.buffer:lines(), opts.width) local display_size = Trans.util.display_size(self.buffer:lines(), opts.width)
if window and window:is_valid() then if window and window:is_valid() then
---@diagnostic disable-next-line: assign-type-mismatch if opts.auto_resize then
display_size.width = opts.auto_resize and math.min(opts.width, display_size.width + opts.padding) or nil display_size.width = math.min(opts.width, display_size.width + opts.padding)
else
display_size.width = nil
end
window:resize(display_size) window:resize(display_size)
else else
window = self:init_window { window = self:init_window {
height = math.min(opts.height, display_size.height), 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 end