Skip to content

Syntax highlighting lost when expanding collapsed tool output #32

@dnouri

Description

@dnouri

Problem

When expanding collapsed tool output (clicking ... (N more lines)), syntax highlighting is missing. It appears erratically "bit by bit" when scrolling around. This is because JIT font-lock only fontifies visible portions lazily.

Root cause

pi--toggle-tool-output calls pi--insert-expanded-content which inserts markdown code fences but never calls font-lock-ensure. Compare with:

  • pi--finalize-streaming-message (line 1062) - calls font-lock-ensure
  • pi--insert-session-history (line 1814) - calls font-lock-ensure

Implementation hints

Add font-lock-ensure after content insertion in pi--toggle-tool-output:

(defun pi--toggle-tool-output (button)
  "Toggle between preview and full content for BUTTON."
  (let* ((inhibit-read-only t)
         ;; ... existing bindings ...
         )
    (save-excursion
      (when-let ((bounds (pi--find-tool-block-bounds)))
        (goto-char (car bounds))
        (forward-line 1)
        (let ((content-start (point)))
          (delete-region content-start (1+ btn-end))
          (goto-char content-start)
          (if expanded
              (pi--insert-collapsed-content preview-content full-content lang hidden-count)
            (pi--insert-expanded-content preview-content full-content lang hidden-count))
          ;; NEW: Ensure fontification after insertion
          (font-lock-ensure content-start (point)))))))

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions