refactor: 将title和content的字变成了contents的数组,同时不再自动设置modifiable

This commit is contained in:
JuanZoran 2023-01-21 14:26:33 +08:00
parent e058985bf3
commit eb10830eb5
4 changed files with 38 additions and 42 deletions

View File

@ -41,15 +41,16 @@ local content = {
self.size = 0
end,
attach = function(self)
---将内容连接上对应的窗口
---@param self table content对象
---@param offset integer 起始行
attach = function(self, offset)
if self.size == 0 then
return
end
self.window:bufset('modifiable', true)
local window = self.window
local offset = self.offset
api.nvim_buf_set_lines(window.bufnr, offset, offset + 1, true, self.lines)
for _, hl in ipairs(self.highlights) do
@ -153,13 +154,12 @@ local content = {
---content的构造函数
---@param window table 链接的窗口
---@return table 构造好的content
return function(window, offset)
return function(window)
vim.validate {
window = { window, 't' },
}
return setmetatable({
modifiable = true,
offset = offset or 0,
window = window,
size = 0,
hl_size = 0,

View File

@ -2,7 +2,7 @@ local m_window
local m_result
local function set_title()
local title = m_window.title
local title = m_window.contents[1]
local github = 'https://github.com/JuanZoran/Trans.nvim'
-- TODO :config this
@ -10,7 +10,7 @@ local function set_title()
end
local action = {
quit = function ()
quit = function()
m_window:try_close()
end,
}
@ -21,7 +21,7 @@ return function(word)
local float = require('Trans').conf.float
m_result = require('Trans.query.offline')(word)
local opt = {
local opt = {
relative = 'editor',
width = float.width,
height = float.height,
@ -30,7 +30,7 @@ return function(word)
row = math.floor((vim.o.lines - float.height) / 2),
col = math.floor((vim.o.columns - float.width) / 2),
}
m_window = require('Trans.window')(true, opt)
m_window = require('Trans.window')(true, opt)
m_window.animation = float.animation
set_title()

View File

@ -275,7 +275,7 @@ return function(word)
m_window = require("Trans.window")(false, opt)
m_window.animation = hover.animation
m_content = m_window.content
m_content = m_window.contents[1]
if m_result then
for _, field in ipairs(conf.order) do
@ -287,7 +287,12 @@ return function(word)
end
m_window:draw()
m_window.height = m_content:actual_height(true)
local height = m_content:actual_height(true)
if height < m_window.height then
m_window:set_height(height)
end
m_window:open(function()
m_window:set('wrap', true)
end)

View File

@ -85,8 +85,11 @@ local window = {
---@param self table 窗口的对象
draw = function(self)
-- TODO :
self.title:attach()
self.content:attach()
local offset = 0
for _, content in ipairs(self.contents) do
content:attach(offset)
offset = offset + content.size
end
end,
open = function(self, callback)
@ -193,20 +196,18 @@ local window = {
}
---@class window
---@field title table 窗口不变的title对象,载入后不可修改
---@field winid integer 窗口的handle
---@field bufnr integer 窗口对应buffer的handle
---@field content table 窗口内容的对象, 和title一样是content类
---@field width integer 窗口当前的宽度
---@field height integer 窗口当前的高度
---@field hl integer 窗口highlight的namespace
---@field contents table[] 窗口内容的对象数组
---窗口对象的构造器
---@param entry boolean 光标初始化时是否应该进入窗口
---@param option table 需要设置的选项
---@return window
---@return window win
---@nodiscard
return function(entry, option)
vim.validate {
@ -235,34 +236,24 @@ return function(entry, option)
local bufnr = api.nvim_create_buf(false, true)
local winid = api.nvim_open_win(bufnr, entry, opt)
local win = setmetatable({
winid = winid,
bufnr = bufnr,
title = nil,
content = nil,
width = opt.width,
height = opt.height,
hl = api.nvim_create_namespace('TransWinHl'),
},
{ __index = function(tbl, key)
if key == 'content' then
if tbl.title then
tbl.content = require('Trans.content')(tbl, tbl.title.size)
tbl.title.modifiable = false
else
tbl.content = require('Trans.content')(tbl)
end
return tbl.content
elseif key == 'title' then
tbl.title = require('Trans.content')(tbl, 0)
return tbl.title
else
return window[key]
local win
win = {
winid = winid,
bufnr = bufnr,
width = opt.width,
height = opt.height,
hl = api.nvim_create_namespace('TransWinHl'),
contents = setmetatable({}, {
__index = function(self, key)
assert(type(key) == 'number')
self[key] = require('Trans.content')(win)
return self[key]
end
end })
})
}
setmetatable(win, { __index = window })
win:set('winhl', 'Normal:TransWin,FloatBorder:TransBorder')
win:bufset('filetype', 'Trans')
win:bufset('buftype', 'nofile')