chore: backup progress, begin to rewrite version 3

This commit is contained in:
JuanZoran
2023-07-10 21:19:55 +08:00
parent 5845a40d94
commit c2e56f7769
7 changed files with 377 additions and 94 deletions

View File

@ -2,99 +2,166 @@ local Trans = require 'Trans'
---@class TransBackend
---@field no_wait? boolean whether need to wait for the result
---@field name string @backend name
---@field name_zh string @backend name in Chinese
---@field name string
---@field display_text string?
---@field conf table? @User specific config
---@class TransOnlineBackend: TransBackend
---@class TransBackendOnline: TransBackend
---@field uri string @request uri
---@field method 'get' | 'post' @request method
---@field formatter fun(body: table, data: TransData): TransResult|false|nil @formatter
---@field get_query fun(data: TransData): table<string, string> @get query
---@field header? table<string, string> | fun(data: TransData): table<string, string> @request header
---@field debug? fun(body: table?) @debug
---@field get_query fun(data: TransData): table<string, any> @get query table
---@class TransOfflineBackend: TransBackend
---@class TransBackendOffline: TransBackend
---@field query fun(data: TransData)
---@field query_field string[] @query field
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
---@class TransBackendCore
local M = {
all_name = all_name,
---@type table<string, TransBackend>
sources = {},
}
---Template method for online query
---@param data TransData @data
---@param backend TransOnlineBackend @backend
function M.do_query(data, backend)
local name = backend.name
local uri = backend.uri
local method = backend.method
local formatter = backend.formatter
local query = backend.get_query(data)
local header = type(backend.header) == 'function' and backend.header(data) or backend.header
local m_util = {}
local function handle(output)
local status, body = pcall(vim.json.decode, output.body)
if not status or not body then
if not Trans.conf.debug then
backend.debug(body)
data.trace[name] = output
end
data.result[name] = false
return
end
data.result[name] = formatter(body, data)
end
-- TODO :Implement all of utility functions
M.util = m_util
M.random_num = math.random(bit.lshift(1, 15))
Trans.curl[method](uri, {
query = query,
callback = handle,
header = header,
})
-- Hook ?
end
---@class Trans
---@field backend TransBackends
---@field backend TransBackendCore
return setmetatable(M, {
__index = function(self, name)
---@type TransBackend
local backend = require('Trans.backend.' .. name)
backend.conf = user_conf[name]
if user_conf[name] then
for key, value in pairs(user_conf[name]) do
backend[key] = value
end
end
self[name] = backend
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
-- ---@param data TransData @data
-- ---@param backend TransOnlineBackend @backend
-- function M.do_query(data, backend)
-- local name = backend.name
-- local uri = backend.uri
-- local method = backend.method
-- local formatter = backend.formatter
-- local query = backend.get_query(data)
-- local header = type(backend.header) == 'function' and backend.header(data) or backend.header
-- local function handle(output)
-- local status, body = pcall(vim.json.decode, output.body)
-- if not status or not body then
-- if not Trans.conf.debug then
-- backend.debug(body)
-- data.trace[name] = output
-- end
-- data.result[name] = false
-- return
-- end
-- data.result[name] = formatter(body, data)
-- end
-- Trans.curl[method](uri, {
-- query = query,
-- callback = handle,
-- header = header,
-- })
-- -- Hook ?
-- end

View File

@ -0,0 +1,6 @@
local M = {}
-- TODO :
return M

View File

@ -184,11 +184,12 @@ window.__index = window
-- relative = relative,
-- }
---@class TransWindowOpts
local default_opts = {
enter = false,
winid = -1,
enter = false,
winid = -1,
---@type WindowOpts
win_opts = {
win_opts = {
-- INFO : ensured options
-- col
-- row
@ -201,14 +202,10 @@ local default_opts = {
focusable = true,
noautocmd = true,
},
animation = nil, ---@type table? Hover Window Animation
buffer = nil, ---@type TransBuffer attached buffer object
}
---@class TransWindowOpts
---@field buffer TransBuffer attached buffer object
---@field enter? boolean cursor should [enter] window when open,default: false
---@field win_opts WindowOpts window config [**When open**]
---@field animation? table? Hover Window Animation
---Create new window
---@param opts TransWindowOpts window config
---@return TransWindow