fix: fix typo and add debug print option
This commit is contained in:
parent
e56267ea8c
commit
a6a5a33bff
@ -81,6 +81,44 @@ local field = {
|
||||
}
|
||||
|
||||
|
||||
|
||||
function M.debug(body)
|
||||
if not body then
|
||||
vim.notify('Unknown errors, nil body', vim.log.levels.ERROR)
|
||||
end
|
||||
local debug_msg = ({
|
||||
[101] = "缺少必填的参数,首先确保必填参数齐全,然后确认参数书写是否正确。",
|
||||
[102] = "不支持的语言类型",
|
||||
[103] = "翻译文本过长",
|
||||
[104] = "不支持的API类型",
|
||||
[105] = "不支持的签名类型",
|
||||
[106] = "不支持的响应类型",
|
||||
[107] = "不支持的传输加密类型",
|
||||
[108] = "应用ID无效,注册账号,登录后台创建应用和实例并完成绑定,可获得应用ID和应用密钥等信息",
|
||||
[109] = "batchLog格式不正确",
|
||||
[110] = "无相关服务的有效实例,应用没有绑定服务实例,可以新建服务实例,绑定服务实例。注:某些服务的翻译结果发音需要tts实例,需要在控制台创建语音合成实例绑定应用后方能使用。",
|
||||
[111] = "开发者账号无效",
|
||||
[113] = "q不能为空",
|
||||
[120] = "不是词,或未收录",
|
||||
[201] = "解密失败,可能为DES,BASE64,URLDecode的错误",
|
||||
[202] = "签名检验失败",
|
||||
[203] = "访问IP地址不在可访问IP列表",
|
||||
[205] = "请求的接口与应用的平台类型不一致,确保接入方式(Android SDK、IOS SDK、API)与创建的应用平台类型一致。如有疑问请参考入门指南",
|
||||
[206] = "因为时间戳无效导致签名校验失败",
|
||||
[207] = "重放请求",
|
||||
[301] = "辞典查询失败",
|
||||
[302] = "翻译查询失败",
|
||||
[303] = "服务端的其它异常",
|
||||
[305] = "批量翻译部分成功",
|
||||
[401] = "账户已经欠费,请进行账户充值",
|
||||
[411] = "访问频率受限,请稍后访问",
|
||||
[412] = "长请求过于频繁,请稍后访问",
|
||||
[390001] = "词典名称不正确",
|
||||
})[tonumber(body.errorCode)]
|
||||
|
||||
vim.notify('Youdao API Error: ' .. (debug_msg or vim.inspect(body)), vim.log.levels.ERROR)
|
||||
end
|
||||
|
||||
---@overload fun(TransData): TransResult
|
||||
---Query Using Youdao API
|
||||
---@param data TransData
|
||||
@ -97,6 +135,7 @@ function M.query(data)
|
||||
end
|
||||
|
||||
if not status or not body or body.errorCode ~= "0" then
|
||||
if not require('Trans').conf.debug then M.debug(body) end
|
||||
data.result.youdao = false
|
||||
data[#data + 1] = res
|
||||
return
|
||||
@ -112,15 +151,17 @@ function M.query(data)
|
||||
|
||||
|
||||
local tmp = {
|
||||
title = {
|
||||
title = {
|
||||
word = body.query,
|
||||
phonetic = body.basic.phonetic,
|
||||
},
|
||||
web = body.web,
|
||||
phrases = body.phrases,
|
||||
explains = body.basic.explains,
|
||||
synonyms = body.synonyms,
|
||||
sentenceSample = body.sentenceSample,
|
||||
web = body.web,
|
||||
explains = body.basic.explains,
|
||||
-- phrases = body.phrases,
|
||||
-- synonyms = body.synonyms,
|
||||
-- sentenceSample = body.sentenceSample,
|
||||
|
||||
|
||||
[data.from == 'en' and 'translation' or 'definition'] = body.translation,
|
||||
}
|
||||
|
||||
|
@ -106,7 +106,7 @@ end
|
||||
function buffer:add_highlight(linenr, hl_group, col_start, col_end, ns)
|
||||
-- vim.print(linenr, hl_group, col_start, col_end, ns)
|
||||
main_loop(function()
|
||||
linenr = linenr - 1 or -1
|
||||
linenr = linenr - 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)
|
||||
@ -159,7 +159,7 @@ buffer.__index = function(self, key)
|
||||
if res then
|
||||
return res
|
||||
elseif type(key) == 'number' then
|
||||
-- return fn.getbufoneline(self.bufnr, key) -- Vimscript Function Or Lua API ?? -- INFO :v0.8.1 doesn't support getbufoneline
|
||||
-- return fn.getbufoneline(self.bufnr, key) -- Vimscript Function Or Lua API ?? -- INFO :only work on neovim-nightly
|
||||
return api.nvim_buf_get_lines(self.bufnr, key - 1, key, true)[1]
|
||||
else
|
||||
error('invalid key: ' .. key)
|
||||
|
@ -16,6 +16,7 @@ return {
|
||||
---@type string the directory for database file and password file
|
||||
dir = os.getenv('HOME') .. '/.vim/dict',
|
||||
query = 'fallback',
|
||||
debug = true,
|
||||
-- backend_order = {},
|
||||
---@type 'default' | 'dracula' | 'tokyonight' global Trans theme [see lua/Trans/style/theme.lua]
|
||||
theme = 'default', -- default | tokyonight | dracula
|
||||
@ -102,11 +103,11 @@ return {
|
||||
no = '', -- ❌ | ❎ | ✖ | ✘ | ✗ |
|
||||
cell = '■', -- ■ | □ | ▇ | ▏ ▎ ▍ ▌ ▋ ▊ ▉ █
|
||||
web = '', --🌍 | 🌎 | 🌏 | 🌐 |
|
||||
tag = ' ',
|
||||
tag = '',
|
||||
pos = '',
|
||||
translation = '',
|
||||
definition = '',
|
||||
exchange = '✳',
|
||||
exchange = '',
|
||||
},
|
||||
},
|
||||
},
|
||||
|
@ -143,6 +143,43 @@ function M.main_loop(func)
|
||||
coroutine.yield()
|
||||
end
|
||||
|
||||
---Split text into paragraphs
|
||||
---@param lines string[] text to be split
|
||||
---@return string[][] paragraphs
|
||||
function M.split_to_paragraphs(lines, opts)
|
||||
--- TODO :More options and better algorithm to detect paragraphs
|
||||
opts = opts or {}
|
||||
local paragraphs = {}
|
||||
local paragraph = {}
|
||||
for _, line in ipairs(lines) do
|
||||
if line == '' then
|
||||
paragraphs[#paragraphs + 1] = paragraph
|
||||
paragraph = {}
|
||||
else
|
||||
paragraph[#paragraph + 1] = line
|
||||
end
|
||||
end
|
||||
return paragraphs
|
||||
end
|
||||
|
||||
---Get visible lines in the window or current window
|
||||
---@param opts { winid: integer, height: integer }?
|
||||
---@return string[]
|
||||
function M.visible_lines(opts)
|
||||
opts = opts or {}
|
||||
|
||||
-- INFO : don't calculate the height of statusline and cmdheight or winbar?
|
||||
local winid = opts.winid or 0
|
||||
local win_height = opts.height or api.nvim_win_get_height(winid)
|
||||
local current_line = api.nvim_win_get_cursor(winid)[1]
|
||||
local current_relative_line = vim.fn.winline()
|
||||
|
||||
|
||||
local _start = current_line - current_relative_line
|
||||
local _end = _start + win_height - vim.o.cmdheight --[[ - 1 -- maybe 1 for statusline?? ]]
|
||||
|
||||
return api.nvim_buf_get_lines(0, _start, _end, false)
|
||||
end
|
||||
|
||||
---@class Trans
|
||||
---@field util TransUtil
|
||||
|
@ -1,4 +1,5 @@
|
||||
local api = vim.api
|
||||
---@class Trans
|
||||
local Trans = require("Trans")
|
||||
|
||||
---@class TransWindow
|
||||
@ -110,6 +111,7 @@ function window:resize(opts)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
---Try to close window with animation?
|
||||
function window:try_close()
|
||||
local close_animation = self.animation.close
|
||||
|
@ -10,7 +10,6 @@ function M.web(hover, result)
|
||||
local buffer = hover.buffer
|
||||
buffer:setline(co(hover.opts.icon.web .. ' 网络释义'))
|
||||
|
||||
local indent = interval .. interval .. hover.opts.icon.list .. ' '
|
||||
local function remove_duplicate(strs)
|
||||
local uniq_strs = {}
|
||||
local str_map = {}
|
||||
@ -29,6 +28,7 @@ function M.web(hover, result)
|
||||
return uniq_strs
|
||||
end
|
||||
|
||||
local indent = interval .. ' ' .. hover.opts.icon.list .. ' '
|
||||
for _, w in ipairs(result.web) do
|
||||
buffer:setline(it(
|
||||
interval .. w.key,
|
||||
|
Loading…
x
Reference in New Issue
Block a user