feat: debug format function and add content function support

This commit is contained in:
JuanZoran
2023-01-09 01:59:04 +08:00
parent 5a424c66bb
commit eaf8a3acce
12 changed files with 341 additions and 126 deletions

View File

@@ -0,0 +1,96 @@
local M = {}
local type_check = require("Trans.util.debug").type_check
M.__index = M
M.lines = {}
M.highlight = {}
M.size = 0
function M:new()
local content = {}
setmetatable(content, self)
return content
end
--- NOTE :highlight 格式说明:
--- 1. 字符串
function M:insert_items_to_line(items, opts)
type_check {
items = { items, 'table' },
opts = { opts, 'table', true },
}
self.size = self.size + 1 -- line数加一
local line = {
space = (' '):rep(opts.interval),
indent = opts.indent,
highlight = opts.highlight,
}
local highlight = {}
for i, item in ipairs(items) do
if type(item) == 'string' then
item = { item }
end
line[i] = item[1]
if item[2] then
highlight[i] = item[2]
end
end
self.highlight[self.size] = highlight
self.lines[self.size] = line
end
---遍历lines和高亮的迭代器
---Usage:
--- local buffer_id
--- local lines, highlights = M:lines()
--- vim.api.nvim_buf_set_lines(buffer_id, 0, -1, false,lines)
--- for i, hl in ipairs(highlights) do
--- vim.api.nvim_buf_add_highlight(buffer_id, 0, hl.name, i, hl._start, hl._end)
--- end
---@return table line
---@return table highlight
function M:lines()
-- NOTE 返回格式化的行,如果需要高亮,则第二个参数返回高亮
local lines = {}
local highlights = {}
for index = 1, #self.lines do
local line = ''
local highlight = {}
local l = self.lines[index]
local hl = self.highlight[index]
if l.indent then
line = (' '):rep(l.indent)
end
if l.highlight then
line = line .. table.concat(l, l.space)
highlight[1] = { name = l.highlight, _start = 1, _end = -1 }
else
line = line .. l[1]
if hl[1] then
-- WARN :可能需要设置成字符串宽度!!!
table.insert(highlight, { name = hl[1], _start = #line - #l[1], _end = #line })
end
for i = 2, #l do
line = line .. l.space .. l[i]
if hl[i] then
table.insert(highlight, { name = hl[i], _start = #line - #l[i], _end = #line })
end
end
end
-- return line, highlights
lines[index] = line
highlights[index] = highlight
end
return lines, highlights
end
return M

View File

@@ -0,0 +1,11 @@
local M = {}
local type_check = require("Trans.util.debug").type_check
M.__index = M
function M:new()
end
return M

View File

@@ -1,29 +1,78 @@
local M = {}
local display = require("Trans.conf.loader").loaded.conf.ui.display
-- Example:
-- local content = {
-- width = 1,
-- height = 1;
-- lines = {
-- Highlight = {
-- 'first line',
-- 'second line',
-- }
-- }, ---@table
local icon = require("Trans.conf.loader").loaded.conf.ui.icon
local m_field = {}
-- {
-- collins = 3,
-- definition = "n. an expression of greeting",
-- exchange = "s:hellos",
-- oxford = 1,
-- phonetic = "hə'ləʊ",
-- pos = "u:97/n:3",
-- tag = "zk gk",
-- translation = "n. 表示问候, 惊奇或唤起注意时的用语\nint. 喂;哈罗\nn. (Hello)人名;(法)埃洛",
-- word = "hello"
-- }
-- local function format()
--
-- end
local content = {
lines = {
need_format = {
{}, -- line
{}, -- line
{}, -- line
{}, -- line
}
},
highlight = {
[2] = { -- 第几行第几个组件的高亮
[1] = 'highlightname',
},
}
}
M.to_content = function (field)
-- TODO
local line = ''
local format = '%s %s %s %s'
local content = {
height = 1,
local function get_items()
local items = {
m_field.word,
}
if display.phonetic then
table.insert(items, '[' .. m_field.phonetic .. ']')
end
if display.collins_star then
table.insert(items, icon.star:rep(m_field.collins))
end
if display.oxford then
local item
if m_field.oxford and m_field.oxford == 1 then
item = icon.isOxford
else
item = icon.notOxford
end
table.insert(items, item)
end
return items
end
M.content = function(field)
-- TODO
m_field = field or {}
local content = {}
content.lines = {
need_format = {
get_items()
},
highlight = {
[1] = { -- 第一行
'Trans',
}
}
}
return content
end

View File

@@ -0,0 +1,12 @@
local M = {}
local type_check = require("Trans.util.debug").type_check
-- vim.pretty_print(vim.lsp.util._make_floating_popup_size)
local v = [=[
local test = {{
}}
]=]
print(v)
return M