fix: more lsp info comment and fix backend opts error
This commit is contained in:
parent
dab105b27f
commit
f47abd1691
@ -1,3 +1,9 @@
|
||||
---@class Baidu: TransBackend
|
||||
---@field uri string api uri
|
||||
---@field salt string
|
||||
---@field app_id string
|
||||
---@field app_passwd string
|
||||
---@field disable boolean
|
||||
local M = {
|
||||
uri = 'https://fanyi-api.baidu.com/api/trans/vip/translate',
|
||||
salt = tostring(math.random(bit.lshift(1, 15))),
|
||||
@ -6,6 +12,19 @@ local M = {
|
||||
|
||||
local Trans = require('Trans')
|
||||
|
||||
|
||||
---@class BaiduQuery
|
||||
---@field q string
|
||||
---@field from string
|
||||
---@field to string
|
||||
---@field appid string
|
||||
---@field salt string
|
||||
---@field sign string
|
||||
|
||||
|
||||
---Get content for query
|
||||
---@param data TransData
|
||||
---@return BaiduQuery
|
||||
function M.get_content(data)
|
||||
local tmp = M.app_id .. data.str .. M.salt .. M.app_passwd
|
||||
local sign = Trans.util.md5.sumhexa(tmp)
|
||||
@ -27,7 +46,9 @@ end
|
||||
-- status = 200
|
||||
-- }
|
||||
|
||||
|
||||
---@overload fun(TransData): TransResult
|
||||
---Query Using Baidu API
|
||||
---@param data TransData
|
||||
function M.query(data)
|
||||
if M.disable then
|
||||
data.result.baidu = false
|
||||
@ -37,7 +58,13 @@ function M.query(data)
|
||||
|
||||
local handle = function(res)
|
||||
local status, body = pcall(vim.json.decode, res.body)
|
||||
if status and body then
|
||||
if not status or not body then
|
||||
data.result.baidu = false
|
||||
data.trace = res
|
||||
return
|
||||
end
|
||||
|
||||
|
||||
local result = body.trans_result
|
||||
if result then
|
||||
-- TEST :whether multi result
|
||||
@ -47,14 +74,9 @@ function M.query(data)
|
||||
['title'] = result.src,
|
||||
[data.from == 'en' and 'translation' or 'definition'] = result.dst,
|
||||
}
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
data.result.baidu = false
|
||||
data.trace = res
|
||||
end
|
||||
|
||||
|
||||
Trans.curl.get(M.uri, {
|
||||
query = M.get_content(data),
|
||||
@ -62,6 +84,8 @@ function M.query(data)
|
||||
})
|
||||
end
|
||||
|
||||
---@class TransBackend
|
||||
---@field baidu Baidu
|
||||
return M
|
||||
|
||||
-- -- NOTE :free tts:
|
||||
|
@ -1,8 +1,10 @@
|
||||
---@class Offline: TransBackend
|
||||
local M = {
|
||||
no_wait = true,
|
||||
name = 'offline',
|
||||
}
|
||||
|
||||
|
||||
local db = require 'sqlite.db'
|
||||
vim.api.nvim_create_autocmd('VimLeavePre', {
|
||||
once = true,
|
||||
@ -13,6 +15,10 @@ vim.api.nvim_create_autocmd('VimLeavePre', {
|
||||
end
|
||||
})
|
||||
|
||||
|
||||
---@param data any
|
||||
---@return any
|
||||
---@overload fun(TransData): TransResult
|
||||
function M.query(data)
|
||||
if data.is_word == false or data.from == 'zh' then return end
|
||||
|
||||
@ -48,6 +54,7 @@ local function exist(str)
|
||||
return str and str ~= ''
|
||||
end
|
||||
|
||||
---@type (fun(res):any)[]
|
||||
local formatter = {
|
||||
title = function(res)
|
||||
local title = {
|
||||
@ -152,6 +159,9 @@ local formatter = {
|
||||
end,
|
||||
}
|
||||
|
||||
---Formater for TransResul
|
||||
---@param res TransResult
|
||||
---@return TransResult
|
||||
function M.formatter(res)
|
||||
for field, func in pairs(formatter) do
|
||||
res[field] = func(res)
|
||||
@ -160,4 +170,5 @@ function M.formatter(res)
|
||||
return res
|
||||
end
|
||||
|
||||
|
||||
return M
|
||||
|
@ -4,6 +4,8 @@ local Trans = require('Trans')
|
||||
---@class TransBackend
|
||||
---@field query fun(TransData): TransResult
|
||||
---@field opts TransBackendOpts
|
||||
---@field no_wait? boolean whether need to wait for the result
|
||||
---@field name string @backend name
|
||||
|
||||
|
||||
local conf = Trans.conf
|
||||
@ -22,7 +24,6 @@ end
|
||||
local default_opts = conf.backend.default
|
||||
default_opts.__index = default_opts
|
||||
|
||||
|
||||
---@class Trans
|
||||
---@field backend table<string, TransBackend>
|
||||
return setmetatable({}, {
|
||||
|
@ -92,12 +92,12 @@ end
|
||||
|
||||
---Add highlight to buffer
|
||||
---@param linenr number line number should be set[one index]
|
||||
---@param col_start number column start [zero index]
|
||||
---@param col_end number column end
|
||||
---@param hl_group string highlight group
|
||||
---@param col_start? number column start [zero index]
|
||||
---@param col_end? number column end
|
||||
---@param ns number? highlight namespace
|
||||
function buffer:add_highlight(linenr, hl_group, col_start, col_end, ns)
|
||||
linenr = linenr and linenr - 1 or -1
|
||||
linenr = linenr - 1 or -1
|
||||
col_start = col_start or 0
|
||||
api.nvim_buf_add_highlight(self.bufnr, ns or -1, hl_group, linenr, col_start, col_end or -1)
|
||||
end
|
||||
|
@ -48,7 +48,7 @@ return {
|
||||
interval = 12,
|
||||
},
|
||||
},
|
||||
---@class HoverOptions : TransFrontendOpts
|
||||
---@class TransHoverOpts : TransFrontendOpts
|
||||
hover = {
|
||||
---@type integer Max Width of Hover Window
|
||||
width = 37,
|
||||
|
@ -7,7 +7,7 @@ local Trans = require('Trans')
|
||||
---@field is_word boolean @Is the str a word
|
||||
---@field str string @The original string
|
||||
---@field mode string @The mode of the str
|
||||
---@field result table<string, TransResult> @The result of the translation
|
||||
---@field result table<string, TransResult|boolean> @The result of the translation
|
||||
---@field frontend TransFrontend
|
||||
---@field backends table<string, TransBackend>
|
||||
local M = {}
|
||||
|
@ -26,7 +26,7 @@ end
|
||||
---@class TransFrontend
|
||||
---@field opts TransFrontendOpts
|
||||
---@field get_active_instance fun():TransFrontend?
|
||||
---@field process fun(data: TransData, result: TransResult)
|
||||
---@field process fun(self: TransFrontend, data: TransData, result: TransResult)
|
||||
---@field wait fun(self: TransFrontend, result: TransResult, name: string, timeout: integer)
|
||||
---@field execute fun(action: string) @Execute action for frontend instance
|
||||
|
||||
|
@ -27,7 +27,7 @@ local function do_query(data)
|
||||
backend.query(data)
|
||||
else
|
||||
backend.query(data)
|
||||
frontend:wait(result, name, backend.timeout)
|
||||
frontend:wait(result, name, backend.opts.timeout)
|
||||
end
|
||||
|
||||
|
||||
|
@ -1,5 +1,3 @@
|
||||
---@class Trans
|
||||
---@field util TransUtil
|
||||
local Trans = require('Trans')
|
||||
|
||||
local fn, api = vim.fn, vim.api
|
||||
@ -79,4 +77,6 @@ function M.is_English(str)
|
||||
return true
|
||||
end
|
||||
|
||||
Trans.util = M
|
||||
---@class Trans
|
||||
---@field util TransUtil
|
||||
return M
|
||||
|
@ -169,11 +169,15 @@ end
|
||||
window.__index = window
|
||||
|
||||
|
||||
---@alias WindowOpts
|
||||
---|{style: string, border: string, focusable: boolean, noautocmd?: boolean, relative: string, width: integer, height: integer, col: integer, row: integer, zindex?: integer, title?: table | string}
|
||||
|
||||
|
||||
---@class TransWindowOpts
|
||||
local default_opts = {
|
||||
enter = false,
|
||||
winid = -1,
|
||||
---@type WindowOpts
|
||||
win_opts = {
|
||||
style = 'minimal',
|
||||
border = 'rounded',
|
||||
@ -182,6 +186,9 @@ local default_opts = {
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
---@class TransWindow
|
||||
---@field buffer TransBuffer attached buffer object
|
||||
---@field win_opts table window config [**When open**]
|
||||
|
@ -20,6 +20,8 @@ local strategy = {
|
||||
}
|
||||
|
||||
|
||||
---@class TransHover
|
||||
---@field execute fun(hover: TransHover, action: string)
|
||||
return function(hover, action)
|
||||
-- TODO :
|
||||
strategy[action](hover)
|
||||
|
@ -1,23 +1,13 @@
|
||||
local Trans = require('Trans')
|
||||
|
||||
---@class hover
|
||||
|
||||
---@class TransHover: TransFrontend
|
||||
---@field ns integer @namespace for hover window
|
||||
---@field buffer TransBuffer @buffer for hover window
|
||||
---@field window TransWindow @hover window
|
||||
---@field queue hover[] @hover queue for all hover instances
|
||||
---@field destroy_funcs table @functions to be executed when hover window is closed
|
||||
---@field opts table @options for hover window
|
||||
---@field opts.title string @title for hover window
|
||||
---@field opts.width number @width for hover window
|
||||
---@field opts.height number @height for hover window
|
||||
---@field opts.animation boolean @whether to use animation for hover window
|
||||
---@field opts.fallback_message string @message to be displayed when hover window is waiting for data
|
||||
---@field opts.spinner string @spinner to be displayed when hover window is waiting for data
|
||||
---@field opts.icon table @icons for hover window
|
||||
---@field opts.icon.notfound string @icon for not found
|
||||
---@field opts.icon.yes string @icon for yes
|
||||
---@field opts.icon.no string @icon for no
|
||||
---@field opts.icon.star string @icon for star
|
||||
---@field opts.icon.cell string @icon for cell used in waitting animation
|
||||
---@field queue TransHover[] @hover queue for all hover instances
|
||||
---@field destroy_funcs fun(hover:TransHover)[] @functions to be executed when hover window is closed
|
||||
---@field opts TransHoverOpts @options for hover window
|
||||
local M = Trans.metatable('frontend.hover', {
|
||||
ns = vim.api.nvim_create_namespace('TransHoverWin'),
|
||||
queue = {},
|
||||
@ -25,7 +15,7 @@ local M = Trans.metatable('frontend.hover', {
|
||||
M.__index = M
|
||||
|
||||
---Create a new hover instance
|
||||
---@return hover new_instance
|
||||
---@return TransHover new_instance
|
||||
function M.new()
|
||||
local new_instance = {
|
||||
buffer = Trans.buffer.new(),
|
||||
@ -37,7 +27,7 @@ function M.new()
|
||||
end
|
||||
|
||||
---Get the first active instances
|
||||
---@return hover
|
||||
---@return TransHover
|
||||
function M.get_active_instance()
|
||||
M.clear_dead_instance()
|
||||
return M.queue[1]
|
||||
@ -66,7 +56,8 @@ function M:destroy()
|
||||
end
|
||||
|
||||
---Init hover window
|
||||
---@param opts table? @window options: width, height
|
||||
---@param opts?
|
||||
---|{win_opts: WindowOpts,}
|
||||
---@return unknown
|
||||
function M:init_window(opts)
|
||||
opts = opts or {}
|
||||
@ -104,11 +95,11 @@ function M:wait(tbl, name, timeout)
|
||||
local size = #spinner
|
||||
local cell = self.opts.icon.cell
|
||||
|
||||
|
||||
local function update_text(times)
|
||||
return spinner[times % size + 1] .. (cell):rep(times)
|
||||
end
|
||||
|
||||
|
||||
self:init_window({
|
||||
win_opts = {
|
||||
height = 1,
|
||||
@ -130,6 +121,10 @@ function M:wait(tbl, name, timeout)
|
||||
buffer[1] = ''
|
||||
end
|
||||
|
||||
---Display Result in hover window
|
||||
---@param _ any
|
||||
---@param result TransResult
|
||||
---@overload fun(result:TransResult)
|
||||
function M:process(_, result)
|
||||
-- local node = Trans.util.node
|
||||
-- local it, t, f = node.item, node.text, node.format
|
||||
@ -158,6 +153,8 @@ function M:is_available()
|
||||
return self.buffer:is_valid() and self.window:is_valid()
|
||||
end
|
||||
|
||||
---@class TransFrontend
|
||||
---@field hover TransHover @hover frontend
|
||||
return M
|
||||
-- local cmd_id
|
||||
-- local next
|
||||
|
@ -1,7 +1,6 @@
|
||||
local node = require('Trans').util.node
|
||||
local it, t, f = node.item, node.text, node.format
|
||||
|
||||
|
||||
local function conjunction(text)
|
||||
return {
|
||||
it('', 'TransTitleRound'),
|
||||
@ -116,6 +115,11 @@ local strategy = {
|
||||
end,
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
---@class TransHover
|
||||
---@field load fun(hover: TransHover, result: TransResult, field: string)
|
||||
return function(hover, result, field)
|
||||
strategy[field](hover, result[field])
|
||||
end
|
||||
|
@ -5,10 +5,12 @@
|
||||
local function metatable(folder_name, origin)
|
||||
return setmetatable(origin or {}, {
|
||||
__index = function(tbl, key)
|
||||
local result = require(('Trans.%s.%s'):format(folder_name, key))
|
||||
local status, result = pcall(require, ('Trans.%s.%s'):format(folder_name, key))
|
||||
if status then
|
||||
tbl[key] = result
|
||||
return result
|
||||
end
|
||||
end
|
||||
})
|
||||
end
|
||||
|
||||
|
@ -1,3 +1,7 @@
|
||||
---@class TransUtil
|
||||
---@field md5 TransUtilMd5
|
||||
|
||||
---@class TransUtilMd5
|
||||
local md5 = {}
|
||||
-- local md5 = {
|
||||
-- _VERSION = "md5.lua 1.1.0",
|
||||
|
Loading…
x
Reference in New Issue
Block a user