2023-03-11 00:24:48 +08:00
|
|
|
local Trans = require('Trans')
|
|
|
|
local util = Trans.util
|
2023-03-09 19:42:41 +08:00
|
|
|
|
2023-03-11 00:24:48 +08:00
|
|
|
local function new_data(opts)
|
2023-03-09 19:42:41 +08:00
|
|
|
opts = opts or {}
|
2023-03-12 09:56:31 +08:00
|
|
|
local mode = opts.method or ({
|
2023-03-11 00:24:48 +08:00
|
|
|
n = 'normal',
|
|
|
|
v = 'visual',
|
2023-03-11 11:32:13 +08:00
|
|
|
})[vim.api.nvim_get_mode().mode]
|
2023-03-11 00:24:48 +08:00
|
|
|
|
2023-03-12 09:56:31 +08:00
|
|
|
local str = util.get_str(mode)
|
2023-03-09 19:42:41 +08:00
|
|
|
if str == '' then return end
|
|
|
|
|
2023-03-12 09:56:31 +08:00
|
|
|
local strategy = Trans.conf.strategy[mode]
|
2023-03-09 19:42:41 +08:00
|
|
|
local data = {
|
|
|
|
str = str,
|
2023-03-12 09:56:31 +08:00
|
|
|
mode = mode,
|
2023-03-11 00:24:48 +08:00
|
|
|
frontend = strategy.frontend,
|
2023-03-12 09:56:31 +08:00
|
|
|
backend = strategy.backend,
|
2023-03-11 11:32:13 +08:00
|
|
|
result = {},
|
2023-03-09 19:42:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if util.is_English(str) then
|
|
|
|
data.from = 'en'
|
|
|
|
data.to = 'zh'
|
|
|
|
else
|
|
|
|
data.from = 'zh'
|
|
|
|
data.to = 'en'
|
|
|
|
end
|
2023-03-12 15:18:27 +08:00
|
|
|
|
|
|
|
-- TODO : Check if the str is a word
|
|
|
|
data.is_word = true
|
|
|
|
|
2023-03-11 00:24:48 +08:00
|
|
|
return data
|
|
|
|
end
|
2023-03-09 19:42:41 +08:00
|
|
|
|
2023-03-11 00:24:48 +08:00
|
|
|
local function set_result(data)
|
2023-03-12 15:18:27 +08:00
|
|
|
local backend_list = data.backend
|
|
|
|
local backends = Trans.backend
|
|
|
|
|
|
|
|
|
|
|
|
local frontend = Trans.frontend[data.frontend]
|
2023-03-11 00:24:48 +08:00
|
|
|
|
2023-03-12 16:14:03 +08:00
|
|
|
-- HACK :Rewrite this function to support multi request
|
2023-03-12 15:18:27 +08:00
|
|
|
local function do_query(name)
|
|
|
|
local backend = backends[name]
|
|
|
|
if backend.no_wait then
|
|
|
|
backend.query(data)
|
|
|
|
else
|
|
|
|
backend.query(data)
|
|
|
|
frontend.wait(data.result, name, backend.timeout)
|
|
|
|
end
|
2023-03-12 16:14:03 +08:00
|
|
|
|
|
|
|
return type(data.result[name]) == "table"
|
2023-03-12 15:18:27 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
for _, name in ipairs(backend_list) do
|
2023-03-12 16:14:03 +08:00
|
|
|
if do_query(name) then
|
|
|
|
-- TODO : process data
|
|
|
|
break
|
|
|
|
end
|
2023-03-12 15:18:27 +08:00
|
|
|
end
|
|
|
|
end
|
2023-03-11 11:32:13 +08:00
|
|
|
|
2023-03-12 15:18:27 +08:00
|
|
|
local function render_window(data)
|
|
|
|
-- TODO
|
|
|
|
print('begin to render window')
|
2023-03-11 00:24:48 +08:00
|
|
|
end
|
|
|
|
|
2023-03-11 11:32:13 +08:00
|
|
|
-- HACK : Core process logic
|
2023-03-11 00:24:48 +08:00
|
|
|
local function process(opts)
|
|
|
|
Trans.translate = coroutine.wrap(process)
|
|
|
|
|
|
|
|
local data = new_data(opts)
|
|
|
|
if not data then return end
|
|
|
|
|
|
|
|
set_result(data)
|
2023-03-12 16:14:03 +08:00
|
|
|
local success = false
|
|
|
|
for _, v in pairs(data.result) do
|
|
|
|
if type(v) == "table" then
|
|
|
|
success = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2023-03-09 19:42:41 +08:00
|
|
|
|
2023-03-12 16:14:03 +08:00
|
|
|
vim.pretty_print(data)
|
|
|
|
if success == false then return end
|
2023-03-12 15:18:27 +08:00
|
|
|
render_window(data)
|
2023-03-09 19:42:41 +08:00
|
|
|
end
|
|
|
|
|
2023-03-09 22:55:04 +08:00
|
|
|
return coroutine.wrap(process)
|