@@ -249,8 +249,21 @@ For example, Lua, Ruby, etc."
249249 " Face used to display fringe contents."
250250 :group 'treesit-fold )
251251
252- (defvar-keymap treesit-fold-mode-map
253- :doc " Keymap used when `treesit-fold-mode' is active." )
252+ (defcustom treesit-fold-line-count-show nil
253+ " Show the number of lines in folded text."
254+ :type 'boolean
255+ :group 'treesit-fold )
256+
257+ (defcustom treesit-fold-line-count-format
258+ (concat (truncate-string-ellipsis)
259+ " %d "
260+ (truncate-string-ellipsis))
261+ " Format string for displaying line count in folded text.
262+
263+ The %d will be replaced with the number of lines in the folded region."
264+ :type 'string
265+ :group 'treesit-fold )
266+
254267; ;
255268; ; (@* "Externals" )
256269; ;
@@ -264,6 +277,9 @@ For example, Lua, Ruby, etc."
264277; ; (@* "Entry" )
265278; ;
266279
280+ (defvar-keymap treesit-fold-mode-map
281+ :doc " Keymap used when `treesit-fold-mode' is active." )
282+
267283(defun treesit-fold--enable ()
268284 " Start folding minor mode."
269285 (setq-local line-move-ignore-invisible t )
@@ -388,6 +404,22 @@ This function is borrowed from `tree-sitter-node-at-point'."
388404; ; (@* "Overlays" )
389405; ;
390406
407+ (defun treesit-fold--format-overlay-text (beg end )
408+ " Return the text to display in the overlay for the fold from BEG to END."
409+ (let ((summary (and treesit-fold-summary-show
410+ (treesit-fold-summary--get (buffer-substring beg end)))))
411+ (cond
412+ ; ; Handle line count display.
413+ ((when-let*
414+ ((line-count (and treesit-fold-line-count-show
415+ (count-lines beg end)))
416+ (line-count-str (format treesit-fold-line-count-format line-count)))
417+ (concat (or summary " " ) line-count-str)))
418+ ; ; `summary' handles truncation itself; just return it if not nil.
419+ (summary )
420+ ; ; Fallback to ellipsis.
421+ (t (truncate-string-ellipsis)))))
422+
391423(defun treesit-fold--create-overlay (range )
392424 " Create invisible overlay in RANGE."
393425 (when range
@@ -400,9 +432,7 @@ This function is borrowed from `tree-sitter-node-at-point'."
400432 (overlay-put ov 'priority treesit-fold-priority)
401433 (overlay-put ov 'invisible 'treesit-fold )
402434 (overlay-put ov 'display
403- (propertize (or (and treesit-fold-summary-show
404- (treesit-fold-summary--get (buffer-substring beg end)))
405- (truncate-string-ellipsis))
435+ (propertize (treesit-fold--format-overlay-text beg end)
406436 'mouse-face 'treesit-fold-replacement-mouse-face
407437 'help-echo " mouse-1: unfold this node"
408438 'keymap map))
@@ -434,9 +464,11 @@ This function is borrowed from `tree-sitter-node-at-point'."
434464 (let ((beg (overlay-start ov))
435465 (end (overlay-end ov)))
436466 (overlay-put ov 'invisible 'treesit-fold )
437- (overlay-put ov 'display (or (and treesit-fold-summary-show
438- (treesit-fold-summary--get (buffer-substring beg end)))
439- (truncate-string-ellipsis)))
467+ (overlay-put ov 'display
468+ (propertize (treesit-fold--format-overlay-text beg end)
469+ 'mouse-face 'treesit-fold-replacement-mouse-face
470+ 'help-echo " mouse-1: unfold this node"
471+ 'keymap (overlay-get ov 'keymap )))
440472 (overlay-put ov 'face 'treesit-fold-replacement-face ))
441473 (treesit-fold-indicators-refresh))
442474
0 commit comments