refactor: rewrite TransNode and use main_loop in process instead of buffer function
This commit is contained in:
@@ -1,32 +1,24 @@
|
||||
local api, fn = vim.api, vim.fn
|
||||
local Trans = require('Trans')
|
||||
|
||||
---@class TransBuffer
|
||||
---@field bufnr integer buffer handle
|
||||
---@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()
|
||||
main_loop(function()
|
||||
api.nvim_buf_set_lines(self.bufnr, 0, -1, false, {})
|
||||
end)
|
||||
api.nvim_buf_set_lines(self.bufnr, 0, -1, false, {})
|
||||
end
|
||||
|
||||
---Delete buffer [_start, _end] line content [one index]
|
||||
---@param _start? integer start line index
|
||||
---@param _end? integer end line index
|
||||
function buffer:deleteline(_start, _end)
|
||||
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
|
||||
api.nvim_buf_set_lines(self.bufnr, _start, _end, false, {})
|
||||
end)
|
||||
---@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
|
||||
api.nvim_buf_set_lines(self.bufnr, _start, _end, false, {})
|
||||
end
|
||||
|
||||
---Set buffer option
|
||||
@@ -105,11 +97,9 @@ 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
|
||||
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)
|
||||
linenr = linenr - 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
|
||||
|
||||
---Get buffer line count
|
||||
@@ -170,7 +160,6 @@ end
|
||||
buffer.__newindex = function(self, key, nodes)
|
||||
if type(key) == 'number' then
|
||||
self:setline(nodes, key)
|
||||
|
||||
else
|
||||
rawset(self, key, nodes)
|
||||
end
|
||||
|
@@ -169,7 +169,7 @@ end
|
||||
---@param opts { winid: integer, height: integer }?
|
||||
---@return string[]
|
||||
function M.visible_lines(opts)
|
||||
opts = opts or {}
|
||||
opts = opts or {}
|
||||
|
||||
-- TODO : Use getpos('w0') and getpos('w$') to get the visible lines
|
||||
-- INFO : don't calculate the height of statusline and cmdheight or winbar?
|
||||
@@ -192,6 +192,34 @@ function M.is_word(str)
|
||||
return str:match('%w+') == str
|
||||
end
|
||||
|
||||
---@param list any[]
|
||||
---@param step table
|
||||
---@return any[]
|
||||
function M.list_concat(list, step)
|
||||
local size = #list
|
||||
local ret = { list[1] }
|
||||
if size <= 1 then return ret end
|
||||
for i = 2, size do
|
||||
ret[i * 2 - 2] = step
|
||||
ret[i * 2 - 1] = list[i]
|
||||
end
|
||||
-- FIXME : Use deepcopy step?
|
||||
return ret
|
||||
end
|
||||
|
||||
|
||||
---Get the field of the list
|
||||
---@param list any[]
|
||||
---@param field any
|
||||
---@return any[]
|
||||
function M.list_fields(list, field)
|
||||
local ret = {}
|
||||
for i, v in ipairs(list) do
|
||||
ret[i] = v[field]
|
||||
end
|
||||
return ret
|
||||
end
|
||||
|
||||
---@class Trans
|
||||
---@field util TransUtil
|
||||
return M
|
||||
|
Reference in New Issue
Block a user