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
lua/Trans

@ -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

@ -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 = '[[',

@ -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

@ -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