Skip to content

Commit

Permalink
refactor: redirect Trans.conf
Browse files Browse the repository at this point in the history
  • Loading branch information
JuanZoran committed Jul 13, 2023
1 parent c2e56f7 commit 073e866
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 143 deletions.
141 changes: 36 additions & 105 deletions lua/Trans/core/backend.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
})
46 changes: 38 additions & 8 deletions lua/Trans/core/conf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,41 @@
---@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<TransMode, { frontend:string, backend:string | string[] }> 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<string, string>
default = {
auto_play = true,
query = 'fallback',
border = 'rounded',
title = vim.fn.has 'nvim-0.9' == 1 and {
{ '', 'TransTitleRound' },
{ ' 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'
Expand All @@ -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
Expand All @@ -56,8 +64,6 @@ return {
-- pin = '<leader>[',
-- close = '<leader>]',
-- toggle_entry = '<leader>;',

-- play = '_', -- Deprecated
},
---@type string[] auto close events
auto_close_events = {
Expand Down Expand Up @@ -104,8 +110,32 @@ return {
},
},
},



-- debug = true,
}






















-- TODO :
-- float = {
Expand Down
8 changes: 5 additions & 3 deletions lua/Trans/core/data.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
48 changes: 24 additions & 24 deletions lua/Trans/core/setup.lua
Original file line number Diff line number Diff line change
@@ -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



Expand All @@ -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
6 changes: 3 additions & 3 deletions lua/Trans/health.lua
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 073e866

Please sign in to comment.