Skip to content

Commit 443ba51

Browse files
authored
feat(ruby): Enhance Ruby parser (#26)
* feat(ruby): Add fold range for do_block node * feat(ruby): support if, elsif, and else
1 parent 17d131f commit 443ba51

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

ts-fold-parsers.el

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
(declare-function ts-fold-range-html "ts-fold.el")
4848
(declare-function ts-fold-range-ocaml "ts-fold.el")
4949
(declare-function ts-fold-range-python "ts-fold.el")
50-
(declare-function ts-fold-range-ruby "ts-fold.el")
50+
(declare-function ts-fold-range-ruby-class-def "ts-fold.el")
51+
(declare-function ts-fold-range-ruby-if "ts-fold.el")
5152
(declare-function ts-fold-range-rust-macro "ts-fold.el")
5253
(declare-function ts-fold-range-elixir "ts-fold.el")
5354

@@ -194,9 +195,13 @@
194195

195196
(defun ts-fold-parsers-ruby ()
196197
"Rule set for Ruby."
197-
'((class . ts-fold-range-ruby)
198-
(method . ts-fold-range-ruby)
199-
(array . ts-fold-range-seq)
198+
'((class . ts-fold-range-ruby-class-def)
199+
(method . ts-fold-range-ruby-class-def)
200+
(array . ts-fold-range-seq)
201+
(do . (ts-fold-range-seq 1 -2)) ; match with `end`
202+
(do_block . (ts-fold-range-seq 1 -2)) ; match with `end`, in spec file
203+
(then . ts-fold-range-ruby-if) ; `if` and `elsif` block
204+
(else . (ts-fold-range-ruby-if 4 0)) ; `else` block
200205
(comment
201206
. (lambda (node offset)
202207
(ts-fold-range-line-comment node offset "#")))))

ts-fold.el

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ more information."
550550
(end (tsc-node-end-position node)))
551551
(ts-fold--cons-add (cons beg end) offset)))
552552

553-
(defun ts-fold-range-ruby (node offset)
553+
(defun ts-fold-range-ruby-class-def (node offset)
554554
"Define fold range for `method' and `class' in Ruby.
555555
556556
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
@@ -560,6 +560,15 @@ more information."
560560
(tsc-get-child-by-field node :name)))
561561
(beg (tsc-node-end-position named-node))
562562
(end (tsc-node-end-position node)))
563+
(ts-fold--cons-add (cons beg (- end 3)) offset)))
564+
565+
(defun ts-fold-range-ruby-if (node offset)
566+
"Define fold range for `if' (then), `elsif', and `else' in Ruby.
567+
568+
For arguments NODE and OFFSET, see function `ts-fold-range-seq' for
569+
more information."
570+
(when-let ((beg (tsc-node-start-position node))
571+
(end (tsc-node-end-position node)))
563572
(ts-fold--cons-add (cons beg end) offset)))
564573

565574
(defun ts-fold-range-rust-macro (node offset)

0 commit comments

Comments
 (0)