Skip to content

Commit 66324eb

Browse files
committed
Add extra folding definitions for golang
1 parent fe568eb commit 66324eb

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

README.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,30 @@ For the folding functions, ts-fold provides some default
185185
int main() {...} // Folded node
186186
```
187187
188+
- `ts-fold-range-markers` - Folds the node starting from a giving delimiter
189+
character. Useful if tree-sitter's node definition doesn't align with the
190+
start of the desired folding section.
191+
192+
**NOTE:** This folding function requires a lambda (or an externally
193+
defined function wrapper) so that the delimiter can be specified. You
194+
usually don't need to worry about the `node` and `offset` variables, so just
195+
pass them through.
196+
197+
```go
198+
type Dog interface {
199+
Bark() (string, error)
200+
Beg() (bool, error)
201+
}
202+
203+
/* | Note: The tree-sitter node starts at the word interface, not at the '{'.
204+
* | '(interface_type . (lambda (node offset)
205+
* | (ts-fold-range-markers node offset "{" "}")))
206+
* V
207+
*/
208+
209+
type Dog interface {...}
210+
```
211+
188212
- `ts-fold-range-block-comment` - Folds multi-line comments that are of the form
189213
`/*...*/`. Should show a summary if the commentary plugin is turned on.
190214

@@ -210,8 +234,10 @@ For the folding functions, ts-fold provides some default
210234
211235
- `ts-fold-range-line-comment` - For languages that have one line comment blocks
212236
with the comment delimiter starting each line. Condenses all the comment nodes
213-
into a single fold. This folding function requires a lambda (or an externally
214-
defined function wrapper) so that the comment delimiter can be specified. You
237+
into a single fold.
238+
239+
**Note:** This folding function requires a lambda (or an externally
240+
defined function wrapper) so that the delimiter can be specified. You
215241
usually don't need to worry about the `node` and `offset` variables, so just
216242
pass them through.
217243

ts-fold-parsers.el

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,14 @@
122122

123123
(defun ts-fold-parsers-go ()
124124
"Rule set for Go."
125-
'((block . ts-fold-range-seq)
126-
(comment . ts-fold-range-seq)))
125+
'((block . ts-fold-range-seq)
126+
(comment . ts-fold-range-seq)
127+
(const_declaration . (lambda (node offset)
128+
(ts-fold-range-markers node offset "(" ")")))
129+
(field_declaration_list . ts-fold-range-seq)
130+
(import_spec_list . ts-fold-range-seq)
131+
(interface_type . (lambda (node offset)
132+
(ts-fold-range-markers node offset "{" "}")))))
127133

128134
(defun ts-fold-parsers-html ()
129135
"Rule set for HTML."

ts-fold.el

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,28 @@ Argument OFFSET can be used to tweak the final beginning and end position."
392392
(end (1- (tsc-node-end-position node))))
393393
(ts-fold--cons-add (cons beg end) offset)))
394394

395+
(defun ts-fold-range-markers (node offset start-seq &optional end-seq)
396+
"Returns the fold range for NODE with an OFFSET where the range
397+
starts at the end of the first occurence of the regular expression
398+
START-SEQ and ends at the end of the node or the last occurence of
399+
the optional regular expression LAST-SEQ.
400+
401+
402+
If no occurence is found for START-SEQ or END-SEQ or the
403+
occurences overlap, then the range returned is nil."
404+
(when start-seq
405+
(let ((beg (tsc-node-start-position node))
406+
(end (tsc-node-end-position node)))
407+
(save-excursion
408+
(when-let* ((start-position (progn (goto-char beg)
409+
(search-forward-regexp start-seq end t)))
410+
(end-position (if end-seq
411+
(progn (goto-char end)
412+
(search-backward-regexp end-seq beg t))
413+
(1- end))))
414+
(unless (> start-position end-position)
415+
(ts-fold--cons-add (cons start-position end-position) offset)))))))
416+
395417
(defun ts-fold-range-line-comment (node offset prefix)
396418
"Define fold range for line comment.
397419

0 commit comments

Comments
 (0)