chore: add lsp doc
This commit is contained in:
parent
f63574026a
commit
34a01920a5
@ -39,7 +39,7 @@ function M.get_query(data)
|
||||
}
|
||||
end
|
||||
|
||||
---@overload fun(TransData): TransResult
|
||||
---@overload fun(body: table, data:TransData): TransResult
|
||||
---Query Using Baidu API
|
||||
---@param body table BaiduQuery Response
|
||||
---@return table|false
|
||||
|
@ -1,4 +1,4 @@
|
||||
---@class Offline: TransBackend
|
||||
---@class TransOfflineBackend
|
||||
local M = {
|
||||
name = "offline",
|
||||
no_wait = true,
|
||||
@ -175,4 +175,6 @@ function M.formatter(res)
|
||||
return res
|
||||
end
|
||||
|
||||
---@class TransBackends
|
||||
---@field offline TransOfflineBackend
|
||||
return M
|
||||
|
@ -20,44 +20,41 @@ local conf = Trans.conf
|
||||
local path = conf.dir .. '/Trans.json'
|
||||
local file = io.open(path, "r")
|
||||
|
||||
local result = {}
|
||||
|
||||
local user_conf = {}
|
||||
if file then
|
||||
local content = file:read("*a")
|
||||
result = vim.json.decode(content) or result
|
||||
user_conf = vim.json.decode(content) or user_conf
|
||||
file:close()
|
||||
end
|
||||
|
||||
|
||||
local all_name = {}
|
||||
local backend_order = conf.backend_order or vim.tbl_keys(result)
|
||||
local backend_order = conf.backend_order or vim.tbl_keys(user_conf)
|
||||
for _, name in ipairs(backend_order) do
|
||||
if not result[name].disable then
|
||||
if not user_conf[name].disable and user_conf[name] then
|
||||
all_name[#all_name + 1] = name
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
---@class TransBackends
|
||||
---@field all_name string[] all backend names
|
||||
|
||||
---@class Trans
|
||||
---@field backend table<string, TransBackend>
|
||||
---@field backend TransBackends
|
||||
return setmetatable({
|
||||
all_name = all_name,
|
||||
}, {
|
||||
__index = function(self, name)
|
||||
---@type TransBackend
|
||||
local backend = require('Trans.backend.' .. name)
|
||||
if backend then
|
||||
self[name] = backend
|
||||
else
|
||||
backend = self[name]
|
||||
end
|
||||
|
||||
local private_opts = result[name]
|
||||
if private_opts then
|
||||
for field, value in pairs(private_opts) do
|
||||
backend[field] = value
|
||||
end
|
||||
|
||||
for key, value in pairs(user_conf[name] or {}) do
|
||||
backend[key] = value
|
||||
end
|
||||
|
||||
self[name] = backend
|
||||
return backend
|
||||
end
|
||||
})
|
||||
|
@ -1,12 +1,3 @@
|
||||
local title
|
||||
if vim.fn.has('nvim-0.9') == 1 then
|
||||
title = {
|
||||
{ '', 'TransTitleRound' },
|
||||
{ ' Trans', 'TransTitle' },
|
||||
{ '', 'TransTitleRound' },
|
||||
}
|
||||
end
|
||||
|
||||
---@class Trans
|
||||
---@field conf TransConf
|
||||
|
||||
@ -33,7 +24,11 @@ return {
|
||||
---@field keymaps table<string, string>
|
||||
default = {
|
||||
border = 'rounded',
|
||||
title = title, -- need nvim-0.9+
|
||||
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 = {
|
||||
|
@ -30,9 +30,10 @@ end
|
||||
---@class TransFrontend
|
||||
---@field opts TransFrontendOpts
|
||||
---@field get_active_instance fun():TransFrontend?
|
||||
---@field process fun(self: TransFrontend, data: TransData, result: TransResult)
|
||||
---@field process fun(self: TransFrontend, data: TransData)
|
||||
---@field wait fun(self: TransFrontend): fun() Update wait status
|
||||
---@field execute fun(action: string) @Execute action for frontend instance
|
||||
---@field fallback fun() @Fallback method when no result
|
||||
|
||||
---@class Trans
|
||||
---@field frontend TransFrontend
|
||||
|
75
lua/Trans/core/immersive.lua
Normal file
75
lua/Trans/core/immersive.lua
Normal file
@ -0,0 +1,75 @@
|
||||
local function trans()
|
||||
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
|
||||
local paragraphs = {}
|
||||
|
||||
-- TODO : trim empty lines in the beginning and the end
|
||||
for index, line in ipairs(lines) do
|
||||
if line:match('%S+') then
|
||||
table.insert(paragraphs, { index - 1, line })
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
local Trans = require('Trans')
|
||||
local baidu = Trans.backend.baidu
|
||||
---@cast baidu Baidu
|
||||
|
||||
for _, line in ipairs(paragraphs) do
|
||||
local query = baidu.get_query({
|
||||
str = line[2],
|
||||
from = "en",
|
||||
to = "zh",
|
||||
})
|
||||
|
||||
Trans.curl.get(baidu.uri, {
|
||||
query = query,
|
||||
callback = function(output)
|
||||
-- vim.print(output)
|
||||
local body = output.body
|
||||
local status, ret = pcall(vim.json.decode, body)
|
||||
assert(status and ret, "Failed to parse json:" .. vim.inspect(body))
|
||||
local result = ret.trans_result
|
||||
assert(result, "Failed to get result: " .. vim.inspect(ret))
|
||||
|
||||
|
||||
result = result[1]
|
||||
line.translation = result.dst
|
||||
end,
|
||||
})
|
||||
end
|
||||
|
||||
local ns = vim.api.nvim_create_namespace("Trans")
|
||||
for _, line in ipairs(paragraphs) do
|
||||
local index = line[1]
|
||||
local co = coroutine.running()
|
||||
local times = 0
|
||||
while not line.translation do
|
||||
vim.defer_fn(function()
|
||||
coroutine.resume(co)
|
||||
end, 100)
|
||||
|
||||
print('waitting' .. ('.'):rep(times))
|
||||
times = times + 1
|
||||
-- if times == 10 then break end
|
||||
coroutine.yield()
|
||||
end
|
||||
|
||||
|
||||
local translation = line.translation
|
||||
print(translation, index)
|
||||
Trans.util.main_loop(function()
|
||||
vim.api.nvim_buf_set_extmark(0, ns, index, #line[2], {
|
||||
virt_lines = {
|
||||
{ { translation, "MoreMsg" } }
|
||||
},
|
||||
})
|
||||
end)
|
||||
|
||||
print('done')
|
||||
end
|
||||
-- TODO :双语翻译
|
||||
end
|
||||
|
||||
return function()
|
||||
coroutine.wrap(trans)()
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user