chore: sync
This commit is contained in:
parent
b9abc99086
commit
75fdfc6880
@ -7,8 +7,9 @@
|
||||
|
||||
- [x] Refactor query engine to 'Backend' and 'Frontend'
|
||||
- [x] Use `Trans.install` instead of `install.sh`
|
||||
- [ ] Check if str is a word
|
||||
- [x] waitting animation
|
||||
- [x] init frontend window
|
||||
- [ ] build frontend window format logic
|
||||
- [ ] Add Query FallBack
|
||||
- [x] build frontend window format logic
|
||||
- [x] Add Query FallBack
|
||||
- [ ] Check if str is a word
|
||||
- [ ] Unlimit width for sentence
|
||||
|
@ -19,39 +19,6 @@ local M = {
|
||||
---@field sign string
|
||||
|
||||
|
||||
-- local field = {
|
||||
-- "phonetic",
|
||||
-- 'usPhonetic',
|
||||
-- "ukPhonetic",
|
||||
-- "text", -- text 短语
|
||||
-- "explain", -- String Array 词义解释列表
|
||||
-- "wordFormats", -- Object Array 单词形式变化列表
|
||||
-- "name", -- String 形式名称,例如:复数
|
||||
-- "web", -- JSONArray 网络释义
|
||||
-- "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 示例
|
||||
-- "dict", -- String 词典deeplink
|
||||
-- "webDict", -- String 词典网页deeplink
|
||||
-- "sentenceSample", -- text 例句
|
||||
-- "sentence", -- text 例句
|
||||
-- "sentenceBold", -- text 将查询内容加粗的例句
|
||||
-- "translation", -- text 例句翻译
|
||||
-- "wfs", -- text 单词形式变化
|
||||
-- "exam_type", -- text 考试类型
|
||||
-- }
|
||||
|
||||
|
||||
---Get content for query
|
||||
---@param data TransData
|
||||
---@return YoudaoQuery
|
||||
@ -85,6 +52,92 @@ function M.get_content(data)
|
||||
}
|
||||
end
|
||||
|
||||
local field = {
|
||||
"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 考试类型
|
||||
}
|
||||
|
||||
|
||||
---@overload fun(TransData): TransResult
|
||||
---Query Using Youdao API
|
||||
---@param data TransData
|
||||
function M.query(data)
|
||||
local handle = function(res)
|
||||
local status, body = pcall(vim.json.decode, res.body)
|
||||
-- vim.print(body)
|
||||
if body then
|
||||
for _, f in ipairs(field) do
|
||||
if body[f] then
|
||||
print(('%s found : %s'):format(f, vim.inspect(body[f])))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if not status or not body or body.errorCode ~= "0" then
|
||||
data.result.youdao = false
|
||||
data[#data + 1] = res
|
||||
return
|
||||
end
|
||||
|
||||
if not body.isWord then
|
||||
data.result.youdao = {
|
||||
title = body.query,
|
||||
[data.from == 'en' and 'translation' or 'definition'] = body.translation,
|
||||
}
|
||||
return
|
||||
end
|
||||
|
||||
|
||||
local tmp = {
|
||||
title = {
|
||||
word = body.query,
|
||||
phonetic = body.basic.phonetic,
|
||||
},
|
||||
web = body.web,
|
||||
phrases = body.phrases,
|
||||
explains = body.basic.explains,
|
||||
synonyms = body.synonyms,
|
||||
sentenceSample = body.sentenceSample,
|
||||
[data.from == 'en' and 'translation' or 'definition'] = body.translation,
|
||||
}
|
||||
|
||||
|
||||
data.result.youdao = tmp
|
||||
end
|
||||
|
||||
require('Trans').curl.get(M.uri, {
|
||||
query = M.get_content(data),
|
||||
callback = handle,
|
||||
})
|
||||
end
|
||||
|
||||
---@class TransBackend
|
||||
---@field youdao Youdao
|
||||
return M
|
||||
|
||||
-- {
|
||||
-- basic = {
|
||||
-- explains = { "normal", "regular", "normality" },
|
||||
@ -153,54 +206,3 @@ end
|
||||
-- url = "http://mobile.youdao.com/dict?le=eng&q=%E6%AD%A3%E5%B8%B8%E5%88%A9%E6%B6%A6"
|
||||
-- }
|
||||
-- }
|
||||
|
||||
|
||||
|
||||
---@overload fun(TransData): TransResult
|
||||
---Query Using Youdao API
|
||||
---@param data TransData
|
||||
function M.query(data)
|
||||
local handle = function(res)
|
||||
local status, body = pcall(vim.json.decode, res.body)
|
||||
-- vim.print(body)
|
||||
if not status or not body or body.errorCode ~= "0" then
|
||||
data.result.youdao = false
|
||||
data[#data + 1] = res
|
||||
return
|
||||
end
|
||||
|
||||
if not body.isWord then
|
||||
data.result.youdao = {
|
||||
title = body.query,
|
||||
[data.from == 'en' and 'translation' or 'definition'] = body.translation,
|
||||
}
|
||||
return
|
||||
end
|
||||
|
||||
|
||||
local tmp = {
|
||||
title = {
|
||||
word = body.query,
|
||||
phonetic = body.basic.phonetic,
|
||||
},
|
||||
web = body.web,
|
||||
phrases = body.phrases,
|
||||
explains = body.basic.explains,
|
||||
synonyms = body.synonyms,
|
||||
sentenceSample = body.sentenceSample,
|
||||
[data.from == 'en' and 'translation' or 'definition'] = body.translation,
|
||||
}
|
||||
|
||||
|
||||
data.result.youdao = tmp
|
||||
end
|
||||
|
||||
require('Trans').curl.get(M.uri, {
|
||||
query = M.get_content(data),
|
||||
callback = handle,
|
||||
})
|
||||
end
|
||||
|
||||
---@class TransBackend
|
||||
---@field youdao Youdao
|
||||
return M
|
||||
|
@ -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 ??
|
||||
-- return fn.getbufoneline(self.bufnr, key) -- Vimscript Function Or Lua API ?? -- INFO :v0.8.1 doesn't support getbufoneline
|
||||
return api.nvim_buf_get_lines(self.bufnr, key - 1, key, true)[1]
|
||||
else
|
||||
error('invalid key: ' .. key)
|
||||
|
@ -54,6 +54,7 @@ return {
|
||||
---@type string -- TODO :support replace with {{special word}}
|
||||
fallback_message = '{{notfound}} 翻译超时或没有找到相关的翻译',
|
||||
auto_resize = true,
|
||||
-- strict = false, -- TODO :No Width limit when str is a sentence
|
||||
padding = 10, -- padding for hover window width
|
||||
keymaps = {
|
||||
pageup = '[[',
|
||||
|
@ -1,9 +1,9 @@
|
||||
local health = vim.health
|
||||
local health, fn = vim.health, vim.fn
|
||||
local ok = health.report_ok
|
||||
local warn = health.report_warn
|
||||
local error = health.report_error
|
||||
local has = vim.fn.has
|
||||
local executable = vim.fn.executable
|
||||
local has = fn.has
|
||||
local executable = fn.executable
|
||||
|
||||
|
||||
local function check_neovim_version()
|
||||
@ -45,6 +45,7 @@ local function check_binary_dependencies()
|
||||
binary_dependencies[3] = 'node'
|
||||
end
|
||||
|
||||
|
||||
for _, dep in ipairs(binary_dependencies) do
|
||||
if executable(dep) == 1 then
|
||||
ok(string.format('Binary dependency [%s] is installed', dep))
|
||||
@ -56,7 +57,7 @@ end
|
||||
|
||||
local function check_database()
|
||||
local db_path = require('Trans').conf.dir .. '/ultimate.db'
|
||||
if vim.fn.filereadable(db_path) == 1 then
|
||||
if fn.filereadable(db_path) == 1 then
|
||||
ok [[ultimate database found ]]
|
||||
else
|
||||
error [[Stardict database not found
|
||||
@ -67,15 +68,20 @@ local function check_database()
|
||||
end
|
||||
|
||||
local function check_configure_file()
|
||||
local path = vim.fn.expand(require('Trans').conf.dir .. '/Trans.json')
|
||||
local path = fn.expand(require('Trans').conf.dir .. '/Trans.json')
|
||||
if not fn.filereadable(path) then
|
||||
warn 'Backend configuration file[%s] not found'
|
||||
end
|
||||
|
||||
|
||||
local file = io.open(path, "r")
|
||||
local valid = file and pcall(vim.json.decode, file:read("*a"))
|
||||
|
||||
|
||||
if valid then
|
||||
ok(string.format([[Engine configuration file[%s] found and valid ]], path))
|
||||
ok(string.format([[Backend configuration file [%s] found and valid ]], path))
|
||||
else
|
||||
error(string.format([[Engine configuration file not found[%s] or invalid
|
||||
error(string.format([[Backend configuration file [%s] invalid
|
||||
Please check the doc in github: [https://github.com/JuanZoran/Trans.nvim]
|
||||
]], path))
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user