fix: fix format and process function behaviours

This commit is contained in:
JuanZoran
2023-01-10 17:51:07 +08:00
parent 1b95485ae6
commit a57a6e47ab
14 changed files with 193 additions and 324 deletions

View File

@ -3,7 +3,9 @@ local type_check = require("Trans.util.debug").type_check
M.__index = M
M.lines = {}
M.highlight = {}
M.size = 0
M.height = 0
M.width = 0
M.interval = ' '
function M:new()
@ -31,10 +33,9 @@ function M:insert(items)
items = { items, 'table' },
}
self.size = self.size + 1 -- line数加一
self.height = self.height + 1 -- line数加一
local line = {
space = (' '):rep(items.interval),
indent = items.indent,
highlight = items.highlight,
}
@ -52,8 +53,8 @@ function M:insert(items)
end
end
self.highlight[self.size] = highlight
self.lines[self.size] = line
self.highlight[self.height] = highlight
self.lines[self.height] = line
end
---Usage:
@ -78,9 +79,11 @@ function M:data()
if l.indent then
line = (' '):rep(l.indent)
end
if l.highlight then
line = line .. table.concat(l, l.space)
line = line .. table.concat(l, self.interval)
highlight[1] = { name = l.highlight, _start = 1, _end = -1 }
else
line = line .. l[1]
@ -90,7 +93,7 @@ function M:data()
end
for i = 2, #l do
line = line .. l.space .. l[i]
line = line .. self.interval .. l[i]
if hl[i] then
local _end = #line
table.insert(highlight, { name = hl[i], _start = _end - #l[i], _end = _end })
@ -98,11 +101,37 @@ function M:data()
end
end
-- return line, highlights
lines[index] = line
local len = #line
if self.width < len then
self.width = len
end
highlights[index] = highlight
end
return lines, highlights
end
function M:attach(bufnr, winid)
local height = vim.api.nvim_win_get_height(winid)
local width = vim.api.nvim_win_get_width(winid)
vim.api.nvim_win_set_height(winid, self.height)
local lines, hls = self:data()
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
for line, l_hl in ipairs(hls) do
for _, hl in ipairs(l_hl) do
vim.api.nvim_buf_add_highlight(bufnr, -1, hl.name, line - 1, hl._start, hl._end)
end
end
if self.height < height then
vim.api.nvim_win_set_height(winid, self.height)
end
if self.width < width then
vim.api.nvim_win_set_width(winid, self.width)
end
end
return M

View File

@ -1,6 +1,6 @@
local M = {}
M.to_content = function (field)
M.component = function (field)
-- TODO
end

View File

@ -1,6 +1,6 @@
local M = {}
M.to_content = function (field)
M.component = function (field)
-- TODO
end

View File

@ -1,6 +1,6 @@
local M = {}
M.to_content = function (field)
M.component = function (field)
-- TODO
end

View File

@ -1,7 +1,47 @@
local M = {}
M.to_content = function (field)
-- TODO
local tag_map = {
zk = '中考',
gk = '高考',
ky = '考研',
cet4 = '四级',
cet6 = '六级',
ielts = '雅思',
toefl = '托福',
gre = 'GRE',
}
---从查询结果中获取字符串
---@param field table 查询的结果
---@return component? component 提取的组件信息[包含多个组件]
M.component = function(field)
-- TODO
if field.tag and field.tag ~= '' then
local ref = {
{ '标签:', 'TransRef' },
}
local tags = {
needformat = true,
highlight = 'TransTag',
indent = 4,
}
for _tag in vim.gsplit(field.tag, ' ', true) do
if _tag ~= '' then
local tag = tag_map[_tag]
if tag then
table.insert(tags, tag)
else
error('add tag_map for [' .. _tag .. ']')
end
end
end
return { ref, tags }
end
end
return M

View File

@ -25,13 +25,13 @@ local icon = require("Trans.conf.loader").loaded_conf.ui.icon
-- -- { phonetic, 'TransPhonetic' },
-- }
---@alias stuff
---| 'data' # 所有组件的信息
---@alias items
---| 'string[]' # 所有组件的信息
---| 'needformat?'# 是否需要格式化
---| 'highlight?' # 整个组件的高亮
---| 'indent?' # 每行整体的缩进
---| 'interval?' # 每个组件的间隔
---@alias component stuff[]
---@alias component items[]
---从查询结果中获取字符串
---@param field table 查询的结果
---@return component component 提取的组件信息[包含多个组件]
@ -41,7 +41,7 @@ M.component = function(field)
{ field.word, 'TransWord' },
}
if display.phnoetic and field.phonetic ~= '' then
if display.phnoetic and field.phonetic and field.phonetic ~= '' then
table.insert(
data,
{ '[' .. field.phonetic .. ']', 'TransPhonetic' }

View File

@ -1,6 +1,6 @@
local M = {}
M.to_content = function (field)
M.component = function (field)
-- TODO
end