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

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

Open goranmoomin opened this issue 6 years ago • 2 comments

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.

goranmoomin avatar Jul 28 '19 10:07 goranmoomin

  (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)

sooqua avatar Sep 11 '19 18:09 sooqua

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.

MageJohn avatar Feb 04 '21 23:02 MageJohn