diff --git a/lua/Trans/backend/baidu.lua b/lua/Trans/backend/baidu.lua index 7b072f6..8386131 100644 --- a/lua/Trans/backend/baidu.lua +++ b/lua/Trans/backend/baidu.lua @@ -40,7 +40,7 @@ function M.query(data) -- TEST :whether multi result assert(#result == 1, 'multi result :' .. vim.inspect(result)) result = result[1] - data.result = { + data.result.baidu = { title = result.src, translation = result.dst, } @@ -48,7 +48,7 @@ function M.query(data) end end - data.result = false + data.result.baidu = false data.trace = res end diff --git a/lua/Trans/backend/offline.lua b/lua/Trans/backend/offline.lua index 8fad428..36d6c60 100644 --- a/lua/Trans/backend/offline.lua +++ b/lua/Trans/backend/offline.lua @@ -28,11 +28,8 @@ function M.query(data) limit = 1, })[1] - if res then - data.result = data.formatter(res) - end - + data.result.offline = res and data.formatter(res) or false return data end diff --git a/lua/Trans/core/conf.lua b/lua/Trans/core/conf.lua index 0adfcaa..42ca8ad 100644 --- a/lua/Trans/core/conf.lua +++ b/lua/Trans/core/conf.lua @@ -8,17 +8,17 @@ if vim.fn.has('nvim-0.9') == 1 then end return { - theme = 'default', -- see lua/Trans/style/theme.lua + theme = 'default', -- see lua/Trans/style/theme.lua auto_play = true, - dir = vim.fn.expand('$HOME/.vim/dict'), - strategy = { + dir = vim.fn.expand('$HOME/.vim/dict'), + strategy = { frontend = 'hover', backend = '*', }, - backend = { + backend = { timeout = 2000, }, - frontend = { + frontend = { hover = { title = title, -- need nvim-0.9 width = 37, @@ -54,12 +54,12 @@ return { }, }, -- or use emoji - icon = { - star = '', -- ⭐ + icon = { + star = '', -- ⭐ notfound = ' ', -- ❔ - yes = '✔', -- ✔️ - no = '', -- ❌ - cell = '■', -- ■ | □ | ▇ | ▏ ▎ ▍ ▌ ▋ ▊ ▉ █ + yes = '✔', -- ✔️ + no = '', -- ❌ + cell = '■', -- ■ | □ | ▇ | ▏ ▎ ▍ ▌ ▋ ▊ ▉ █ }, } diff --git a/lua/Trans/core/defines.lua b/lua/Trans/core/defines.lua new file mode 100644 index 0000000..b30bcbd --- /dev/null +++ b/lua/Trans/core/defines.lua @@ -0,0 +1,3 @@ +return { + +} diff --git a/lua/Trans/core/setup.lua b/lua/Trans/core/setup.lua index f7500fa..4b8fdcd 100644 --- a/lua/Trans/core/setup.lua +++ b/lua/Trans/core/setup.lua @@ -1,12 +1,38 @@ +local function set_backend_opts(conf) + local strategys = conf.strategy + + local backend = strategys.backend + if type(backend) == 'string' then + strategys.backend = backend == '*' and backend or { backend } + end + + for i = 2, #conf.backends do + local name = conf.backends[i] + if not strategys[name] then + strategys[name] = { + frontend = strategys.frontend, + backend = strategys.backend, + } + end + end +end + + +local function define_highlights(conf) + local set_hl = vim.api.nvim_set_hl + local highlights = require('Trans.style.theme')[conf.theme] + for hl, opt in pairs(highlights) do + set_hl(0, hl, opt) + end +end + return function(opts) local M = require('Trans') if opts then M.conf = vim.tbl_deep_extend('force', M.conf, opts) end + local conf = M.conf - local set_hl = vim.api.nvim_set_hl - local hls = require('Trans.style.theme')[M.conf.theme] - for hl, opt in pairs(hls) do - set_hl(0, hl, opt) - end + set_backend_opts(conf) + define_highlights(conf) end diff --git a/lua/Trans/core/translate.lua b/lua/Trans/core/translate.lua index 1f6d1c4..7f2920d 100644 --- a/lua/Trans/core/translate.lua +++ b/lua/Trans/core/translate.lua @@ -1,17 +1,14 @@ local Trans = require('Trans') local util = Trans.util -local backends = { - 'offline', - 'baidu', -} +local backends = Trans.conf.backends local function new_data(opts) opts = opts or {} local method = opts.method or ({ n = 'normal', v = 'visual', - })(vim.api.nvim_get_mode().mode) + })[vim.api.nvim_get_mode().mode] local str = util.get_str(method) if str == '' then return end @@ -21,6 +18,7 @@ local function new_data(opts) str = str, method = method, frontend = strategy.frontend, + result = {}, } local backend = strategy.backend @@ -40,6 +38,15 @@ local function new_data(opts) end local function set_result(data) + local t_backend = require('Trans.backend') + for _, name in data.backend do + local backend = t_backend[name] + backend.query(data) + if backend.no_wait then + + end + end + require('Trans.backend').baidu.query(data) local thread = coroutine.running() local resume = function() @@ -57,9 +64,10 @@ local function set_result(data) end local function render_window() - + end +-- HACK : Core process logic local function process(opts) Trans.translate = coroutine.wrap(process) diff --git a/lua/Trans/core/util.lua b/lua/Trans/core/util.lua index dc0695f..6032538 100644 --- a/lua/Trans/core/util.lua +++ b/lua/Trans/core/util.lua @@ -46,7 +46,7 @@ function M.get_str(method) input = function() return fn.input('请输入需要查询的单词:') end, - })(method)() + })[method]() end function M.is_English(str) diff --git a/lua/Trans/frontend/hover.lua b/lua/Trans/frontend/hover.lua index 4d2cada..a95a71d 100644 --- a/lua/Trans/frontend/hover.lua +++ b/lua/Trans/frontend/hover.lua @@ -2,6 +2,9 @@ local M = {} local api = vim.api +function M.process(data) + +end return M diff --git a/lua/Trans/health.lua b/lua/Trans/health.lua index 130d7a0..2ad3350 100644 --- a/lua/Trans/health.lua +++ b/lua/Trans/health.lua @@ -83,11 +83,11 @@ local function check_configure_file() end local function check() + check_database() check_neovim_version() + check_configure_file() check_plugin_dependencies() check_binary_dependencies() - check_database() - check_configure_file() end return { check = check, } diff --git a/plugin/Trans.lua b/plugin/Trans.lua index 05f78f3..44949a9 100644 --- a/plugin/Trans.lua +++ b/plugin/Trans.lua @@ -35,24 +35,33 @@ end, { desc = ' 自动发音' }) --- INFO :Parse online engines config file -local path = vim.fn.expand("$HOME/.vim/dict/Trans.json") -local file = io.open(path, "r") -if file then - local content = file:read("*a") - file:close() - local status, engines = pcall(vim.json.decode, content) - assert(status, 'Unable to parse json file: ' .. path) +local function parse_engine_file() + local path = M.conf.dir .. '/Trans.json' + local file = io.open(path, "r") - engines = engines or {} - for k, v in pairs(engines) do - if not v.enable then - engines[k] = nil - end + if file then + local content = file:read("*a") + local status, result = pcall(vim.json.decode, content) + file:close() + assert(status, 'Unable to parse json file: ' .. path) + return result end - - M.conf.engines = engines -else - M.conf.engines = {} end +M.conf.backends = { 'offline' } +M.engines = {} + +local result = parse_engine_file() +if result then + local backends = M.conf.backends + local engines = M.engines + for name, opts in pairs(result) do + if opts.enable then + backends[#backends + 1] = name + engines[name] = opts + end + end +end + + -- new_command('TranslateInput', function() M.translate { mode = 'i' } end, { desc = ' 搜索翻译', })