From 073e8667b2e9cd02c3ea49bcc417884be4564a38 Mon Sep 17 00:00:00 2001 From: JuanZoran <1430359574@qq.com> Date: Thu, 13 Jul 2023 10:15:38 +0800 Subject: [PATCH] refactor: redirect Trans.conf --- lua/Trans/core/backend.lua | 141 ++++++++++--------------------------- lua/Trans/core/conf.lua | 46 +++++++++--- lua/Trans/core/data.lua | 8 ++- lua/Trans/core/setup.lua | 48 ++++++------- lua/Trans/health.lua | 6 +- 5 files changed, 106 insertions(+), 143 deletions(-) diff --git a/lua/Trans/core/backend.lua b/lua/Trans/core/backend.lua index ab85303..922146c 100644 --- a/lua/Trans/core/backend.lua +++ b/lua/Trans/core/backend.lua @@ -28,111 +28,7 @@ local M = { local m_util = {} - - --- TODO :Implement all of utility functions - - -M.util = m_util -M.random_num = math.random(bit.lshift(1, 15)) - - ----@class Trans ----@field backend TransBackendCore -return setmetatable(M, { - __index = function(self, name) - ---@type TransBackend - local backend = require('Trans.backend.' .. name) - backend.conf = user_conf[name] - - self.sources[name] = backend - return backend - end, -}) - - --- local new = (function() --- ---@class TransBackend --- local mt = { --- ---State hooks --- ---@param self TransBackend --- ---@param state TransState @state name --- ---@param hook fun() hook function --- ---@param event? TransEvents --- register = function(self, state, hook, event) --- table.insert(self.on[state][event], hook) --- end, - --- ---Update state and Notify hooks --- ---@param self TransBackend --- ---@param newstate TransState @state name --- update = function(self, newstate) --- -- Enter --- for _, hook in ipairs(self.on[newstate].enter) do --- hook() --- end - --- -- Leave --- for _, hook in ipairs(self.on[self.state].leave) do --- hook() --- end - --- -- Change --- for _, hook in ipairs(self.on[self.state].change) do --- hook() --- end - --- for _, hook in ipairs(self.on[newstate].change) do --- hook() --- end - --- self.state = newstate --- end, --- } --- mt.__index = mt - --- ---TransBackend Constructor --- ---@param origin TransBackendDefinition --- ---@return TransBackend --- return function(origin) --- origin.on = vim.defaulttable() --- origin.state = origin.state or 'IDLE' --- ---@cast origin TransBackend --- return setmetatable(origin, mt) --- end --- end)() - --- local conf = Trans.conf --- --- INFO :Parse online engine keys config file --- local path = conf.dir .. '/Trans.json' --- local file = io.open(path, 'r') - - --- local user_conf = {} --- if file then --- local content = file:read '*a' --- user_conf = vim.json.decode(content) or user_conf --- file:close() --- end - --- local all_name = { --- 'offline', -- default backend --- } - --- for _, config in ipairs(user_conf) do --- if not config.disable then --- all_name[#all_name + 1] = config.name --- user_conf[config.name] = config --- end --- end - --- ---@class TransBackends --- ---@field all_name string[] all backend names --- local M = { --- all_name = all_name, --- } - --- ---Template method for online query +-- INFO :Template method for online query -- ---@param data TransData @data -- ---@param backend TransOnlineBackend @backend -- function M.do_query(data, backend) @@ -165,3 +61,38 @@ return setmetatable(M, { -- }) -- -- Hook ? -- end + + + + + + +-- TODO :Implement all of utility functions + + +M.util = m_util +M.random_num = math.random(bit.lshift(1, 15)) + +-- INFO :Parse configuration file +local path = Trans.conf.dir .. '/Trans.json' +local file = io.open(path, 'r') +local user_conf = {} +if file then + local content = file:read '*a' + user_conf = vim.json.decode(content) or user_conf + file:close() +end +-- WARNING : [Breaking change] 'Trans.json' should use json object instead of array + +---@class Trans +---@field backend TransBackendCore +return setmetatable(M, { + __index = function(self, name) + ---@type TransBackend + local backend = require('Trans.backend.' .. name) + backend.conf = user_conf[name] + + self.sources[name] = backend + return backend + end, +}) diff --git a/lua/Trans/core/conf.lua b/lua/Trans/core/conf.lua index 6f7c506..c92b5be 100644 --- a/lua/Trans/core/conf.lua +++ b/lua/Trans/core/conf.lua @@ -2,25 +2,34 @@ ---@field conf TransConf +---@alias TransMode 'visual' 'input' + ---@class TransConf return { ---@type string the directory for database file and password file dir = require 'Trans'.plugin_dir, - debug = true, - ---@type 'default' | 'dracula' | 'tokyonight' global Trans theme [see lua/Trans/style/theme.lua] + ---@type 'default' | 'dracula' | 'tokyonight' global Trans theme [@see lua/Trans/style/theme.lua] theme = 'default', -- default | tokyonight | dracula + ---@type table fallback strategy for mode strategy = { - ---@type { frontend:string, backend:string | string[] } fallback strategy for mode default = { frontend = 'hover', - backend = '*', + backend = { + 'offline', + -- 'youdao', + -- 'baidu', + } }, + -- input = { + -- visual = { + -- ... }, ---@type table frontend options frontend = { ---@class TransFrontendOpts ---@field keymaps table default = { + auto_play = true, query = 'fallback', border = 'rounded', title = vim.fn.has 'nvim-0.9' == 1 and { @@ -28,7 +37,6 @@ return { { ' Trans', 'TransTitle' }, { '', 'TransTitleRound' }, } or nil, -- need nvim-0.9+ - auto_play = true, ---@type {open: string | boolean, close: string | boolean, interval: integer} Hover Window Animation animation = { open = 'slid', -- 'fold', 'slid' @@ -46,7 +54,7 @@ return { ---@type string -- see: /lua/Trans/style/spinner spinner = 'dots', ---@type string - fallback_message = '{{notfound}} 翻译超时或没有找到相关的翻译', + fallback_message = '{{notfound}} {{error_message}}', auto_resize = true, split_width = 60, padding = 10, -- padding for hover window width @@ -56,8 +64,6 @@ return { -- pin = '[', -- close = ']', -- toggle_entry = ';', - - -- play = '_', -- Deprecated }, ---@type string[] auto close events auto_close_events = { @@ -104,8 +110,32 @@ return { }, }, }, + + + + -- debug = true, } + + + + + + + + + + + + + + + + + + + + -- TODO : -- float = { diff --git a/lua/Trans/core/data.lua b/lua/Trans/core/data.lua index b5d0cec..512913a 100644 --- a/lua/Trans/core/data.lua +++ b/lua/Trans/core/data.lua @@ -33,9 +33,11 @@ function M.new(opts) data.frontend = Trans.frontend[strategy.frontend].new() data.backends = {} - for i, name in ipairs(strategy.backend) do - data.backends[i] = Trans.backend[name] - end + + -- FIXME : + -- for i, name in ipairs(strategy.backend) do + -- data.backends[i] = Trans.backend[name] + -- end if Trans.util.is_english(str) then data.from = 'en' diff --git a/lua/Trans/core/setup.lua b/lua/Trans/core/setup.lua index f1cb675..10e7088 100644 --- a/lua/Trans/core/setup.lua +++ b/lua/Trans/core/setup.lua @@ -1,34 +1,34 @@ local Trans = require 'Trans' -local function set_strategy_opts(conf) - local all_backends = Trans.backend.all_name - local g_strategy = conf.strategy +-- local function set_strategy_opts(conf) +-- local all_backends = conf.frontend.default.enabled_backend +-- local g_strategy = conf.strategy - local function parse_backend(backend) - if type(backend) == 'string' then - return backend == '*' and all_backends or { backend } - end +-- local function parse_backend(backend) +-- if type(backend) == 'string' then +-- return backend == '*' and all_backends or { backend } +-- end - return backend - end +-- return backend +-- end - local default_strategy = g_strategy.default - default_strategy.backend = parse_backend(default_strategy.backend) - default_strategy.__index = default_strategy +-- local default_strategy = g_strategy.default +-- default_strategy.backend = parse_backend(default_strategy.backend) +-- default_strategy.__index = default_strategy - g_strategy.default = nil +-- g_strategy.default = nil - setmetatable(g_strategy, { - __index = function() - return default_strategy - end, - }) +-- setmetatable(g_strategy, { +-- __index = function() +-- return default_strategy +-- end, +-- }) - for _, strategy in pairs(g_strategy) do - strategy.backend = parse_backend(strategy.backend) - setmetatable(strategy, default_strategy) - end -end +-- for _, strategy in pairs(g_strategy) do +-- strategy.backend = parse_backend(strategy.backend) +-- setmetatable(strategy, default_strategy) +-- end +-- end @@ -50,6 +50,6 @@ return function(opts) local conf = Trans.conf conf.dir = vim.fn.expand(conf.dir) - set_strategy_opts(conf) + -- set_strategy_opts(conf) define_highlights(conf) end diff --git a/lua/Trans/health.lua b/lua/Trans/health.lua index e58ccc1..12cb1f4 100644 --- a/lua/Trans/health.lua +++ b/lua/Trans/health.lua @@ -1,9 +1,9 @@ local Trans = require 'Trans' local health, fn = vim.health, vim.fn -local ok = health.report_ok -local warn = health.report_warn -local error = health.report_error +local ok = health.ok or health.report_ok +local warn = health.warn or health.report_warn +local error = health.error or health.report_error local has = fn.has local executable = fn.executable