Skip to content
Open
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
11 changes: 10 additions & 1 deletion agent-shell-ui.el
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@
"A hash table used to save sui content like body.
This avoids duplicating body content in text properties which is more costly.")

(defvar agent-shell-ui-post-toggle-fragment-at-point-functions nil
"Hook called after toggling a fragment at point.
Each function is called with three arguments:
BLOCK - the block range alist with :start and :end
BODY - the body range alist with :start and :end
COLLAPSED - non-nil if the fragment is now collapsed")

(cl-defun agent-shell-ui-make-fragment-model (&key (namespace-id "global") (block-id "1") label-left label-right body)
"Create a fragment model alist.
NAMESPACE-ID, BLOCK-ID, LABEL-LEFT, LABEL-RIGHT, and BODY are the keys."
Expand Down Expand Up @@ -529,7 +536,9 @@ When NO-UNDO is non-nil, disable undo recording."
(point) indicator-properties)
(map-put! state :collapsed new-collapsed-state)
(put-text-property (map-elt block :start)
(map-elt block :end) 'agent-shell-ui-state state)))))
(map-elt block :end) 'agent-shell-ui-state state)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agent-shell-ui.el is fairly generic in enabling buffer updates through collapsable fragements. With this change, we'd be introducing client-specific logic.

I'd prefer if we found a way to keep markdown-specific logic in agent-shell.el. Maybe through an agent-shell-ui-post-toggle-fragment-at-point-function (hook) that we funcall from here? Happy to consider alternatives.

(run-hook-with-args 'agent-shell-ui-post-toggle-fragment-at-point-functions
block body new-collapsed-state)))))

(defun agent-shell-ui-collapse-fragment-by-id (namespace-id block-id)
"Collapse fragment with NAMESPACE-ID and BLOCK-ID."
Expand Down
3 changes: 3 additions & 0 deletions agent-shell-viewport.el
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
(declare-function agent-shell-ui-backward-block "agent-shell")
(declare-function agent-shell-ui-forward-block "agent-shell")
(declare-function agent-shell-ui-mode "agent-shell")
(declare-function agent-shell--apply-markdown-overlays-on-body-expand "agent-shell")
(declare-function agent-shell-completion-mode "agent-shell-completion")
(declare-function agent-shell-yank-dwim "agent-shell")

Expand Down Expand Up @@ -1315,6 +1316,8 @@ For example, offer to kill associated shell session."
\\{agent-shell-viewport-view-mode-map}"
(cursor-intangible-mode +1)
(agent-shell-ui-mode +1)
(add-hook 'agent-shell-ui-post-toggle-fragment-at-point-functions
#'agent-shell--apply-markdown-overlays-on-body-expand nil t)
(agent-shell-viewport--update-header)
(setq-local filter-buffer-substring-function #'agent-shell--filter-buffer-substring)
(setq buffer-read-only t)
Expand Down
35 changes: 25 additions & 10 deletions agent-shell.el
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,15 @@ See https://github.com/xenodium/agent-shell/issues/119"
:type 'boolean
:group 'agent-shell)

(defun agent-shell--apply-markdown-overlays-on-body-expand (_block body collapsed)
"Apply markdown overlays when BODY is expanded (COLLAPSED is nil).
Intended for use with `agent-shell-ui-post-toggle-fragment-at-point-functions'."
(unless collapsed
(save-restriction
(narrow-to-region (map-elt body :start) (map-elt body :end))
(let ((markdown-overlays-highlight-blocks agent-shell-highlight-blocks))
(markdown-overlays-put)))))

(defcustom agent-shell-confirm-interrupt t
"Whether to prompt for confirmation before interrupting.

Expand Down Expand Up @@ -2816,6 +2825,8 @@ variable (see makunbound)"))
(add-hook 'kill-buffer-hook #'agent-shell--clean-up nil t)
(add-hook 'change-major-mode-hook #'agent-shell--clean-up nil t)
(agent-shell-ui-mode +1)
(add-hook 'agent-shell-ui-post-toggle-fragment-at-point-functions
#'agent-shell--apply-markdown-overlays-on-body-expand nil t)
(when agent-shell-file-completion-enabled
(agent-shell-completion-mode +1))
(agent-shell--setup-modeline)
Expand Down Expand Up @@ -3003,9 +3014,11 @@ by default, RENDER-BODY-IMAGES to enable inline image rendering in body."
(when-let ((body-start (map-nested-elt range '(:body :start)))
(body-end (map-nested-elt range '(:body :end))))
(narrow-to-region body-start body-end)
(let ((markdown-overlays-highlight-blocks agent-shell-highlight-blocks)
(markdown-overlays-render-images render-body-images))
(markdown-overlays-put))))
;; Skip overlays when body is collapsed.
(unless (text-property-any (point-min) (point-max) 'invisible t)
(let ((markdown-overlays-highlight-blocks agent-shell-highlight-blocks)
(markdown-overlays-render-images render-body-images))
(markdown-overlays-put)))))
;; Note: For now, we're skipping applying markdown overlays
;; on left labels as they currently carry propertized text
;; for statuses (ie. boxed).
Expand Down Expand Up @@ -3053,13 +3066,15 @@ by default, RENDER-BODY-IMAGES to enable inline image rendering in body."
;; `agent-shell-next-item' and `agent-shell-previous-item'.
(add-text-properties (or padding-start block-start)
(or padding-end block-end) '(field output)))
;; Apply markdown overlay to body.
(when-let ((body-start (map-nested-elt range '(:body :start)))
(body-end (map-nested-elt range '(:body :end))))
(narrow-to-region body-start body-end)
(let ((markdown-overlays-highlight-blocks agent-shell-highlight-blocks))
(markdown-overlays-put))
(widen))
;; Apply markdown overlay to body.
(when-let ((body-start (map-nested-elt range '(:body :start)))
(body-end (map-nested-elt range '(:body :end))))
(narrow-to-region body-start body-end)
;; Skip overlays when body is collapsed.
(unless (text-property-any (point-min) (point-max) 'invisible t)
(let ((markdown-overlays-highlight-blocks agent-shell-highlight-blocks))
(markdown-overlays-put)))
(widen))
;;
;; Note: For now, we're skipping applying markdown overlays
;; on left labels as they currently carry propertized text
Expand Down