fix: use api.nvim_buf_get_lines instead of fn.getbufoneline

This commit is contained in:
JuanZoran 2023-03-16 21:15:00 +08:00
parent 4a0f9b5c85
commit b9abc99086
9 changed files with 118 additions and 32 deletions

View File

@ -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,
}

View File

@ -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)
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

View File

@ -84,6 +84,12 @@ return {
'translation',
'definition',
},
youdao = {
'title',
'translation',
'definition',
'web',
}
},
---@type table<string, string>
icon = {

View File

@ -59,6 +59,8 @@ end
---@field exchange table<string, string>? @table: {name, value}
---@field definition? string[]? @array of definitions
---@field translation? string[]? @array of translations
---@field web? table<string, string[]>[]? @web definitions
---@field explains? string[]? @basic explains
---Get the first available result [return nil if no result]

View File

@ -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()

View File

@ -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<string, TransHoverFormatter>
---@type TransHoverRenderer
local default = {
str = function(hover, result)
@ -48,7 +59,7 @@ local default = {
default.__index = default
---@type table<string, TransHoverRenderer>
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

View File

@ -15,14 +15,12 @@ function M.tag(hover, result)
local size = #tag
for i = 1, size, 3 do
buffer:setline(
it(
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

View File

@ -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

View File

@ -47,6 +47,10 @@ return {
TransWaitting = {
link = 'MoreMsg'
},
TransWeb = {
-- TODO :
link = 'MoreMsg',
}
},
--- TODO :