66
77[ ![ CI] ( https://github.com/jcs-elpa/ts-fold/actions/workflows/test.yml/badge.svg )] ( https://github.com/jcs-elpa/ts-fold/actions/workflows/test.yml )
88
9- ts-fold builds on top of [ elisp-tree-sitter] ( https://github.com/emacs-tree-sitter/elisp-tree-sitter )
10- to provide code folding base on the tree-sitter syntax tree.
9+ ` ts-fold ` builds on top of [ elisp-tree-sitter] ( https://github.com/emacs-tree-sitter/elisp-tree-sitter )
10+ to provide code folding based on the tree-sitter syntax tree.
1111
1212<p align =" center " >
1313<img src =" ./etc/screenshot.png " width =" 80% " height =" 80% " />
1414</p >
1515
16+ <!-- Markdown is not able to render links with unicode so after refreshing the toc, select it and:
17+ `M-x regexp-replace #[^a-zA-Z] <ret> # <ret>` -->
18+
1619<!-- markdown-toc start - Don't edit this section. Run M-x markdown-toc-refresh-toc -->
1720** Table of Contents**
1821
1922- [ ts-fold] ( #ts-fold )
20- - [ 💾 Installation] ( #💾 -installation )
21- - [ 🔍 Methods 1. with ` straight.el ` and ` use-package ` :] ( #🔍-methods -1-with-straightel-and-use-package )
22- - [ 🔍 Methods 2. Manual] ( #🔍-methods -2-manual )
23- - [ 📇 Commands] ( #📇 -commands )
24- - [ 🔨 Supported languages] ( #🔨 -supported-languages )
25- - [ ⚖️ Indicators Mode] ( #⚖ ️-indicators-mode )
26- - [ 📝 Summary] ( #📝 -summary )
27- - [ 🔰 Contribute] ( #🔰 -contribute )
28- - [ ❓ How to create a folding parser?] ( #❓ -how-to-create-a-folding-parser )
29- - [ 🔍 Where can I look for tree-sitter node?] ( #🔍 -where-can-i-look-for-tree-sitter-node )
30- - [ 🔍 How do I create the function for the corresponding node?] ( #🔍 -how-do-i-create-the-function-for-the-corresponding-node )
31- - [ 🔍 Register to folding parsers alist!] ( #🔍 -register-to -folding-parsers-alist )
32- - [ ❓ How to create a summary parser?] ( #❓ -how-to-create-a-summary-parser )
33- - [ 🔍 Register to summary parsers alist!] ( #🔍 -register-to-summary-parsers-alist )
23+ - [ 💾 Installation] ( #-installation )
24+ - [ 🔍 Method 1. with ` straight.el ` and ` use-package ` :] ( #-method -1-with-straightel-and-use-package )
25+ - [ 🔍 Method 2. Manual] ( #-method -2-manual )
26+ - [ 📇 Commands] ( #-commands )
27+ - [ 🔨 Supported languages] ( #-supported-languages )
28+ - [ ⚖️ Indicators Mode] ( #️-indicators-mode )
29+ - [ 📝 Summary] ( #-summary )
30+ - [ 🔰 Contribute] ( #-contribute )
31+ - [ ❓ How to create a folding parser?] ( #-how-to-create-a-folding-parser )
32+ - [ 🔍 Where can I look for tree-sitter node?] ( #-where-can-i-look-for-tree-sitter-node )
33+ - [ 🔍 How do I create the function for the corresponding node?] ( #-how-do-i-create-the-function-for-the-corresponding-node )
34+ - [ 🔍 Register in the folding parsers alist!] ( #-register-in-the -folding-parsers-alist )
35+ - [ ❓ How to create a summary parser?] ( #-how-to-create-a-summary-parser )
36+ - [ 🔍 Register to summary parsers alist!] ( #-register-to-summary-parsers-alist )
3437
3538<!-- markdown-toc end -->
3639
3740## 💾 Installation
3841
39- ### 🔍 Methods 1. with ` straight.el ` and ` use-package ` :
42+ ### 🔍 Method 1. with ` straight.el ` and ` use-package ` :
4043
41- ``` el
44+ ``` elisp
4245(use-package ts-fold
4346 :straight (ts-fold :type git :host github :repo "jcs-elpa/ts-fold"))
4447```
4548
46- ### 🔍 Methods 2. Manual
49+ ### 🔍 Method 2. Manual
4750
4851``` sh
4952git clone https://github.com/jcs-elpa/ts-fold /path/to/lib
5053```
5154
5255then in Emacs:
5356
54- ``` el
57+ ``` elisp
5558(add-to-list 'load-path "/path/to/lib")
5659(require ts-fold)
5760```
5861
62+ or
63+
64+ ``` elisp
65+ (use-package ts-fold
66+ :load-path "/path/to/lib")
67+ ```
68+
5969## 📇 Commands
6070
6171| Commands | Description |
@@ -98,77 +108,96 @@ These languages are in development:
98108<img src="./etc/indicators.png" width="40%" height=480%"/>
99109</p >
100110
101- You can enable this manually by doing the folloiwng
111+ You need to load ` ts-fold-indicators-mode ` :
112+ - ` use-package `
113+ ``` elisp
114+ (use-package ts-fold-indicators
115+ :straight (ts-fold-indicators :type git :host github :repo "jcs-elpa/ts-fold"))
116+ ```
117+
118+ -
119+ ``` elisp
120+ (add-to-list 'load-path "/path/to/lib")
121+ (require ts-fold)
122+ ```
123+ or
124+
125+ ``` elisp
126+ (use-package ts-fold-indicators
127+ :load-path "/path/to/lib")
128+ ```
129+
130+ You can then enable this manually by doing the following
102131
103132```
104133M-x ts-fold-indicators-mode
105134```
106135
107- To enable this automatically whenever ` tree-sitter-mode ` is enabled:
136+ - To enable this automatically whenever ` tree-sitter-mode ` is enabled:
108137
109- ``` el
110- (add-hook 'tree-sitter-after-on-hook #ts-fold-indicators-mode)
111- ```
138+ ``` el
139+ (add-hook 'tree-sitter-after-on-hook #ts-fold-indicators-mode)
140+ ```
112141
113- To switch to left/right fringe: (Default is ` left-fringe ` )
142+ - To switch to left/right fringe: (Default is ` left-fringe ` )
114143
115- ``` el
116- (setq ts-fold-indicators-fringe 'right-fringe)
117- ```
144+ ``` el
145+ (setq ts-fold-indicators-fringe 'right-fringe)
146+ ```
118147
119- To lower/higher the fringe overlays : (Default is ` 30 ` )
148+ - To lower/higher the fringe overlay's priority : (Default is ` 30 ` )
120149
121- ``` el
122- (setq ts-fold-indicators-priority 30)
123- ```
150+ ``` el
151+ (setq ts-fold-indicators-priority 30)
152+ ```
124153
125- To apply different face depends on some conditions: (Default is ` nil ` )
154+ - To apply different faces depending on some conditions: (Default is ` nil ` )
126155
127- For example, to coordinate [ line-reminder] ( https://github.com/emacs-vs/line-reminder )
156+ For example, to coordinate [ line-reminder] ( https://github.com/emacs-vs/line-reminder )
128157with this plugin.
129158
130- ``` el
131- (setq ts-fold-indicators-face-function
159+ ``` elisp
160+ (setq ts-fold-indicators-face-function
132161 (lambda (pos &rest _)
133162 (let ((ln (line-number-at-pos pos)))
134163 (cond
135164 ((memq ln line-reminder--change-lines) 'line-reminder-modified-sign-face)
136165 ((memq ln line-reminder--saved-lines) 'line-reminder-saved-sign-face)
137166 (t nil)))))
138- ```
167+ ```
139168
140169## 📝 Summary
141170
142171<p align =" center " >
143172<img src =" ./etc/summary.gif " width =" 80% " height =" 80% " />
144173</p >
145174
146- This plugin automatically extract summary from the comment/document string,
147- so you can have a nice way to peek what's inside the fold range.
175+ This plugin automatically extracts summary from the comment/document string,
176+ so you can have a nice way to peek at what's inside the fold range.
148177
149- If you don't want this to happen, do: (Default is ` t ` )
178+ - If you don't want this to happen, do: (Default is ` t ` )
150179
151- ``` el
152- (setq ts-fold-summary-show nil)
153- ```
180+ ``` elisp
181+ (setq ts-fold-summary-show nil)
182+ ```
154183
155- Summary are truncated by length: (Default is ` 60 ` )
184+ - Summary are truncated by length: (Default is ` 60 ` )
156185
157- ``` el
158- (setq ts-fold-summary-max-length 60)
159- ```
186+ ``` elisp
187+ (setq ts-fold-summary-max-length 60)
188+ ```
160189
161- The exceeding string are replace by: (Default is ` "..." ` )
190+ - The exceeding string are replace by: (Default is ` "..." ` )
162191
163- ``` el
164- (setq ts-fold-summary-exceeded-string "...")
165- ```
192+ ``` elisp
193+ (setq ts-fold-summary-exceeded-string "...")
194+ ```
166195
167- To change summary format: (Default is ` " <S> %s " ` )
196+ - To change summary format: (Default is ` " <S> %s " ` )
168197
169- ``` el
170- (setq ts-fold-summary-format " <S> %s ")
171- ```
198+ ``` elisp
199+ (setq ts-fold-summary-format " <S> %s ")
200+ ```
172201
173202## 🔰 Contribute
174203
@@ -182,12 +211,12 @@ has an excellent documentation on how to write `tree-sitter` queries.
182211
183212### ❓ How to create a folding parser?
184213
185- Parsers are ruled in the ` ts-fold-parsers.el ` file. Parser function follow
186- with the prefix ` ts-fold-parsers- ` plus the ` language name ` . For example, if
187- you want to create a parser for ` C ` programming language. It should be named
214+ Parsers are defined in the ` ts-fold-parsers.el ` file. Parser functions are named
215+ with the prefix ` ts-fold-parsers- ` followed by the ` language name ` . For example, if
216+ you want to create a parser for the ` C ` programming language you should name it
188217` ts-fold-parsers-c ` .
189218
190- The parser is consist of an association list (alist), each item is consist
219+ Parsers are association lists (alist) whose items consist
191220of tree-sitter ` node ` and a function that returns the folding range. See
192221the following example:
193222
@@ -198,49 +227,50 @@ the following example:
198227 ...))
199228```
200229
201- ` block ` is the tree-sitter node, and ` ts-fold-range-seq ` is the function
230+ ` block ` is the tree-sitter node and ` ts-fold-range-seq ` is the function
202231that will return the folding range.
203232
204233Let's move into details,
205234
206235#### 🔍 Where can I look for tree-sitter node?
207236
208- To look for the correct node, you should look at the ` tree-sitter-[lang]/grammar.js `
209- implementation. In the above example, ` block ` node is defined in the
210- [ tree-sitter-c-sharp] ( https://github.com/tree-sitter/tree-sitter-c-sharp ) 's
211- ` grammar.js ` file.
237+ To look for the correct node you have three options:
238+ - look at the ` tree-sitter-[lang]/grammar.js ` implementation. In the above example, ` block ` node is defined in the [ tree-sitter-c-sharp] ( https://github.com/tree-sitter/tree-sitter-c-sharp ) 's
239+ ` grammar.js ` file
240+ - open a file of your language choice in emacs and ` M-x tree-sitter-debug-mode ` . This will display the whole s-expr representing your file
241+ - ` (message "%S" (tsc-node-to-sexp)) ` in your function to display what your function is seeing
212242
213243> ⚠️ Warning
214244>
215245> Make sure you look into the correct repository. Repositories are managed
216246> under [ tree-sitter-langs] ( https://github.com/emacs-tree-sitter/tree-sitter-langs ) 's
217- > using the git submodule. Some tree-sitter module aren't using the latest version!
247+ > using git submodule. Some tree-sitter module aren't using the latest version!
218248
219249#### 🔍 How do I create the function for the corresponding node?
220250
221- Function takes 2 arguments, ` node ` and ` offset ` .
251+ Function take 2 arguments, ` node ` and ` offset ` .
222252
223- * ` node ` - the targeting tree-sitter node; in this example, ` block ` will be the
253+ * ` node ` - the targeted tree-sitter node; in this example, ` block ` will be the
224254targeting node.
225- * ` offset ` - (optional) a cons consist of two integers. This is handy when you have
255+ * ` offset ` - (optional) a cons of two integers. This is handy when you have
226256a similar rule with little of positioning adjustment.
227257
228- ` tree-sitter-[lang] ` parsers are generally integrated with different authors,
258+ ` tree-sitter-[lang] ` parsers are generally integrated by different authors,
229259hence their naming and ruling are slightly different (+1/-1 position).
230260
231- Let's look at function ` ts-fold-range-seq ` for better understanding,
261+ Let's look at function ` ts-fold-range-seq ` for better understanding,
232262
233- ``` elisp
234- (defun ts-fold-range-seq (node offset)
235- "..."
236- (let ((beg (1+ (tsc-node-start-position node))) ; node beginning position (from Rust layer)
237- (end (1- (tsc-node-end-position node)))) ; node end position (from Rust layer)
238- (ts-fold--cons-add (cons beg end) offset))) ; return fold range
239- ```
263+ ``` elisp
264+ (defun ts-fold-range-seq (node offset)
265+ "..."
266+ (let ((beg (1+ (tsc-node-start-position node))) ; node beginning position (from Rust layer)
267+ (end (1- (tsc-node-end-position node)))) ; node end position (from Rust layer)
268+ (ts-fold--cons-add (cons beg end) offset))) ; return fold range
269+ ```
240270
241- #### 🔍 Register to folding parsers alist!
271+ #### 🔍 Register in the folding parsers alist!
242272
243- Don't forget to add your parser to the entry alist with the corresponding
273+ Don't forget to add your parser to the entry alist with its corresponding
244274` major-mode ` .
245275
246276``` elisp
@@ -256,32 +286,32 @@ This variable is defined in package main file, `ts-fold.el`.
256286
257287### ❓ How to create a summary parser?
258288
259- ` ts-fold-summary.el ` module is use to extract and display a short description
289+ ` ts-fold-summary.el ` module is used to extract and display a short description
260290from the comment/docstring.
261291
262- To create summary parser, you would just have to create a function that could
263- remove comment syntax correctly, then register it to ` ts-fold-summary-parsers-alist `
264- defined in ` ts-fold-summary.el ` . The display and shortening will be handled
265- by the module itself.
292+ To create a summary parser, you just have to create a function that could
293+ extract comment syntax correctly then register this function to
294+ ` ts-fold-summary-parsers-alist ` defined in ` ts-fold-summary.el ` .
295+ The display and shortening will be handled by the module itself.
266296
267- Function should follow with prefix ` ts-fold-summary- ` and the ` style name ` .
268- For example, to create summary parser for Javadoc style, then it should be
297+ Functions should be named with the prefix ` ts-fold-summary- ` followed by ` style name ` .
298+ For example, to create a summary parser for Javadoc style, then it should be
269299named ` ts-fold-summary-javadoc ` .
270300
271301Let's see the implementation,
272302
273303``` elisp
274304(defun ts-fold-summary-javadoc (doc-str)
275305 "..."
276- (ts-fold-summary--generic doc-str "*")) ; strip all asterisk
306+ (ts-fold-summary--generic doc-str "*")) ; strip all asterisks
277307```
278308
279309The above summary parser for Javadoc simply remove ` * ` from any given point.
280310
281311#### 🔍 Register to summary parsers alist!
282312
283313Like folding parsers, you should register your summary parser to the entry alist
284- with the corresponding ` major-mode ` .
314+ with its corresponding ` major-mode ` .
285315
286316``` elisp
287317(defcustom ts-fold-summary-parsers-alist
0 commit comments