Skip to content

Commit e6ec73c

Browse files
authored
refactor(ui): remove auto option (#2591)
Closes #2590
1 parent 274b2c6 commit e6ec73c

File tree

5 files changed

+57
-53
lines changed

5 files changed

+57
-53
lines changed

doc/codecompanion.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*codecompanion.txt* For NVIM v0.11 Last change: 2025 December 22
1+
*codecompanion.txt* For NVIM v0.11 Last change: 2025 December 24
22

33
==============================================================================
44
Table of Contents *codecompanion-table-of-contents*

doc/configuration/chat-buffer.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,8 +565,8 @@ require("codecompanion").setup({
565565
full_height = true, -- for vertical layout
566566
position = nil, -- left|right|top|bottom (nil will default depending on vim.opt.splitright|vim.opt.splitbelow)
567567

568-
width = 0.5, ---@type number|"auto" using "auto" will allow full_height buffers to act like normal buffers
569-
height = 0.8,
568+
width = 0.5, ---@return number|fun(): number
569+
height = 0.8, ---@return number|fun(): number
570570
border = "single",
571571
relative = "editor",
572572

lua/codecompanion/config.lua

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -882,8 +882,8 @@ The user is working on a %s machine. Please respond with system specific command
882882
full_height = true, -- for vertical layout
883883
position = nil, -- left|right|top|bottom (nil will default depending on vim.opt.splitright|vim.opt.splitbelow)
884884

885-
width = 0.5, ---@type number|"auto" using "auto" will allow full_height buffers to act like normal buffers
886-
height = 0.8,
885+
width = 0.5, ---@return number|fun(): number
886+
height = 0.8, ---@return number|fun(): number
887887
border = "single",
888888
relative = "editor",
889889

@@ -1099,13 +1099,6 @@ M.can_send_code = function()
10991099
return false
11001100
end
11011101

1102-
---Resolve a config value that might be a function or static value
1103-
---@param value any
1104-
---@return any
1105-
function M.resolve_value(value)
1106-
return type(value) == "function" and value() or value
1107-
end
1108-
11091102
return setmetatable(M, {
11101103
__index = function(_, key)
11111104
if key == "setup" then

lua/codecompanion/interactions/chat/ui/init.lua

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,39 @@ function UI:open(opts)
130130
else
131131
window = config.display.chat.window
132132
end
133-
local width = math.floor(vim.o.columns * 0.45)
134-
if window.width ~= "auto" then
135-
width = window.width > 1 and window.width or math.floor(vim.o.columns * window.width)
133+
134+
local function cols()
135+
return vim.o.columns
136+
end
137+
local function rows()
138+
return vim.o.lines
139+
end
140+
141+
--NOTE: Previously we allowed "auto" values for the width, so guarding against that here
142+
if type(window.height) == "string" then
143+
window.height = rows()
144+
end
145+
if type(window.width) == "string" then
146+
window.width = cols()
136147
end
137-
local height = window.height > 1 and window.height or math.floor(vim.o.lines * window.height)
148+
149+
if type(window.height) == "function" then
150+
window.height = window.height()
151+
end
152+
if type(window.width) == "function" then
153+
window.width = window.width()
154+
end
155+
156+
local height = window.height > 1 and window.height or math.floor(rows() * window.height)
157+
local width = window.width > 1 and window.width or math.floor(cols() * window.width)
138158

139159
if window.layout == "float" then
140160
local win_opts = {
141161
relative = window.relative,
142162
width = width,
143163
height = height,
144-
row = window.row or math.floor((vim.o.lines - height) / 2),
145-
col = window.col or math.floor((vim.o.columns - width) / 2),
164+
col = window.col or math.floor((cols() - width) / 2),
165+
row = window.row or math.floor((rows() - height) / 2),
146166
border = window.border,
147167
title = window.title or "CodeCompanion",
148168
title_pos = "center",
@@ -171,9 +191,7 @@ function UI:open(opts)
171191
if position == "right" and not vim.opt.splitright:get() then
172192
vim.cmd("wincmd l")
173193
end
174-
if window.width ~= "auto" then
175-
vim.cmd("vertical resize " .. width)
176-
end
194+
vim.cmd("vertical resize " .. width)
177195
self.winnr = api.nvim_get_current_win()
178196
api.nvim_win_set_buf(self.winnr, self.chat_bufnr)
179197
apply_window_config(self.winnr, self.chat_bufnr, window.opts)

lua/codecompanion/utils/ui.lua

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,34 @@ end
5757
M.create_float = function(lines, opts)
5858
local window = opts.window
5959

60+
---TODO: Remove this and remove all background dimming
6061
-- Create background window for dimming effect if enabled
6162
if opts.show_dim then
6263
M.create_background_window()
6364
end
6465

65-
local config = require("codecompanion.config")
66-
local window_width = config.resolve_value(window.width)
67-
local width = window_width and (window_width > 1 and window_width or opts.width or 85) or opts.width or 85
68-
local window_height = config.resolve_value(window.height)
69-
local height = window_height and (window_height > 1 and window_height or opts.height or 17) or opts.height or 17
66+
local cols = function()
67+
return vim.o.columns
68+
end
69+
local rows = function()
70+
return vim.o.lines
71+
end
72+
73+
if type(window.height) == "function" then
74+
window.height = window.height()
75+
end
76+
if type(window.width) == "function" then
77+
window.width = window.width()
78+
end
79+
if type(window.height) == "string" then
80+
window.height = rows()
81+
end
82+
if type(window.width) == "string" then
83+
window.width = cols()
84+
end
85+
86+
local width = window.width and (window.width > 1 and window.width or opts.width or 85) or opts.width or 85
87+
local height = window.height and (window.height > 1 and window.height or opts.height or 17) or opts.height or 17
7088

7189
local bufnr = opts.bufnr or api.nvim_create_buf(false, true)
7290

@@ -76,10 +94,10 @@ M.create_float = function(lines, opts)
7694
local row = opts.row or window.row or 10
7795
local col = opts.col or window.col or 0
7896
if row == "center" then
79-
row = math.floor((vim.o.lines - height) / 2 - 1) -- Account for status line for better UX
97+
row = math.floor((rows() - height) / 2 - 1) -- Account for status line for better UX
8098
end
8199
if col == "center" then
82-
col = math.floor((vim.o.columns - width) / 2)
100+
col = math.floor((cols() - width) / 2)
83101
end
84102

85103
local winnr = api.nvim_open_win(bufnr, true, {
@@ -293,31 +311,6 @@ function M.scroll_to_line(bufnr, line_num)
293311
end)
294312
end
295313

296-
---Scroll to line and briefly highlight the edit area
297-
---@param bufnr number The buffer number
298-
---@param line_num number The line number to scroll to
299-
---@param num_lines? number Number of lines that were changed
300-
function M.scroll_and_highlight(bufnr, line_num, num_lines)
301-
num_lines = num_lines or 1
302-
303-
M.scroll_to_line(bufnr, line_num)
304-
305-
local ns_id = api.nvim_create_namespace("codecompanion_edit_highlight")
306-
307-
-- Highlight the edited lines
308-
for i = 0, num_lines - 1 do
309-
local highlight_line = line_num + i - 1 -- Convert to 0-based
310-
if highlight_line >= 0 and highlight_line < api.nvim_buf_line_count(bufnr) then
311-
api.nvim_buf_add_highlight(bufnr, ns_id, "DiffAdd", highlight_line, 0, -1)
312-
end
313-
end
314-
315-
-- Clear highlight after a short delay
316-
vim.defer_fn(function()
317-
api.nvim_buf_clear_namespace(bufnr, ns_id, 0, -1)
318-
end, 2000) -- 2 seconds
319-
end
320-
321314
---@param bufnr nil|number
322315
---@return nil|number
323316
M.buf_get_win = function(bufnr)

0 commit comments

Comments
 (0)