branch: elpa/treesit-fold commit 2c70f4d471f3ea820e0721c8cd7e2947c6e4c78d Author: Jen-Chieh Shen <jcs090...@gmail.com> Commit: GitHub <nore...@github.com>
feat: add line count to folded text overlay (#26) --- README.md | 43 ++++++++++++++++++++++++++++++++++++++++ etc/line-count-custom.png | Bin 0 -> 42042 bytes etc/line-count-default.png | Bin 0 -> 36243 bytes treesit-fold.el | 48 +++++++++++++++++++++++++++++++++++++-------- 4 files changed, 83 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index fe03650a45..394f03333b 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 diff --git a/etc/line-count-custom.png b/etc/line-count-custom.png new file mode 100644 index 0000000000..261b3cfccd Binary files /dev/null and b/etc/line-count-custom.png differ diff --git a/etc/line-count-default.png b/etc/line-count-default.png new file mode 100644 index 0000000000..ed31036697 Binary files /dev/null and b/etc/line-count-default.png differ diff --git a/treesit-fold.el b/treesit-fold.el index 3bd2027634..7ff5494982 100644 --- a/treesit-fold.el +++ b/treesit-fold.el @@ -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" ) ;; @@ -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) @@ -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 @@ -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)) @@ -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))