Skip to content

feat: accept a function that returns the layout to use #1087

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion doc/CopilotChat.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions lua/CopilotChat/config.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
local select = require('CopilotChat.select')

---@alias CopilotChat.config.Layout 'vertical'|'horizontal'|'float'|'replace'

---@class CopilotChat.config.window
---@field layout 'vertical'|'horizontal'|'float'|'replace'?
---@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?
Expand Down Expand Up @@ -76,7 +78,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
Expand Down
12 changes: 10 additions & 2 deletions lua/CopilotChat/ui/chat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ end
---@class CopilotChat.ui.Chat : CopilotChat.ui.Overlay
---@field winnr number?
---@field config CopilotChat.config.shared
---@field layout CopilotChat.config.Layout?
---@field sections table<CopilotChat.ui.Chat.Section>
---@field references table<CopilotChat.Provider.reference>
---@field token_count number?
Expand All @@ -71,6 +72,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
Expand Down Expand Up @@ -268,15 +270,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
Expand Down Expand Up @@ -357,7 +365,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
Expand Down