felipeochoa / rjsx-mode

A JSX major mode for Emacs
https://github.com/felipeochoa/rjsx-mode
MIT License
641 stars 32 forks source link

The recommended way to fold jsx tags #134

Open shiyuangu opened 2 years ago

shiyuangu commented 2 years ago

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!

felipeochoa commented 2 years ago

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