refactor: add frontend.hover.load function to set buffer content

This commit is contained in:
JuanZoran
2023-03-13 21:41:17 +08:00
parent 7731c6c8cb
commit 693dd0c7aa
8 changed files with 201 additions and 345 deletions

View File

@ -123,11 +123,13 @@ end
---@param nodes string|table|table[] string -> as line content | table -> as a node | table[] -> as node[]
---@param linenr number? line number should be set[one index] or let it be nil to append
function buffer:setline(nodes, linenr)
linenr = linenr and linenr - 1 or -1
self:set('modifiable', true)
linenr = linenr and linenr - 1 or self:line_count()
if type(nodes) == 'string' then
api.nvim_buf_set_lines(self.bufnr, linenr, linenr, false, { nodes })
elseif type(nodes) == 'table' then
else
if type(nodes[1]) == 'string' then
-- FIXME :set [nodes] type as node
---@diagnostic disable-next-line: assign-type-mismatch
@ -149,6 +151,8 @@ function buffer:setline(nodes, linenr)
end
end
end
self:set('modifiable', false)
end
---@private
@ -183,6 +187,7 @@ function buffer.new()
}, buffer)
new_buf:set('modifiable', false)
new_buf:set('filetype', 'Trans')
new_buf:set('buftype', 'nofile')
return new_buf

View File

@ -3,8 +3,20 @@ local Trans = require('Trans')
local M = {}
M.__index = M
---@class data
---@field str string
---@field mode string
---@field result table
---@field frontend table
---@field backend table
---@field from string
---@field to string
---@field is_word boolean
---Data constructor
---@param opts table
---@return data
function M.new(opts)
local mode = opts.mode
local str = opts.str
@ -39,4 +51,17 @@ function M.new(opts)
return setmetatable(data, M)
end
-- ---Get the first available result [return nil if no result]
-- ---@return table?
-- function M:get_available_result()
-- local result = self.result
-- local backend = self.backend
-- for _, name in ipairs(backend) do
-- if result[name] then
-- return result[name]
-- end
-- end
-- end
return M

View File

@ -13,19 +13,26 @@ local function init_opts(opts)
return opts
end
local function set_result(data)
local function do_query(data)
-- HACK :Rewrite this function to support multi requests
local frontend = data.frontend
local result = data.result
for _, backend in ipairs(data.backend) do
local name = backend.name
if backend.no_wait then
backend.query(data)
else
backend.query(data)
frontend:wait(data.result, name, backend.timeout)
frontend:wait(result, name, backend.timeout)
end
if type(data.result[name]) == 'table' then break end
if type(result[name]) == 'table' then
return result[name]
else
result[name] = nil
end
end
end
@ -46,20 +53,11 @@ local function process(opts)
local data = Trans.data.new(opts)
set_result(data)
local success = false
for _, v in pairs(data.result) do
if type(v) == "table" then
success = true
break
end
end
if success == false then return end
local result = do_query(data)
if not result then return end
Trans.cache[data.str] = data
data.frontend:process(data)
data.frontend:process(data, result)
end
return coroutine.wrap(process)