refactor: remove auto setup frontend keymap
This commit is contained in:
parent
82fa2a008a
commit
a4dff90064
@ -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
|
||||
|
||||
|
@ -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,
|
||||
})
|
||||
|
@ -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 = '本地',
|
||||
-- }
|
||||
|
@ -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,8 +225,9 @@ function M:process(data)
|
||||
local lines = buffer:lines()
|
||||
|
||||
|
||||
local valid = window and window:is_valid()
|
||||
local width =
|
||||
window and window:is_valid() and
|
||||
valid and
|
||||
(opts.auto_resize and
|
||||
math.max(
|
||||
math.min(opts.width, util.display_width(lines) + opts.padding),
|
||||
@ -222,7 +238,7 @@ function M:process(data)
|
||||
|
||||
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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user