diff --git a/lua/Trans/core/buffer.lua b/lua/Trans/core/buffer.lua index 1cc6adc..fb4dbae 100644 --- a/lua/Trans/core/buffer.lua +++ b/lua/Trans/core/buffer.lua @@ -160,10 +160,11 @@ end ---@nodiscard ---TransBuffer constructor +---@param bufnr? integer buffer handle ---@return TransBuffer -function buffer.new() +function buffer.new(bufnr) local new_buf = setmetatable({}, buffer) - new_buf:init() + new_buf:init(bufnr) return new_buf end diff --git a/lua/Trans/core/frontend.lua b/lua/Trans/core/frontend.lua index 148831b..f4c3396 100644 --- a/lua/Trans/core/frontend.lua +++ b/lua/Trans/core/frontend.lua @@ -3,30 +3,6 @@ local conf = Trans.conf local frontend_opts = conf.frontend ----Setup frontend Keymaps ----@param frontend TransFrontend -local function set_frontend_keymap(frontend) - local set = vim.keymap.set - local keymap_opts = { - silent = true, - -- expr = true, - -- nowait = true, - } - - for action, key in pairs(frontend.opts.keymaps) do - set('n', key, function() - local instance = frontend.get_active_instance() - - if instance then - coroutine.wrap(instance.execute)(instance, action) - else - return key - end - end, keymap_opts) - end -end - - ---@class TransFrontend ---@field opts TransFrontendOpts ---@field get_active_instance fun():TransFrontend? @@ -34,6 +10,7 @@ end ---@field wait fun(self: TransFrontend): fun() Update wait status ---@field execute fun(action: string) @Execute action for frontend instance ---@field fallback fun() @Fallback method when no result +---@field setup? fun() @Setup method for frontend [optional] **NOTE: This method will be called when frontend is first used** ---@class Trans ---@field frontend TransFrontend @@ -47,8 +24,9 @@ return setmetatable({}, { frontend.opts = opts self[name] = frontend - set_frontend_keymap(frontend) - + if frontend.setup then + frontend.setup() + end return frontend end, }) diff --git a/lua/Trans/frontend/float.lua b/lua/Trans/frontend/float.lua index 671f91b..e8f3a73 100644 --- a/lua/Trans/frontend/float.lua +++ b/lua/Trans/frontend/float.lua @@ -1,20 +1,16 @@ --- local api = vim.api --- local conf = require('Trans').conf --- local buffer = require('Trans.buffer')() +local Trans = require 'Trans' --- local node = require("Trans.node") --- local t = node.text --- local it = node.item --- local f = node.format +---@class TransFloat +local M = {} + +function M.new() + -- TODO : + +end --- local engine_map = { --- baidu = '百度', --- youdao = '有道', --- iciba = 'iciba', --- offline = '本地', --- } +return M -- local function set_tag_hl(name, status) -- local hl = conf.float.tag[status] -- m_window:set_hl(name, { @@ -118,3 +114,10 @@ -- m_window:map(key, action[act]) -- end -- end + +-- local engine_map = { +-- baidu = '百度', +-- youdao = '有道', +-- iciba = 'iciba', +-- offline = '本地', +-- } diff --git a/lua/Trans/frontend/hover/init.lua b/lua/Trans/frontend/hover/init.lua index 60fd2a7..b72f8f3 100644 --- a/lua/Trans/frontend/hover/init.lua +++ b/lua/Trans/frontend/hover/init.lua @@ -17,6 +17,22 @@ local M = Trans.metatable('frontend.hover', { }) M.__index = M + +function M.setup() + local set = vim.keymap.set + for action, key in pairs(M.opts.keymaps) do + set('n', key, function() + local instance = M.get_active_instance() + + if instance then + coroutine.wrap(instance.execute)(instance, action) + else + return key + end + end) + end +end + ---Create a new hover instance ---@return TransHover new_instance function M.new() @@ -27,7 +43,6 @@ function M.new() } M.queue[#M.queue + 1] = new_instance - new_instance.buffer:deleteline(1) return setmetatable(new_instance, M) end @@ -186,8 +201,8 @@ function M:process(data) return end - local opts = self.opts local util = Trans.util + local opts = self.opts local buffer = self.buffer if opts.auto_play then @@ -210,19 +225,20 @@ function M:process(data) local lines = buffer:lines() + local valid = window and window:is_valid() 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) + 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 valid then window:resize { width = width, height = height } else window = self:init_window {