From af4bb42d59fcf44f6a49ba250fbf8e907dd6fb5e Mon Sep 17 00:00:00 2001 From: JuanZoran <1430359574@qq.com> Date: Wed, 15 Mar 2023 20:57:28 +0800 Subject: [PATCH] refactor: remove do_query function --- lua/.luarc.json | 5 +-- lua/Trans/core/backend.lua | 6 --- lua/Trans/core/buffer.lua | 11 ++--- lua/Trans/core/conf.lua | 8 +--- lua/Trans/core/data.lua | 2 +- lua/Trans/core/frontend.lua | 2 +- lua/Trans/core/translate.lua | 49 ++++++++++------------ lua/Trans/frontend/hover/init.lua | 70 +++++++++++++++---------------- 8 files changed, 66 insertions(+), 87 deletions(-) diff --git a/lua/.luarc.json b/lua/.luarc.json index 8ae3698..74e1772 100644 --- a/lua/.luarc.json +++ b/lua/.luarc.json @@ -6,8 +6,7 @@ ], "Lua.diagnostics.globals": [ "vim", - "user_conf", - "default_conf" + "user_conf" ], "Lua.workspace.checkThirdParty": false -} \ No newline at end of file +} diff --git a/lua/Trans/core/backend.lua b/lua/Trans/core/backend.lua index 50a9801..9c7750c 100644 --- a/lua/Trans/core/backend.lua +++ b/lua/Trans/core/backend.lua @@ -3,7 +3,6 @@ local Trans = require('Trans') ---@class TransBackend ---@field query fun(data: TransData)---@async ----@field opts TransBackendOpts ---@field no_wait? boolean whether need to wait for the result ---@field name string @backend name @@ -21,9 +20,6 @@ if file then end -local default_opts = conf.backend.default -default_opts.__index = default_opts - ---@class Trans ---@field backend table return setmetatable({}, { @@ -36,8 +32,6 @@ return setmetatable({}, { backend = self[name] end - backend.opts = setmetatable(conf.backend[name] or {}, default_opts) - local private_opts = result[name] if private_opts then for k, v in pairs(private_opts) do diff --git a/lua/Trans/core/buffer.lua b/lua/Trans/core/buffer.lua index 1d8fc71..ec6ab1c 100644 --- a/lua/Trans/core/buffer.lua +++ b/lua/Trans/core/buffer.lua @@ -5,12 +5,13 @@ local api, fn = vim.api, vim.fn ---@field [number] string buffer[line] content local buffer = {} +-- INFO : corountine can't invoke C function ---Clear all content in buffer -function buffer:wipe() - print('begin') - api.nvim_buf_set_lines(self.bufnr, 0, -1, false, {}) - print('end') -end +-- function buffer:wipe() +-- print('begin') +-- api.nvim_buf_set_lines(self.bufnr, 0, -1, false, {}) +-- print('end') +-- end ---Delete buffer [_start, _end] line content [one index] ---@param _start? integer start line index diff --git a/lua/Trans/core/conf.lua b/lua/Trans/core/conf.lua index 03c9ca9..3428ae7 100644 --- a/lua/Trans/core/conf.lua +++ b/lua/Trans/core/conf.lua @@ -25,13 +25,6 @@ return { backend = '*', }, }, - ---@type table fallback backend for mode - backend = { - ---@class TransBackendOpts -- TODO :More core options - default = { - timeout = 2000, - }, - }, ---@type table frontend options frontend = { ---@class TransFrontendOpts @@ -47,6 +40,7 @@ return { close = 'slid', interval = 12, }, + timeout = 2000, }, ---@class TransHoverOpts : TransFrontendOpts hover = { diff --git a/lua/Trans/core/data.lua b/lua/Trans/core/data.lua index 5fad39d..b79f282 100644 --- a/lua/Trans/core/data.lua +++ b/lua/Trans/core/data.lua @@ -7,7 +7,7 @@ local Trans = require('Trans') ---@field is_word boolean @Is the str a word ---@field str string @The original string ---@field mode string @The mode of the str ----@field result table @The result of the translation +---@field result table @The result of the translation ---@field frontend TransFrontend ---@field backends table local M = {} diff --git a/lua/Trans/core/frontend.lua b/lua/Trans/core/frontend.lua index a588bf7..920a134 100644 --- a/lua/Trans/core/frontend.lua +++ b/lua/Trans/core/frontend.lua @@ -27,7 +27,7 @@ end ---@field opts TransFrontendOpts ---@field get_active_instance fun():TransFrontend? ---@field process fun(self: TransFrontend, data: TransData, result: TransResult) ----@field wait fun(self: TransFrontend, result: TransResult, name: string, timeout: integer) +---@field wait fun(self: TransFrontend): fun() Update wait status ---@field execute fun(action: string) @Execute action for frontend instance ---@class Trans diff --git a/lua/Trans/core/translate.lua b/lua/Trans/core/translate.lua index 4c45377..d0c955c 100644 --- a/lua/Trans/core/translate.lua +++ b/lua/Trans/core/translate.lua @@ -13,36 +13,28 @@ local function init_opts(opts) end ----To Query All Backends ----@param data TransData ----@return TransResult? @return nil if no result -local function do_query(data) - -- HACK :Rewrite this function to support multi requests - - ---@type TransFrontend - local frontend = data.frontend - local result = data.result - - - for _, backend in ipairs(data.backends) do - ---@cast backend TransBackend - local name = backend.name - if backend.no_wait then +---@type table +local strategy = { + fallback = function(data, update) + local result = data.result + for _, backend in ipairs(data.backends) do + ---@cast backend TransBackend + local name = backend.name backend.query(data) - else - backend.query(data) - frontend:wait(result, name, backend.opts.timeout) - end - if type(result[name]) == 'table' then - ---@diagnostic disable-next-line: return-type-mismatch - return result[name] - else - result[name] = nil + if not backend.no_wait then + while result[name] == nil do + update() + end + end + + if type(result[name]) == 'table' then + return result[name] + end end end -end +} -- HACK : Core process logic @@ -62,13 +54,14 @@ local function process(opts) end end - local data = Trans.data.new(opts) - local result = do_query(data) + local frontend = data.frontend + local result = strategy[Trans.conf.query](data, frontend:wait()) if not result then return end + Trans.cache[data.str] = data - data.frontend:process(data, result) + frontend:process(data, result) end diff --git a/lua/Trans/frontend/hover/init.lua b/lua/Trans/frontend/hover/init.lua index 6fc9a95..2410adb 100644 --- a/lua/Trans/frontend/hover/init.lua +++ b/lua/Trans/frontend/hover/init.lua @@ -92,58 +92,56 @@ function M:init_window(opts) return self.window end ----Wait for data ----@param tbl table @table to be checked ----@param name string @key to be checked ----@param timeout number @timeout for waiting -function M:wait(tbl, name, timeout) - local opts = self.opts - local width = opts.width - local spinner = Trans.style.spinner[self.opts.spinner] - local size = #spinner - local cell = self.opts.icon.cell +---Get Check function for waiting +---@return function +function M:wait() + local cur = 0 + local opts = self.opts + local buffer = self.buffer + local times = opts.width + local pause = Trans.util.pause + local cell = opts.icon.cell + local spinner = Trans.style.spinner[opts.spinner] + local size = #spinner + local interval = math.floor(opts.timeout / opts.width) - local function update_text(times) - return spinner[times % size + 1] .. (cell):rep(times) - end - - - self:init_window({ + self:init_window { height = 1, - width = width, - }) + width = times, + } - - local interval = math.floor(timeout / width) - local pause = Trans.util.pause - local buffer = self.buffer - for i = 1, width do - if tbl[name] ~= nil then break end - buffer[1] = update_text(i) + return function() + cur = cur + 1 + buffer[1] = spinner[cur % size + 1] .. (cell):rep(cur) buffer:add_highlight(1, 'MoreMsg') pause(interval) + return cur == times end - - -- FIXME : - -- buffer:wipe() - -- vim.api.nvim_buf_set_lines(buffer.bufnr, 1, -1, true, {}) - -- print('jklajsdk') - -- print(vim.fn.deletebufline(buffer.bufnr, 1)) - -- buffer:del() - buffer[1] = '' end +-- -- FIXME : +-- -- buffer:wipe() +-- -- vim.api.nvim_buf_set_lines(buffer.bufnr, 1, -1, true, {}) +-- -- print('jklajsdk') +-- -- print(vim.fn.deletebufline(buffer.bufnr, 1)) +-- -- buffer:del() +-- buffer[1] = '' + ---Display Result in hover window ---@param data TransData ---@param result TransResult ---@overload fun(result:TransResult) function M:process(data, result) - if self.pin then return end + local buffer = self.buffer + print(vim.fn.deletebufline(buffer.bufnr, 1)) + -- buffer[1] = '' -- local node = Trans.util.node -- local it, t, f = node.item, node.text, node.format -- self.buffer:setline(it('hello', 'MoreMsg')) + if self.pin then return end + local opts = self.opts - if not self.buffer:is_valid() then self.buffer:init() end + if not buffer:is_valid() then buffer:init() end if opts.auto_play then (data.from == 'en' and data.str or result.definition[1]):play() @@ -156,7 +154,7 @@ function M:process(data, result) end local window = self.window - local display_size = Trans.util.display_size(self.buffer:lines(), opts.width) + local display_size = Trans.util.display_size(buffer:lines(), opts.width) if window and window:is_valid() then if opts.auto_resize then display_size.width = math.min(opts.width, display_size.width + opts.padding)