fix: better backend configuration file parser
This commit is contained in:
32
lua/Trans/core/backend.lua
Normal file
32
lua/Trans/core/backend.lua
Normal file
@@ -0,0 +1,32 @@
|
||||
local Trans = require('Trans')
|
||||
local M = Trans.metatable('backend')
|
||||
|
||||
--- INFO :Parse online engine keys config file
|
||||
local path = Trans.conf.dir .. '/Trans.json'
|
||||
local file = io.open(path, "r")
|
||||
|
||||
if file then
|
||||
local content = file:read("*a")
|
||||
local status, result = pcall(vim.json.decode, content)
|
||||
file:close()
|
||||
if not status then
|
||||
error('Unable to parse json file: ' .. path .. '\n' .. result)
|
||||
end
|
||||
|
||||
|
||||
|
||||
local default_opts = vim.deepcopy(Trans.conf.backend)
|
||||
for name, private_opts in pairs(result or {}) do
|
||||
local opts = vim.tbl_extend('keep', Trans.conf.backend[name] or {}, default_opts, private_opts)
|
||||
|
||||
local backend = M[name]
|
||||
for k, v in pairs(opts) do
|
||||
if not backend[k] then
|
||||
backend[k] = v
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
return M
|
||||
@@ -3,7 +3,7 @@ local Trans = require('Trans')
|
||||
local function set_strategy_opts(conf)
|
||||
local define = Trans.define
|
||||
local all_modes = define.modes
|
||||
local all_backends = vim.tbl_keys(conf.keys)
|
||||
local all_backends = define.backends
|
||||
|
||||
local function parse_backend(backend)
|
||||
if type(backend) == 'string' then
|
||||
@@ -62,6 +62,26 @@ local function set_frontend_opts(conf)
|
||||
end
|
||||
|
||||
|
||||
local function define_keymaps(conf)
|
||||
local set = vim.keymap.set
|
||||
local opts = { silent = true, expr = true }
|
||||
|
||||
|
||||
for _, name in ipairs(Trans.define.frontends) do
|
||||
for action, key in pairs(conf.frontend[name].keymap) do
|
||||
set('n', key, function()
|
||||
local frontend = Trans.frontend[name]
|
||||
if frontend.is_available() then
|
||||
frontend.actions[action]()
|
||||
else
|
||||
return key
|
||||
end
|
||||
end, opts)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
local function define_highlights(conf)
|
||||
local set_hl = vim.api.nvim_set_hl
|
||||
@@ -80,5 +100,6 @@ return function(opts)
|
||||
|
||||
set_strategy_opts(conf)
|
||||
set_frontend_opts(conf)
|
||||
define_keymaps(conf)
|
||||
define_highlights(conf)
|
||||
end
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
local Trans = require('Trans')
|
||||
local util = Trans.util
|
||||
|
||||
vim.pretty_print(Trans.conf)
|
||||
local function new_data(opts)
|
||||
opts = opts or {}
|
||||
local mode = opts.method or ({
|
||||
@@ -28,37 +27,41 @@ local function new_data(opts)
|
||||
data.from = 'zh'
|
||||
data.to = 'en'
|
||||
end
|
||||
|
||||
-- TODO : Check if the str is a word
|
||||
data.is_word = true
|
||||
|
||||
return data
|
||||
end
|
||||
|
||||
local function set_result(data)
|
||||
-- local t_backend = require('Trans').backend
|
||||
-- for _, name in rdata.backend do
|
||||
-- local backend = t_backend[name]
|
||||
-- backend.query(data)
|
||||
-- if backend.no_wait then
|
||||
local backend_list = data.backend
|
||||
local backends = Trans.backend
|
||||
|
||||
-- end
|
||||
-- end
|
||||
|
||||
-- Trans.backend.baidu.query(data)
|
||||
-- local thread = coroutine.running()
|
||||
-- local resume = function()
|
||||
-- coroutine.resume(thread)
|
||||
-- end
|
||||
local frontend = Trans.frontend[data.frontend]
|
||||
|
||||
-- local time = 0
|
||||
-- while data.result == nil do
|
||||
-- vim.defer_fn(resume, 400)
|
||||
-- time = time + 1
|
||||
-- print('waiting' .. ('.'):rep(time))
|
||||
-- coroutine.yield()
|
||||
-- end
|
||||
-- vim.pretty_print(data)
|
||||
local function do_query(name)
|
||||
local backend = backends[name]
|
||||
if backend.no_wait then
|
||||
backend.query(data)
|
||||
if type(data.result[name]) == 'table' then
|
||||
return
|
||||
end
|
||||
else
|
||||
backend.query(data)
|
||||
frontend.wait(data.result, name, backend.timeout)
|
||||
end
|
||||
end
|
||||
|
||||
for _, name in ipairs(backend_list) do
|
||||
do_query(name)
|
||||
end
|
||||
end
|
||||
|
||||
local function render_window()
|
||||
|
||||
local function render_window(data)
|
||||
-- TODO
|
||||
print('begin to render window')
|
||||
end
|
||||
|
||||
-- HACK : Core process logic
|
||||
@@ -71,7 +74,7 @@ local function process(opts)
|
||||
set_result(data)
|
||||
if data.result == false then return end
|
||||
|
||||
render_window()
|
||||
render_window(data)
|
||||
end
|
||||
|
||||
return coroutine.wrap(process)
|
||||
|
||||
@@ -36,18 +36,16 @@ end
|
||||
---@param mode string 'n' | 'v' | 'i'
|
||||
---@return string
|
||||
function M.get_str(mode)
|
||||
return ({
|
||||
normal = function()
|
||||
return fn.expand('<cword>')
|
||||
end,
|
||||
visual = function()
|
||||
api.nvim_input('<ESC>')
|
||||
return M.get_select()
|
||||
end,
|
||||
input = function()
|
||||
return fn.input('请输入需要查询的单词:')
|
||||
end,
|
||||
})[mode]()
|
||||
if mode == 'n' or mode == 'normal' then
|
||||
return fn.expand('<cword>')
|
||||
elseif mode == 'v' or mode == 'visual' then
|
||||
api.nvim_input('<ESC>')
|
||||
return M.get_select()
|
||||
elseif mode == 'input' then
|
||||
return fn.expand('<cword>')
|
||||
else
|
||||
error('Unsupported mode' .. mode)
|
||||
end
|
||||
end
|
||||
|
||||
function M.is_English(str)
|
||||
|
||||
Reference in New Issue
Block a user