felipeochoa / rjsx-mode

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

Electric `>` for blank tags (a.k.a fragments) #112

Open goranmoomin opened 5 years ago

goranmoomin commented 5 years ago

When using rjsx-mode, I really love the electricity feature of the > key. I have found that, when inserting blank tags (a.k.a fragments), the electric > doesn't work, which makes inserting fragments very tedious. rjsx-fragments I would like if the electric > works for blank tags too.

sooqua commented 5 years ago
  (defun +javascript-rjsx-electric-gt-a (n)
    (when (and (looking-back "<>")
               (looking-at-p "/>"))
      (save-excursion (insert "<"))))
  (advice-add #'rjsx-electric-gt :after #'+javascript-rjsx-electric-gt-a)
MageJohn commented 3 years ago

I think this advice is probably faster (not that yours is slow, I just spotted some optimisations):

(defun +rjsx-electric-gt-fragment-a (n)
  (if (or (/= n 1) (not (and (eq (char-before) ?<) (eq (char-after) ?/)))) 't
    (insert ?> ?<)
    (backward-char)))
(advice-add #'rjsx-electric-gt :before-while #'+rjsx-electric-gt-fragment-a)

It uses all low level functions defined in C, and it will also completely skip rjsx-electric-gt if it can.