rjsx-mode icon indicating copy to clipboard operation
rjsx-mode copied to clipboard

The recommended way to fold jsx tags

Open shiyuangu opened this issue 1 year ago • 1 comments

What is the recommended way to fold jsx tags using rjsx-mode? I tried hide-show minor mode and origami but they all messed up the tags. Thank you!

shiyuangu avatar Jul 25 '22 06:07 shiyuangu

I don't use folding much in JSX, but I do use fold-this in other modes. Here's a quick-and-dirty folding function you can try:

(defun rjsx-fold-tag-at-point ()
  (interactive)
  (js2-mode-wait-for-parse
   (lambda ()
     (let* ((node (rjsx--tag-at-point))
            (child0 (car (rjsx-node-kids node)))
            (closer (and node (rjsx-node-closing-tag node))))
       (cond
        ((null node) (message "No JSX tag found at point"))
        ((null closer) (message "JSX tag is void"))
        ((null child0) (message "JSX tag has no children"))
        (t (let* ((start (js2-node-abs-pos child0))
                  (end (js2-node-abs-pos closer)))
             (fold-this start end))))))))

It doesn't handle newlines, but should be enough to get you started!

As usual, PRs welcome for anyone wanting to add to rjsx core

felipeochoa avatar Nov 23 '22 01:11 felipeochoa