style: code format use lua_ls

This commit is contained in:
JuanZoran
2023-03-24 00:56:36 +08:00
parent 9f5a81249c
commit 82fa2a008a
22 changed files with 227 additions and 247 deletions

View File

@@ -5,14 +5,14 @@
---@field app_passwd string
---@field disable boolean
local M = {
uri = "https://fanyi-api.baidu.com/api/trans/vip/translate",
uri = 'https://fanyi-api.baidu.com/api/trans/vip/translate',
salt = tostring(math.random(bit.lshift(1, 15))),
name = "baidu",
name = 'baidu',
method = 'get',
}
local Trans = require("Trans")
local Trans = require 'Trans'
---@class BaiduQuery
---@field q string
@@ -52,7 +52,7 @@ function M.formatter(body, data)
result = result[1]
return {
str = result.src,
[data.from == "en" and "translation" or "definition"] = { result.dst },
[data.from == 'en' and 'translation' or 'definition'] = { result.dst },
}
end

View File

@@ -20,9 +20,8 @@ function M.get_query(data)
}
end
function M.formatter(body, data)
print('TODO')
print 'TODO'
-- if true and not status or not body or body.errorCode ~= "0" then
-- data.result.iciba = false
-- data[#data + 1] = res

View File

@@ -1,10 +1,10 @@
local Trans = require("Trans")
local Trans = require 'Trans'
local db = require("sqlite.db")
local path = Trans.conf.dir .. Trans.separator .. "ultimate.db"
local db = require 'sqlite.db'
local path = Trans.conf.dir .. Trans.separator .. 'ultimate.db'
local dict = db:open(path)
local db_name = "stardict"
vim.api.nvim_create_autocmd("VimLeavePre", {
local db_name = 'stardict'
vim.api.nvim_create_autocmd('VimLeavePre', {
callback = function()
if db:isopen() then db:close() end
end,
@@ -12,7 +12,7 @@ vim.api.nvim_create_autocmd("VimLeavePre", {
---@class TransOfflineBackend
local M = {
name = "offline",
name = 'offline',
no_wait = true,
}
@@ -20,7 +20,7 @@ local M = {
---@return any
---@overload fun(TransData): TransResult
function M.query(data)
if data.is_word == false or data.from == "zh" then
if data.is_word == false or data.from == 'zh' then
return
end
@@ -35,19 +35,19 @@ end
-- this is a awesome plugin
M.query_field = {
"word",
"phonetic",
"definition",
"translation",
"pos",
"collins",
"oxford",
"tag",
"exchange",
'word',
'phonetic',
'definition',
'translation',
'pos',
'collins',
'oxford',
'tag',
'exchange',
}
local function exist(str)
return str and str ~= ""
return str and str ~= ''
end
---@type (fun(res):any)[]
@@ -71,18 +71,18 @@ local formatter = {
return
end
local tag_map = {
zk = "中考",
gk = "高考",
ky = "考研",
gre = "gre ",
cet4 = "四级",
cet6 = "六级",
ielts = "雅思",
toefl = "托福",
zk = '中考',
gk = '高考',
ky = '考研',
gre = 'gre ',
cet4 = '四级',
cet6 = '六级',
ielts = '雅思',
toefl = '托福',
}
local tag = {}
for i, _tag in ipairs(vim.split(res.tag, " ", { plain = true })) do
for i, _tag in ipairs(vim.split(res.tag, ' ', { plain = true })) do
tag[i] = tag_map[_tag]
end
@@ -93,20 +93,20 @@ local formatter = {
return
end
local exchange_map = {
["0"] = "原型 ",
["1"] = "类别 ",
["p"] = "过去式 ",
["r"] = "比较级 ",
["t"] = "最高级 ",
["s"] = "复数 ",
["d"] = "过去分词 ",
["i"] = "现在分词 ",
["3"] = "第三人称单数",
["f"] = "第三人称单数",
['0'] = '原型 ',
['1'] = '类别 ',
['p'] = '过去式 ',
['r'] = '比较级 ',
['t'] = '最高级 ',
['s'] = '复数 ',
['d'] = '过去分词 ',
['i'] = '现在分词 ',
['3'] = '第三人称单数',
['f'] = '第三人称单数',
}
local exchange = {}
for _, _exchange in ipairs(vim.split(res.exchange, "/", { plain = true })) do
for _, _exchange in ipairs(vim.split(res.exchange, '/', { plain = true })) do
exchange[exchange_map[_exchange:sub(1, 1)]] = _exchange:sub(3)
end
@@ -117,24 +117,24 @@ local formatter = {
return
end
local pos_map = {
a = "代词pron ",
c = "连接词conj ",
i = "介词prep ",
j = "形容词adj ",
m = "数词num ",
n = "名词n ",
p = "代词pron ",
r = "副词adv ",
u = "感叹词int ",
v = "动词v ",
x = "否定标记not ",
t = "不定式标记infm ",
d = "限定词determiner ",
a = '代词pron ',
c = '连接词conj ',
i = '介词prep ',
j = '形容词adj ',
m = '数词num ',
n = '名词n ',
p = '代词pron ',
r = '副词adv ',
u = '感叹词int ',
v = '动词v ',
x = '否定标记not ',
t = '不定式标记infm ',
d = '限定词determiner ',
}
local pos = {}
for _, _pos in ipairs(vim.split(res.pos, "/", { plain = true })) do
pos[pos_map[_pos:sub(1, 1)]] = ("%2s%%"):format(_pos:sub(3))
for _, _pos in ipairs(vim.split(res.pos, '/', { plain = true })) do
pos[pos_map[_pos:sub(1, 1)]] = ('%2s%%'):format(_pos:sub(3))
end
return pos
@@ -144,7 +144,7 @@ local formatter = {
return
end
local translation = {}
for i, _translation in ipairs(vim.split(res.translation, "\n", { plain = true })) do
for i, _translation in ipairs(vim.split(res.translation, '\n', { plain = true })) do
translation[i] = _translation
end
@@ -155,9 +155,9 @@ local formatter = {
return
end
local definition = {}
for i, _definition in ipairs(vim.split(res.definition, "\n", { plain = true })) do
for i, _definition in ipairs(vim.split(res.definition, '\n', { plain = true })) do
-- -- TODO :判断是否需要分割空格
definition[i] = _definition:gsub("^%s+", "", 1)
definition[i] = _definition:gsub('^%s+', '', 1)
end
return definition

View File

@@ -55,31 +55,31 @@ end
local function check_untracked_field(body)
local field = {
"phonetic",
'phonetic',
'usPhonetic',
"ukPhonetic",
"text", -- text 短语
"explain", -- String Array 词义解释列表
"wordFormats", -- Object Array 单词形式变化列表
"name", -- String 形式名称,例如:复数
"phrase", -- String 词组
"meaning", -- String 含义
"synonyms", -- JSONObject 近义词
"pos", -- String 词性
"words", -- String Array 近义词列表
"trans", -- String 释义
"antonyms", -- ObjectArray 反义词
"relatedWords", -- JSONArray 相关词
"wordNet", -- JSONObject 汉语词典网络释义
"phonetic", -- String 发音
"meanings", -- ObjectArray 释义
"meaning", -- String 释义
"example", -- array 示例
"sentenceSample", -- text 例句
"sentence", -- text 例句
"sentenceBold", -- text 将查询内容加粗的例句
"wfs", -- text 单词形式变化
"exam_type", -- text 考试类型
'ukPhonetic',
'text', -- text 短语
'explain', -- String Array 词义解释列表
'wordFormats', -- Object Array 单词形式变化列表
'name', -- String 形式名称,例如:复数
'phrase', -- String 词组
'meaning', -- String 含义
'synonyms', -- JSONObject 近义词
'pos', -- String 词性
'words', -- String Array 近义词列表
'trans', -- String 释义
'antonyms', -- ObjectArray 反义词
'relatedWords', -- JSONArray 相关词
'wordNet', -- JSONObject 汉语词典网络释义
'phonetic', -- String 发音
'meanings', -- ObjectArray 释义
'meaning', -- String 释义
'example', -- array 示例
'sentenceSample', -- text 例句
'sentence', -- text 例句
'sentenceBold', -- text 将查询内容加粗的例句
'wfs', -- text 单词形式变化
'exam_type', -- text 考试类型
}
for _, f in ipairs(field) do
if body[f] then
@@ -96,33 +96,33 @@ function M.debug(body)
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] = "词典名称不正确",
[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)
@@ -134,7 +134,7 @@ end
---@param data TransData Data obj
---@return table|false?
function M.formatter(body, data)
if body.errorCode ~= "0" then return false end
if body.errorCode ~= '0' then return false end
check_untracked_field(body)
if not body.isWord then
@@ -146,15 +146,14 @@ function M.formatter(body, data)
return {
title = {
title = {
word = body.query,
phonetic = body.basic.phonetic,
},
web = body.web,
explains = body.basic.explains,
web = body.web,
explains = body.basic.explains,
[data.from == 'en' and 'translation' or 'definition'] = body.translation,
}
end
---@class TransBackend