From b9abc990865f763fcf35fd0b1febf9751e5d0b7b Mon Sep 17 00:00:00 2001 From: JuanZoran <1430359574@qq.com> Date: Thu, 16 Mar 2023 21:15:00 +0800 Subject: [PATCH] fix: use api.nvim_buf_get_lines instead of fn.getbufoneline --- lua/Trans/backend/youdao.lua | 38 ++++++++++++++++++++++++++-- lua/Trans/core/buffer.lua | 20 ++++++++++----- lua/Trans/core/conf.lua | 6 +++++ lua/Trans/core/data.lua | 2 ++ lua/Trans/frontend/hover/init.lua | 1 + lua/Trans/frontend/hover/load.lua | 27 ++++++++++---------- lua/Trans/frontend/hover/offline.lua | 19 ++++++++------ lua/Trans/frontend/hover/youdao.lua | 33 ++++++++++++++++++++++-- lua/Trans/style/theme.lua | 4 +++ 9 files changed, 118 insertions(+), 32 deletions(-) diff --git a/lua/Trans/backend/youdao.lua b/lua/Trans/backend/youdao.lua index e444706..96dd879 100644 --- a/lua/Trans/backend/youdao.lua +++ b/lua/Trans/backend/youdao.lua @@ -18,6 +18,40 @@ local M = { ---@field salt string ---@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 @@ -137,12 +171,13 @@ function M.query(data) if not body.isWord then data.result.youdao = { - str = body.query, + title = body.query, [data.from == 'en' and 'translation' or 'definition'] = body.translation, } return end + local tmp = { title = { word = body.query, @@ -152,7 +187,6 @@ function M.query(data) phrases = body.phrases, explains = body.basic.explains, synonyms = body.synonyms, - translation = body.translation, sentenceSample = body.sentenceSample, [data.from == 'en' and 'translation' or 'definition'] = body.translation, } diff --git a/lua/Trans/core/buffer.lua b/lua/Trans/core/buffer.lua index 5fc58bc..6b7eb73 100644 --- a/lua/Trans/core/buffer.lua +++ b/lua/Trans/core/buffer.lua @@ -6,10 +6,13 @@ local Trans = require('Trans') ---@field [number] string buffer[line] content local buffer = {} +local main_loop = Trans.util.main_loop + + -- INFO : corountine can't invoke C function ---Clear all content in buffer function buffer:wipe() - Trans.util.main_loop(function() + main_loop(function() api.nvim_buf_set_lines(self.bufnr, 0, -1, false, {}) end) end @@ -18,7 +21,7 @@ end ---@param _start? integer start line index ---@param _end? integer end line index function buffer:deleteline(_start, _end) - Trans.util.main_loop(function() + main_loop(function() ---@diagnostic disable-next-line: cast-local-type _start = _start and _start - 1 or self:line_count() - 1 _end = _end and _end - 1 or _start + 1 @@ -102,9 +105,11 @@ end ---@param ns number? highlight namespace function buffer:add_highlight(linenr, hl_group, col_start, col_end, ns) -- vim.print(linenr, hl_group, col_start, col_end, ns) - 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) + main_loop(function() + 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) end ---Get buffer line count @@ -154,8 +159,8 @@ 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 api.nvim_buf_get_lines(self.bufnr, key - 1, key, true)[1] + -- return fn.getbufoneline(self.bufnr, key) -- Vimscript Function Or Lua API ?? + return api.nvim_buf_get_lines(self.bufnr, key - 1, key, true)[1] else error('invalid key: ' .. key) end @@ -165,6 +170,7 @@ end buffer.__newindex = function(self, key, nodes) if type(key) == 'number' then self:setline(nodes, key) + else rawset(self, key, nodes) end diff --git a/lua/Trans/core/conf.lua b/lua/Trans/core/conf.lua index 8a4c1aa..b300385 100644 --- a/lua/Trans/core/conf.lua +++ b/lua/Trans/core/conf.lua @@ -84,6 +84,12 @@ return { 'translation', 'definition', }, + youdao = { + 'title', + 'translation', + 'definition', + 'web', + } }, ---@type table icon = { diff --git a/lua/Trans/core/data.lua b/lua/Trans/core/data.lua index 4faf44c..3195215 100644 --- a/lua/Trans/core/data.lua +++ b/lua/Trans/core/data.lua @@ -59,6 +59,8 @@ end ---@field exchange table? @table: {name, value} ---@field definition? string[]? @array of definitions ---@field translation? string[]? @array of translations +---@field web? table[]? @web definitions +---@field explains? string[]? @basic explains ---Get the first available result [return nil if no result] diff --git a/lua/Trans/frontend/hover/init.lua b/lua/Trans/frontend/hover/init.lua index ca2f6c4..af7c4c8 100644 --- a/lua/Trans/frontend/hover/init.lua +++ b/lua/Trans/frontend/hover/init.lua @@ -179,6 +179,7 @@ function M:process(data) self:fallback() return end + -- vim.pretty_print(result) local opts = self.opts if opts.auto_play then (data.from == 'en' and data.str or result.definition[1]):play() diff --git a/lua/Trans/frontend/hover/load.lua b/lua/Trans/frontend/hover/load.lua index adb75c9..82f45d5 100644 --- a/lua/Trans/frontend/hover/load.lua +++ b/lua/Trans/frontend/hover/load.lua @@ -2,11 +2,22 @@ local node = require('Trans').util.node local it, conjunction = node.item, node.conjunction local interval = (' '):rep(4) +local M = setmetatable({}, { + __call = function(self, hover, result, name, order) + order = order or hover.opts.order.default + + local method = self.renderer[name] + + for _, field in ipairs(order) do + -- print(field) + method[field](hover, result) + end + end, +}) ---@alias TransHoverFormatter fun(hover:TransHover, result: TransResult) ---@alias TransHoverRenderer table - ---@type TransHoverRenderer local default = { str = function(hover, result) @@ -48,7 +59,7 @@ local default = { default.__index = default ---@type table -local renderer = setmetatable({}, { +M.renderer = setmetatable({}, { __index = function(tbl, key) local status, method = pcall(require, 'Trans.frontend.hover.' .. key) if not status then @@ -60,17 +71,7 @@ local renderer = setmetatable({}, { end, }) --- FIXME : - ---@class TransHover ---@field load fun(hover: TransHover, result: TransResult, name: string, order: string[]) -return function(hover, result, name, order) - order = order or hover.opts.order.default - - local method = renderer[name] - - for _, field in ipairs(order) do - method[field](hover, result) - end -end +return M diff --git a/lua/Trans/frontend/hover/offline.lua b/lua/Trans/frontend/hover/offline.lua index 3e2dacb..4e3361b 100644 --- a/lua/Trans/frontend/hover/offline.lua +++ b/lua/Trans/frontend/hover/offline.lua @@ -15,14 +15,12 @@ function M.tag(hover, result) local size = #tag for i = 1, size, 3 do - buffer:setline( - it( - interval .. tag[i] .. - (tag[i + 1] and interval .. tag[i + 1] .. - (tag[i + 2] and interval .. tag[i + 2] or '') or ''), - 'TransTag' - ) - ) + buffer:setline(it( + interval .. tag[i] .. + (tag[i + 1] and interval .. tag[i + 1] .. + (tag[i + 2] and interval .. tag[i + 2] or '') or ''), + 'TransTag' + )) end buffer:setline('') @@ -63,6 +61,11 @@ end function M.title(hover, result) local title = result.title if not title then return end + if type(title) == 'string' then + hover.buffer:setline(it(title, 'TransWord')) + return + end + local icon = hover.opts.icon diff --git a/lua/Trans/frontend/hover/youdao.lua b/lua/Trans/frontend/hover/youdao.lua index 09c9f94..998cfa6 100644 --- a/lua/Trans/frontend/hover/youdao.lua +++ b/lua/Trans/frontend/hover/youdao.lua @@ -1,14 +1,43 @@ local node = require('Trans').util.node local it, t, f, co = node.item, node.text, node.format, node.conjunction - ---@type TransHoverRenderer local M = {} +local interval = (' '):rep(4) function M.web(hover, result) if not result.web then return end + local buffer = hover.buffer + buffer:setline(co('网络释义')) - + for _, w in ipairs(result.web) do + buffer:setline(it( + --- TODO :Better format style + interval .. w.key .. interval .. table.concat(w.value, ' | '), + 'TransWeb' + )) + end + buffer:setline('') end + +function M.explains(hover, result) + local explains = result.explains + if not explains then return end + local buffer = hover.buffer + buffer:setline(co('基本释义')) + + + for i = 1, #explains, 2 do + buffer:setline(it( + interval .. explains[i] .. + (explains[i + 1] and interval .. explains[i + 1] or ''), + 'TransExplains' + )) + end + buffer:setline('') +end + +M.title = require('Trans').frontend.hover.offline.title + return M diff --git a/lua/Trans/style/theme.lua b/lua/Trans/style/theme.lua index 2b8bfdd..5de0248 100644 --- a/lua/Trans/style/theme.lua +++ b/lua/Trans/style/theme.lua @@ -47,6 +47,10 @@ return { TransWaitting = { link = 'MoreMsg' }, + TransWeb = { + -- TODO : + link = 'MoreMsg', + } }, --- TODO :