Skip to content

Commit 8cbb181

Browse files
authored
Size side windows by body width and add test (#273)
1 parent d2defeb commit 8cbb181

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

ai-code-backends-infra.el

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Can be either `vterm' or `eat'."
6262
:group 'ai-code-backends-infra)
6363

6464
(defcustom ai-code-backends-infra-window-width 90
65-
"Width of the side window when opened on left or right."
65+
"Body width of the side window when opened on left or right."
6666
:type 'integer
6767
:group 'ai-code-backends-infra)
6868

@@ -529,7 +529,8 @@ MULTILINE-INPUT-SEQUENCE configures `S-<return>' and `C-<return>' when non-nil."
529529
(side . ,side)
530530
(slot . 0)
531531
,@(when (memq side '(left right))
532-
`((window-width . ,ai-code-backends-infra-window-width)))
532+
`((window-width
533+
. ,#'ai-code-backends-infra--fit-side-window-body-width)))
533534
,@(when (memq side '(top bottom))
534535
`((window-height . ,ai-code-backends-infra-window-height)))
535536
(window-parameters . ((no-delete-other-windows . t)))))))
@@ -540,6 +541,13 @@ MULTILINE-INPUT-SEQUENCE configures `S-<return>' and `C-<return>' when non-nil."
540541
(select-window window))
541542
window))
542543

544+
(defun ai-code-backends-infra--fit-side-window-body-width (window)
545+
"Resize WINDOW so its body width matches `ai-code-backends-infra-window-width'."
546+
(let ((delta (- ai-code-backends-infra-window-width
547+
(window-body-width window))))
548+
(unless (zerop delta)
549+
(window-resize window delta t))))
550+
543551
;;; Session Helpers
544552

545553
(defun ai-code-backends-infra--session-working-directory ()

test/test_ai-code-backends-infra.el

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,30 @@
217217
(advice-remove handler #'ai-code-backends-infra--terminal-reflow-filter))
218218
(fmakunbound handler)))))
219219

220+
(ert-deftest test-ai-code-backends-infra-display-buffer-in-side-window-uses-body-width ()
221+
"Horizontal side windows should size to the configured body width."
222+
(with-temp-buffer
223+
(let ((ai-code-backends-infra-use-side-window t)
224+
(ai-code-backends-infra-window-side 'right)
225+
(ai-code-backends-infra-window-width 100)
226+
(ai-code-backends-infra-focus-on-open nil)
227+
captured-entry
228+
resize-call)
229+
(rename-buffer " *ai-code-side-width*" t)
230+
(cl-letf (((symbol-function 'display-buffer)
231+
(lambda (_buffer &optional _action)
232+
(setq captured-entry (car display-buffer-alist))
233+
'fake-window))
234+
((symbol-function 'window-body-width)
235+
(lambda (_window) 96))
236+
((symbol-function 'window-resize)
237+
(lambda (window delta horizontal)
238+
(setq resize-call (list window delta horizontal)))))
239+
(ai-code-backends-infra--display-buffer-in-side-window (current-buffer))
240+
(should (functionp (cdr (assq 'window-width captured-entry))))
241+
(funcall (cdr (assq 'window-width captured-entry)) 'fake-window)
242+
(should (equal resize-call '(fake-window 4 t)))))))
243+
220244
(ert-deftest test-ai-code-backends-infra-sync-terminal-cursor-vterm-copy-mode ()
221245
"Show an Emacs cursor in vterm copy mode and restore terminal cursor on exit."
222246
(with-temp-buffer

0 commit comments

Comments
 (0)