chore: sync and leave bug to be fixed

This commit is contained in:
JuanZoran 2023-03-12 09:56:31 +08:00
parent cdb1ab847a
commit 27ef2c00b0
9 changed files with 132 additions and 83 deletions

View File

@ -1,6 +1,5 @@
local M = {} local M = {}
local baidu = require('Trans').conf.engines.baidu local baidu = require('Trans').conf.engines.baidu
local app_id = baidu.app_id local app_id = baidu.app_id
local app_passwd = baidu.app_passwd local app_passwd = baidu.app_passwd

View File

@ -1,7 +1,9 @@
return setmetatable({}, { return setmetatable({}, {
__index = function(t, k) __index = function(t, k)
local res, engine = pcall(require, [[Trans.backend.]] .. k) local res, engine = pcall(require, [[Trans.backend.]] .. k)
assert(res, [[No such Backend: ]] .. k) if not res then
error([[Fail to load backend: ]] .. k .. '\n ' .. engine)
end
t[k] = engine t[k] = engine
return engine return engine
end end

View File

@ -8,34 +8,33 @@ if vim.fn.has('nvim-0.9') == 1 then
end end
return { return {
theme = 'default', -- see lua/Trans/style/theme.lua dir = '$HOME/.vim/dict',
auto_play = true, strategy = {
dir = vim.fn.expand('$HOME/.vim/dict'),
strategy = {
frontend = 'hover', frontend = 'hover',
backend = '*', backend = '*',
}, },
backend = { backend = {
timeout = 2000, timeout = 2000,
}, },
frontend = { frontend = {
auto_play = true,
border = 'rounded',
animation = {
open = 'slid', -- 'fold', 'slid'
close = 'slid',
interval = 12,
},
title = title, -- need nvim-0.9
hover = { hover = {
title = title, -- need nvim-0.9
width = 37, width = 37,
height = 27, height = 27,
border = 'rounded',
keymap = { keymap = {
pageup = '[[', play = '_',
pagedown = ']]', pageup = '[[',
pin = '<leader>[', pagedown = ']]',
close = '<leader>]', pin = '<leader>[',
close = '<leader>]',
toggle_entry = '<leader>;', toggle_entry = '<leader>;',
play = '_',
},
animation = {
open = 'slid', -- 'fold', 'slid'
close = 'slid',
interval = 12,
}, },
auto_close_events = { auto_close_events = {
'InsertEnter', 'InsertEnter',
@ -53,13 +52,17 @@ return {
spinner = 'dots', -- see: /lua/Trans/style/spinner spinner = 'dots', -- see: /lua/Trans/style/spinner
}, },
}, },
-- or use emoji style = {
icon = { -- see lua/Trans/style/theme.lua
star = '', -- ⭐ theme = 'default', -- default | tokyonight | dracula
notfound = '', -- ❔ -- or use emoji
yes = '', -- ✔️ icon = {
no = '', -- ❌ star = '', -- ⭐
cell = '', -- ■ | □ | ▇ | ▏ ▎ ▍ ▌ ▋ ▊ ▉ █ notfound = '', -- ❔
yes = '', -- ✔️
no = '', -- ❌
cell = '', -- ■ | □ | ▇ | ▏ ▎ ▍ ▌ ▋ ▊ ▉ █
},
}, },
} }

14
lua/Trans/core/define.lua Normal file
View File

@ -0,0 +1,14 @@
return {
modes = {
'normal',
'visual',
'input',
},
backends = {
'offline',
'baidu',
},
frontends = {
'hover',
}
}

View File

@ -1,3 +0,0 @@
return {
}

View File

@ -1,26 +1,69 @@
local function set_backend_opts(conf) local function set_strategy_opts(conf)
local strategys = conf.strategy local define = require('Trans').define
local all_modes = define.modes
local all_backends = vim.tbl_keys(conf.engines)
local backend = strategys.backend -- FIXME : Wrong backend baidu
if type(backend) == 'string' then local function parse_backend(backend)
strategys.backend = backend == '*' and backend or { backend } if type(backend) == 'string' then
return backend == '*' and all_backends or { backend }
end
return backend
end end
local global_strategy = conf.strategy
global_strategy.backend = parse_backend(global_strategy.backend)
for i = 2, #conf.backends do
local name = conf.backends[i] local meta = {
if not strategys[name] then __index = function(tbl, key)
strategys[name] = { tbl[key] = global_strategy[key]
frontend = strategys.frontend, return tbl[key]
backend = strategys.backend, end
} }
for _, mode in ipairs(all_modes) do
if not global_strategy[mode] then
global_strategy[mode] = setmetatable({}, meta)
else
if mode.backend then
mode.backend = parse_backend(mode.backend)
end
setmetatable(mode, meta)
end end
end end
end end
local function set_frontend_opts(conf)
local all_frontends = require('Trans').define.frontends
local global_frontend_opts = conf.frontend
local meta = {
__index = function(tbl, key)
tbl[key] = global_frontend_opts[key]
return tbl[key]
end
}
for _, frontend in ipairs(all_frontends) do
local frontend_opts = global_frontend_opts[frontend]
if not frontend_opts then
global_frontend_opts[frontend] = setmetatable({}, meta)
else
setmetatable(frontend_opts, meta)
end
end
end
local function define_highlights(conf) local function define_highlights(conf)
local set_hl = vim.api.nvim_set_hl local set_hl = vim.api.nvim_set_hl
local highlights = require('Trans.style.theme')[conf.theme] local highlights = require('Trans.style.theme')[conf.style.theme]
for hl, opt in pairs(highlights) do for hl, opt in pairs(highlights) do
set_hl(0, hl, opt) set_hl(0, hl, opt)
end end
@ -32,7 +75,9 @@ return function(opts)
M.conf = vim.tbl_deep_extend('force', M.conf, opts) M.conf = vim.tbl_deep_extend('force', M.conf, opts)
end end
local conf = M.conf local conf = M.conf
conf.dir = vim.fn.expand(conf.dir)
set_backend_opts(conf) set_strategy_opts(conf)
set_frontend_opts(conf)
define_highlights(conf) define_highlights(conf)
end end

View File

@ -1,32 +1,26 @@
local Trans = require('Trans') local Trans = require('Trans')
local util = Trans.util local util = Trans.util
local backends = Trans.conf.backends vim.pretty_print(Trans.conf)
local function new_data(opts) local function new_data(opts)
opts = opts or {} opts = opts or {}
local method = opts.method or ({ local mode = opts.method or ({
n = 'normal', n = 'normal',
v = 'visual', v = 'visual',
})[vim.api.nvim_get_mode().mode] })[vim.api.nvim_get_mode().mode]
local str = util.get_str(method) local str = util.get_str(mode)
if str == '' then return end if str == '' then return end
local strategy = Trans.conf.strategy[method] or Trans.conf.strategy local strategy = Trans.conf.strategy[mode]
local data = { local data = {
str = str, str = str,
method = method, mode = mode,
frontend = strategy.frontend, frontend = strategy.frontend,
backend = strategy.backend,
result = {}, result = {},
} }
local backend = strategy.backend
if type(backend) == 'string' then
backend = backend == '*' and backends or { backend }
end
data.backend = backend
if util.is_English(str) then if util.is_English(str) then
data.from = 'en' data.from = 'en'
data.to = 'zh' data.to = 'zh'
@ -38,14 +32,14 @@ local function new_data(opts)
end end
local function set_result(data) local function set_result(data)
local t_backend = require('Trans.backend') -- local t_backend = require('Trans.backend')
for _, name in data.backend do -- for _, name in rdata.backend do
local backend = t_backend[name] -- local backend = t_backend[name]
backend.query(data) -- backend.query(data)
if backend.no_wait then -- if backend.no_wait then
end -- end
end -- end
require('Trans.backend').baidu.query(data) require('Trans.backend').baidu.query(data)
local thread = coroutine.running() local thread = coroutine.running()

View File

@ -32,9 +32,9 @@ function M.get_select()
end end
---Get Text which need to be translated ---Get Text which need to be translated
---@param method string 'n' | 'v' | 'i' ---@param mode string 'n' | 'v' | 'i'
---@return string ---@return string
function M.get_str(method) function M.get_str(mode)
return ({ return ({
normal = function() normal = function()
return fn.expand('<cword>') return fn.expand('<cword>')
@ -46,7 +46,7 @@ function M.get_str(method)
input = function() input = function()
return fn.input('请输入需要查询的单词:') return fn.input('请输入需要查询的单词:')
end, end,
})[method]() })[mode]()
end end
function M.is_English(str) function M.is_English(str)

View File

@ -21,13 +21,13 @@ end
--- INFO :Define plugin command --- INFO :Define plugin command
local M = require('Trans') local Trans = require('Trans')
local command = api.nvim_create_user_command local command = api.nvim_create_user_command
command('Translate', function() M.translate() end, { desc = ' 单词翻译', }) command('Translate', function() Trans.translate() end, { desc = ' 单词翻译', })
command('TransPlay', function() command('TransPlay', function()
local str = M.util.get_str(api.nvim_get_mode().mode) local str = Trans.util.get_str(api.nvim_get_mode().mode)
if str and str ~= '' and M.util.is_English(str) then if str and str ~= '' and Trans.util.is_English(str) then
str:play() str:play()
end end
end, { desc = ' 自动发音' }) end, { desc = ' 自动发音' })
@ -36,7 +36,7 @@ end, { desc = ' 自动发音' })
--- INFO :Parse online engines config file --- INFO :Parse online engines config file
local function parse_engine_file() local function parse_engine_file()
local path = M.conf.dir .. '/Trans.json' local path = Trans.conf.dir .. '/Trans.json'
local file = io.open(path, "r") local file = io.open(path, "r")
if file then if file then
@ -48,20 +48,15 @@ local function parse_engine_file()
end end
end end
M.conf.backends = { 'offline' }
M.engines = {}
local result = parse_engine_file() local result = parse_engine_file()
if result then if result then
local backends = M.conf.backends
local engines = M.engines
for name, opts in pairs(result) do for name, opts in pairs(result) do
if opts.enable then if not opts.enable then
backends[#backends + 1] = name result[name] = nil
engines[name] = opts
end end
end end
Trans.conf.engines = result
else
Trans.conf.engines = {}
end end
-- new_command('TranslateInput', function() M.translate { mode = 'i' } end, { desc = ' 搜索翻译', })