Skip to content

Commit dd4da1b

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

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

ts-fold-parsers.el

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,13 @@
123123
(defun ts-fold-parsers-go ()
124124
"Rule set for Go."
125125
'((block . ts-fold-range-seq)
126-
(comment . 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)