Skip to content

Commit ca88284

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

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,27 @@ 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 starts at
397+
the end of the first occurence of START-SEQ and ends at the end of the node
398+
or the last occurence of the optional parameter LAST-SEQ.
399+
400+
401+
If no occurence is found for START-SEQ or END-SEQ or the
402+
occurences overlap, then the range returned is nil."
403+
(when start-seq
404+
(let ((beg (tsc-node-start-position node))
405+
(end (tsc-node-end-position node)))
406+
(save-excursion
407+
(when-let* ((start-position (progn (goto-char beg)
408+
(search-forward start-seq end t)))
409+
(end-position (if end-seq
410+
(progn (goto-char end)
411+
(search-backward end-seq beg t))
412+
(1- end))))
413+
(unless (> start-position end-position)
414+
(ts-fold--cons-add (cons start-position end-position) offset)))))))
415+
395416
(defun ts-fold-range-line-comment (node offset prefix)
396417
"Define fold range for line comment.
397418

0 commit comments

Comments
 (0)