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

Command to wrap region in an element

Open felipeochoa opened this issue 6 years ago • 1 comments

It would be nice to have a structure-aware command to wrap the region with a new JSX element, similar to web-mode-element-wrap, but preserving a valid JSX file.

felipeochoa avatar Oct 09 '17 12:10 felipeochoa

I use a very naive function to it, it does not check at all if it is a valid JSX file after (I am not sure if it's easy to check JSX validity), I am not very good at eLisp.

(defun rjsx-region-wrap (node)
  "Wrap an region with NODE tag"
  (interactive "sTag wrapper: ")
  (if (region-active-p)
      (save-mark-and-excursion
       (progn
         (if (< (mark) (point)) (exchange-point-and-mark))
         (insert (concat "<" node ">"))
         (exchange-point-and-mark)
         (insert (concat "</" node ">"))))
    (message "no active region")))

(define-key rjsx-mode-map (kbd "C-c C-w") 'rjsx-region-wrap)

yoLotus avatar May 16 '18 15:05 yoLotus