Skip to content

Commit

Permalink
feat: allow height and width for horizontal and vertical (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
deathbeam authored Apr 17, 2024
1 parent 9898b4c commit f033899
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 37 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,11 @@ Also see [here](/lua/CopilotChat/config.lua):
-- default window options
window = {
layout = 'vertical', -- 'vertical', 'horizontal', 'float'
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
relative = 'editor', -- 'editor', 'win', 'cursor', 'mouse'
border = 'single', -- 'none', single', 'double', 'rounded', 'solid', 'shadow'
width = 0.8, -- fractional width of parent
height = 0.6, -- fractional height of parent
row = nil, -- row position of the window, default is centered
col = nil, -- column position of the window, default is centered
title = 'Copilot Chat', -- title of chat window
Expand Down
63 changes: 30 additions & 33 deletions lua/CopilotChat/chat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,49 +112,46 @@ function Chat:open(config)
end

local window = config.window
local win_opts = {
style = 'minimal',
}

local layout = window.layout
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 layout == 'float' then
win_opts.zindex = window.zindex
win_opts.relative = window.relative
win_opts.border = window.border
win_opts.title = window.title
win_opts.row = window.row or math.floor(vim.o.lines * ((1 - config.window.height) / 2))
win_opts.col = window.col or math.floor(vim.o.columns * ((1 - window.width) / 2))
win_opts.width = math.floor(vim.o.columns * window.width)
win_opts.height = math.floor(vim.o.lines * window.height)

local win_opts = {
style = 'minimal',
width = width,
height = height,
zindex = window.zindex,
relative = window.relative,
border = window.border,
title = window.title,
row = window.row or math.floor((vim.o.lines - height) / 2),
col = window.col or math.floor((vim.o.columns - width) / 2),
}
if not is_stable() then
win_opts.footer = window.footer
end
self.winnr = vim.api.nvim_open_win(self.bufnr, false, win_opts)
elseif layout == 'vertical' then
if is_stable() then
local orig = vim.api.nvim_get_current_win()
vim.cmd('vsplit')
self.winnr = vim.api.nvim_get_current_win()
vim.api.nvim_win_set_buf(self.winnr, self.bufnr)
vim.api.nvim_set_current_win(orig)
else
win_opts.vertical = true
local orig = vim.api.nvim_get_current_win()
local cmd = 'vsplit'
if width ~= 0 then
cmd = width .. cmd
end
vim.cmd(cmd)
self.winnr = vim.api.nvim_get_current_win()
vim.api.nvim_win_set_buf(self.winnr, self.bufnr)
vim.api.nvim_set_current_win(orig)
elseif layout == 'horizontal' then
if is_stable() then
local orig = vim.api.nvim_get_current_win()
vim.cmd('split')
self.winnr = vim.api.nvim_get_current_win()
vim.api.nvim_win_set_buf(self.winnr, self.bufnr)
vim.api.nvim_set_current_win(orig)
else
win_opts.vertical = false
local orig = vim.api.nvim_get_current_win()
local cmd = 'split'
if height ~= 0 then
cmd = height .. cmd
end
end

if not self.winnr or not vim.api.nvim_win_is_valid(self.winnr) then
self.winnr = vim.api.nvim_open_win(self.bufnr, false, win_opts)
vim.cmd(cmd)
self.winnr = vim.api.nvim_get_current_win()
vim.api.nvim_win_set_buf(self.winnr, self.bufnr)
vim.api.nvim_set_current_win(orig)
end

vim.wo[self.winnr].wrap = true
Expand Down
4 changes: 2 additions & 2 deletions lua/CopilotChat/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,11 @@ return {
-- default window options
window = {
layout = 'vertical', -- 'vertical', 'horizontal', 'float'
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
relative = 'editor', -- 'editor', 'win', 'cursor', 'mouse'
border = 'single', -- 'none', single', 'double', 'rounded', 'solid', 'shadow'
width = 0.8, -- fractional width of parent
height = 0.6, -- fractional height of parent
row = nil, -- row position of the window, default is centered
col = nil, -- column position of the window, default is centered
title = 'Copilot Chat', -- title of chat window
Expand Down

0 comments on commit f033899

Please sign in to comment.