Skip to content

feat: add line count to folded text overlay #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2025
Merged
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
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ the tree-sitter syntax tree.
- [❔ Example](#-example)
- [↔ Offset](#-offset)
- [🔍 Writing new fold functions](#-writing-new-fold-functions)
- [🔢 Line Count Display](#-line-count-display)
- [🔌 Plugins](#-plugins)
- [⚖ Indicators Mode](#-indicators-mode)
- [💾 Installation](#-installation-1)
Expand Down Expand Up @@ -410,6 +411,48 @@ basic `treesit-fold-range-seq`.
(treesit-fold--cons-add (cons beg end) offset))) ; return fold range
```

### 🔢 Line Count Display

The following variables let you toggle and customize the display of the line count for folded regions.

- `treesit-fold-line-count-show`

This variable controls whether or not the number of lines in a folded text region is displayed.

Type: `boolean`

Default: `nil` (line count is not shown)

If set to `t`, the number of lines in folded regions will be shown.

Example:
```elisp
(setq treesit-fold-line-count-show t) ; Show line count in folded regions
```
<p align="center">
<img src="./etc/line-count-default.png" width="70%" height="70%"/>
</p>

- `treesit-fold-line-count-format`

This variable defines the format string used for displaying the line
count in folded text. The `%d` will be replaced with the actual number
of lines in the folded region.

Type: `string`

Default: `(concat (truncate-string-ellipsis) " %d " (truncate-string-ellipsis))`

Example:

```elisp
(setq treesit-fold-line-count-format " <%d lines> ")
```

<p align="center">
<img src="./etc/line-count-custom.png" width="70%" height="70%"/>
</p>

## 🔌 Plugins

treesit-fold comes with a couple of useful little additions that can be used or
Expand Down
Binary file added etc/line-count-custom.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added etc/line-count-default.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 40 additions & 8 deletions treesit-fold.el
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,21 @@ For example, Lua, Ruby, etc."
"Face used to display fringe contents."
:group 'treesit-fold)

(defvar-keymap treesit-fold-mode-map
:doc "Keymap used when `treesit-fold-mode' is active.")
(defcustom treesit-fold-line-count-show nil
"Show the number of lines in folded text."
:type 'boolean
:group 'treesit-fold)

(defcustom treesit-fold-line-count-format
(concat (truncate-string-ellipsis)
" %d "
(truncate-string-ellipsis))
"Format string for displaying line count in folded text.

The %d will be replaced with the number of lines in the folded region."
:type 'string
:group 'treesit-fold)

;;
;; (@* "Externals" )
;;
Expand All @@ -264,6 +277,9 @@ For example, Lua, Ruby, etc."
;; (@* "Entry" )
;;

(defvar-keymap treesit-fold-mode-map
:doc "Keymap used when `treesit-fold-mode' is active.")

(defun treesit-fold--enable ()
"Start folding minor mode."
(setq-local line-move-ignore-invisible t)
Expand Down Expand Up @@ -388,6 +404,22 @@ This function is borrowed from `tree-sitter-node-at-point'."
;; (@* "Overlays" )
;;

(defun treesit-fold--format-overlay-text (beg end)
"Return the text to display in the overlay for the fold from BEG to END."
(let ((summary (and treesit-fold-summary-show
(treesit-fold-summary--get (buffer-substring beg end)))))
(cond
;; Handle line count display.
((when-let*
((line-count (and treesit-fold-line-count-show
(count-lines beg end)))
(line-count-str (format treesit-fold-line-count-format line-count)))
(concat (or summary "") line-count-str)))
;; `summary' handles truncation itself; just return it if not nil.
(summary )
;; Fallback to ellipsis.
(t (truncate-string-ellipsis)))))

(defun treesit-fold--create-overlay (range)
"Create invisible overlay in RANGE."
(when range
Expand All @@ -400,9 +432,7 @@ This function is borrowed from `tree-sitter-node-at-point'."
(overlay-put ov 'priority treesit-fold-priority)
(overlay-put ov 'invisible 'treesit-fold)
(overlay-put ov 'display
(propertize (or (and treesit-fold-summary-show
(treesit-fold-summary--get (buffer-substring beg end)))
(truncate-string-ellipsis))
(propertize (treesit-fold--format-overlay-text beg end)
'mouse-face 'treesit-fold-replacement-mouse-face
'help-echo "mouse-1: unfold this node"
'keymap map))
Expand Down Expand Up @@ -434,9 +464,11 @@ This function is borrowed from `tree-sitter-node-at-point'."
(let ((beg (overlay-start ov))
(end (overlay-end ov)))
(overlay-put ov 'invisible 'treesit-fold)
(overlay-put ov 'display (or (and treesit-fold-summary-show
(treesit-fold-summary--get (buffer-substring beg end)))
(truncate-string-ellipsis)))
(overlay-put ov 'display
(propertize (treesit-fold--format-overlay-text beg end)
'mouse-face 'treesit-fold-replacement-mouse-face
'help-echo "mouse-1: unfold this node"
'keymap (overlay-get ov 'keymap)))
(overlay-put ov 'face 'treesit-fold-replacement-face))
(treesit-fold-indicators-refresh))

Expand Down