Skip to content

Commit 2c70f4d

Browse files
authored
feat: add line count to folded text overlay (#26)
1 parent 668ca15 commit 2c70f4d

File tree

4 files changed

+83
-8
lines changed

4 files changed

+83
-8
lines changed

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ the tree-sitter syntax tree.
3232
- [❔ Example](#-example)
3333
- [↔ Offset](#-offset)
3434
- [🔍 Writing new fold functions](#-writing-new-fold-functions)
35+
- [🔢 Line Count Display](#-line-count-display)
3536
- [🔌 Plugins](#-plugins)
3637
- [⚖ Indicators Mode](#-indicators-mode)
3738
- [💾 Installation](#-installation-1)
@@ -410,6 +411,48 @@ basic `treesit-fold-range-seq`.
410411
(treesit-fold--cons-add (cons beg end) offset))) ; return fold range
411412
```
412413
414+
### 🔢 Line Count Display
415+
416+
The following variables let you toggle and customize the display of the line count for folded regions.
417+
418+
- `treesit-fold-line-count-show`
419+
420+
This variable controls whether or not the number of lines in a folded text region is displayed.
421+
422+
Type: `boolean`
423+
424+
Default: `nil` (line count is not shown)
425+
426+
If set to `t`, the number of lines in folded regions will be shown.
427+
428+
Example:
429+
```elisp
430+
(setq treesit-fold-line-count-show t) ; Show line count in folded regions
431+
```
432+
<p align="center">
433+
<img src="./etc/line-count-default.png" width="70%" height="70%"/>
434+
</p>
435+
436+
- `treesit-fold-line-count-format`
437+
438+
This variable defines the format string used for displaying the line
439+
count in folded text. The `%d` will be replaced with the actual number
440+
of lines in the folded region.
441+
442+
Type: `string`
443+
444+
Default: `(concat (truncate-string-ellipsis) " %d " (truncate-string-ellipsis))`
445+
446+
Example:
447+
448+
```elisp
449+
(setq treesit-fold-line-count-format " <%d lines> ")
450+
```
451+
452+
<p align="center">
453+
<img src="./etc/line-count-custom.png" width="70%" height="70%"/>
454+
</p>
455+
413456
## 🔌 Plugins
414457
415458
treesit-fold comes with a couple of useful little additions that can be used or

etc/line-count-custom.png

41.1 KB
Loading

etc/line-count-default.png

35.4 KB
Loading

treesit-fold.el

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)