From 9b51ce649d98a08edb22467cd50e0b9538de8a1e Mon Sep 17 00:00:00 2001 From: Yufan You Date: Wed, 2 Apr 2025 00:11:28 +0800 Subject: [PATCH 1/2] feat: accept a function that returns the layout to use This resolves #1080 --- README.md | 2 +- doc/CopilotChat.txt | 2 +- lua/CopilotChat/config.lua | 4 ++-- lua/CopilotChat/ui/chat.lua | 14 ++++++++++++-- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index d85a85f3..ddaf744d 100644 --- a/README.md +++ b/README.md @@ -472,7 +472,7 @@ Below are all available configuration options with their default values: -- default window options window = { - layout = 'vertical', -- 'vertical', 'horizontal', 'float', 'replace' + layout = 'vertical', -- 'vertical', 'horizontal', 'float', 'replace', or a function that returns the layout width = 0.5, -- fractional width of parent, or absolute width in columns when > 1 height = 0.5, -- fractional height of parent, or absolute height in rows when > 1 -- Options below only apply to floating windows diff --git a/doc/CopilotChat.txt b/doc/CopilotChat.txt index 7bc58438..ca625dcb 100644 --- a/doc/CopilotChat.txt +++ b/doc/CopilotChat.txt @@ -531,7 +531,7 @@ Below are all available configuration options with their default values: -- default window options window = { - layout = 'vertical', -- 'vertical', 'horizontal', 'float', 'replace' + layout = 'vertical', -- 'vertical', 'horizontal', 'float', 'replace', or a function that returns the layout width = 0.5, -- fractional width of parent, or absolute width in columns when > 1 height = 0.5, -- fractional height of parent, or absolute height in rows when > 1 -- Options below only apply to floating windows diff --git a/lua/CopilotChat/config.lua b/lua/CopilotChat/config.lua index 5e741377..055bb104 100644 --- a/lua/CopilotChat/config.lua +++ b/lua/CopilotChat/config.lua @@ -1,7 +1,7 @@ local select = require('CopilotChat.select') ---@class CopilotChat.config.window ----@field layout 'vertical'|'horizontal'|'float'|'replace'? +---@field layout? CopilotChat.ui.Chat.Layout|fun():CopilotChat.ui.Chat.Layout ---@field relative 'editor'|'win'|'cursor'|'mouse'? ---@field border 'none'|'single'|'double'|'rounded'|'solid'|'shadow'? ---@field width number? @@ -76,7 +76,7 @@ return { -- default window options window = { - layout = 'vertical', -- 'vertical', 'horizontal', 'float', 'replace' + layout = 'vertical', -- 'vertical', 'horizontal', 'float', 'replace', or a function that returns the layout width = 0.5, -- fractional width of parent, or absolute width in columns when > 1 height = 0.5, -- fractional height of parent, or absolute height in rows when > 1 -- Options below only apply to floating windows diff --git a/lua/CopilotChat/ui/chat.lua b/lua/CopilotChat/ui/chat.lua index 3e416f05..10ec81ee 100644 --- a/lua/CopilotChat/ui/chat.lua +++ b/lua/CopilotChat/ui/chat.lua @@ -33,6 +33,8 @@ local function match_header(header) end end +---@alias CopilotChat.ui.Chat.Layout 'vertical'|'horizontal'|'float'|'replace' + ---@class CopilotChat.ui.Chat.Section.Block.Header ---@field filename string ---@field start_line number @@ -55,6 +57,7 @@ end ---@class CopilotChat.ui.Chat : CopilotChat.ui.Overlay ---@field winnr number? ---@field config CopilotChat.config.shared +---@field layout CopilotChat.ui.Chat.Layout? ---@field sections table ---@field references table ---@field token_count number? @@ -71,6 +74,7 @@ local Chat = class(function(self, question_header, answer_header, separator, hel self.winnr = nil self.sections = {} self.config = {} + self.layout = nil self.references = {} self.token_count = nil self.token_max_count = nil @@ -268,15 +272,21 @@ function Chat:open(config) self:validate() local window = config.window or {} + local layout = window.layout + if type(layout) == 'function' then + layout = layout() + end + local width = window.width > 1 and window.width or math.floor(vim.o.columns * window.width) local height = window.height > 1 and window.height or math.floor(vim.o.lines * window.height) - if self.config and self.config.window and self.config.window.layout ~= layout then + if self.layout ~= layout then self:close() end self.config = config + self.layout = layout if self:visible() then return @@ -357,7 +367,7 @@ function Chat:close(bufnr) utils.return_to_normal_mode() end - if self.config.window and self.config.window.layout == 'replace' then + if self.layout == 'replace' then if bufnr then self:restore(self.winnr, bufnr) end From 3d00a704a30bb7a3c3a5b0c16323be7336ce49df Mon Sep 17 00:00:00 2001 From: Yufan You Date: Wed, 2 Apr 2025 00:52:54 +0800 Subject: [PATCH 2/2] refactor: move the `Layout` type alias to `config` --- lua/CopilotChat/config.lua | 4 +++- lua/CopilotChat/ui/chat.lua | 4 +--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lua/CopilotChat/config.lua b/lua/CopilotChat/config.lua index 055bb104..d1ea254a 100644 --- a/lua/CopilotChat/config.lua +++ b/lua/CopilotChat/config.lua @@ -1,7 +1,9 @@ local select = require('CopilotChat.select') +---@alias CopilotChat.config.Layout 'vertical'|'horizontal'|'float'|'replace' + ---@class CopilotChat.config.window ----@field layout? CopilotChat.ui.Chat.Layout|fun():CopilotChat.ui.Chat.Layout +---@field layout? CopilotChat.config.Layout|fun():CopilotChat.config.Layout ---@field relative 'editor'|'win'|'cursor'|'mouse'? ---@field border 'none'|'single'|'double'|'rounded'|'solid'|'shadow'? ---@field width number? diff --git a/lua/CopilotChat/ui/chat.lua b/lua/CopilotChat/ui/chat.lua index 10ec81ee..82af9789 100644 --- a/lua/CopilotChat/ui/chat.lua +++ b/lua/CopilotChat/ui/chat.lua @@ -33,8 +33,6 @@ local function match_header(header) end end ----@alias CopilotChat.ui.Chat.Layout 'vertical'|'horizontal'|'float'|'replace' - ---@class CopilotChat.ui.Chat.Section.Block.Header ---@field filename string ---@field start_line number @@ -57,7 +55,7 @@ end ---@class CopilotChat.ui.Chat : CopilotChat.ui.Overlay ---@field winnr number? ---@field config CopilotChat.config.shared ----@field layout CopilotChat.ui.Chat.Layout? +---@field layout CopilotChat.config.Layout? ---@field sections table ---@field references table ---@field token_count number?