refactor: remove do_query function
This commit is contained in:
@@ -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<string, TransBackend>
|
||||
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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -25,13 +25,6 @@ return {
|
||||
backend = '*',
|
||||
},
|
||||
},
|
||||
---@type table<string, TransBackendOpts> 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 = {
|
||||
|
||||
@@ -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<string, TransResult|boolean> @The result of the translation
|
||||
---@field result table<string, TransResult|nil|false> @The result of the translation
|
||||
---@field frontend TransFrontend
|
||||
---@field backends table<string, TransBackend>
|
||||
local M = {}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<string, fun(data: TransData, update: fun()): TransResult|false?>
|
||||
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
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user