From eb10830eb53841e7b70185462fa3748a4596da41 Mon Sep 17 00:00:00 2001
From: JuanZoran <1430359574@qq.com>
Date: Sat, 21 Jan 2023 14:26:33 +0800
Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=B0=86title=E5=92=8Ccontent?=
 =?UTF-8?q?=E7=9A=84=E5=AD=97=E5=8F=98=E6=88=90=E4=BA=86contents=E7=9A=84?=
 =?UTF-8?q?=E6=95=B0=E7=BB=84,=E5=90=8C=E6=97=B6=E4=B8=8D=E5=86=8D?=
 =?UTF-8?q?=E8=87=AA=E5=8A=A8=E8=AE=BE=E7=BD=AEmodifiable?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 lua/Trans/content.lua    | 10 ++++----
 lua/Trans/view/float.lua |  8 +++---
 lua/Trans/view/hover.lua |  9 +++++--
 lua/Trans/window.lua     | 53 +++++++++++++++++-----------------------
 4 files changed, 38 insertions(+), 42 deletions(-)

diff --git a/lua/Trans/content.lua b/lua/Trans/content.lua
index db57a43..f76ce9c 100644
--- a/lua/Trans/content.lua
+++ b/lua/Trans/content.lua
@@ -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,
diff --git a/lua/Trans/view/float.lua b/lua/Trans/view/float.lua
index e43fc4a..b4c582f 100644
--- a/lua/Trans/view/float.lua
+++ b/lua/Trans/view/float.lua
@@ -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()
diff --git a/lua/Trans/view/hover.lua b/lua/Trans/view/hover.lua
index 07218e0..8afac7a 100644
--- a/lua/Trans/view/hover.lua
+++ b/lua/Trans/view/hover.lua
@@ -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)
diff --git a/lua/Trans/window.lua b/lua/Trans/window.lua
index 000a478..e385bca 100644
--- a/lua/Trans/window.lua
+++ b/lua/Trans/window.lua
@@ -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')