Skip to content

Commit 0f3340f

Browse files
committed
Improve support for Ruby
1 parent 8cb4874 commit 0f3340f

File tree

2 files changed

+95
-3
lines changed

2 files changed

+95
-3
lines changed

electric-operator.el

+88-3
Original file line numberDiff line numberDiff line change
@@ -1153,12 +1153,97 @@ Also handles C++ lambda capture by reference."
11531153

11541154

11551155

1156-
;;; Other major mode tweaks
1156+
;;; Ruby mode
1157+
1158+
(defun electric-operator-ruby-mode-| ()
1159+
(cond
1160+
;; Start of block params
1161+
;; arr.map { |a| a }
1162+
;; ^^
1163+
;; arr.map do |a| a end
1164+
;; ^^
1165+
((electric-operator-looking-back-locally (rx (or "do" "{") (* whitespace)))
1166+
" |")
1167+
;; End of block params
1168+
;; arr.map { |a| a }
1169+
;; ^^
1170+
;; arr.map do |a| a end
1171+
;; ^^
1172+
((electric-operator-looking-back-locally (rx (or "do" "{") (* whitespace) "|" (* (not (any "|" "\n")))))
1173+
"| ")
1174+
;; Regular operator
1175+
;; 3 | 4
1176+
;; ^^^
1177+
(t
1178+
" | ")))
1179+
(defun electric-operator-ruby-mode-* ()
1180+
(cond
1181+
;; splat
1182+
((electric-operator-probably-unary-operator?) nil)
1183+
((electric-operator-just-inside-bracket) "*")
1184+
;; binary operator
1185+
(t " * ")))
1186+
(defun electric-operator-ruby-mode-** ()
1187+
(cond
1188+
;; keyword argument splat
1189+
((electric-operator-probably-unary-operator?) nil)
1190+
((electric-operator-just-inside-bracket) "**")
1191+
;; binary operator
1192+
(t " ** ")))
1193+
(defun electric-operator-ruby-mode-% ()
1194+
(cond
1195+
;; e.g. %w(a b c)
1196+
((electric-operator-probably-unary-operator?) nil)
1197+
((electric-operator-just-inside-bracket) "%")
1198+
;; binary operator
1199+
(t " % ")))
1200+
(defun electric-operator-ruby-mode-<<- ()
1201+
;; heredoc
1202+
(cond
1203+
((electric-operator-just-inside-bracket) "<<-")
1204+
(t " <<-")))
1205+
(defun electric-operator-ruby-mode-<<~ ()
1206+
;; heredoc
1207+
(cond
1208+
((electric-operator-just-inside-bracket) "<<~")
1209+
(t " <<~")))
11571210

11581211
(apply #'electric-operator-add-rules-for-mode 'ruby-mode (electric-operator-get-rules-for-mode 'prog-mode))
11591212
(electric-operator-add-rules-for-mode 'ruby-mode
1160-
(cons "=~" " =~ ") ; regex equality
1161-
)
1213+
(cons "|" #'electric-operator-ruby-mode-|)
1214+
(cons "*" #'electric-operator-ruby-mode-*)
1215+
(cons "**" #'electric-operator-ruby-mode-**)
1216+
(cons "%" #'electric-operator-ruby-mode-%)
1217+
(cons "<<-" #'electric-operator-ruby-mode-<<-)
1218+
(cons "<<~" #'electric-operator-ruby-mode-<<~)
1219+
(cons "&" nil) ;complicated by &. &: &block
1220+
(cons "?" nil) ;can be boolean_method?() or ternary
1221+
(cons "/" nil) ;complicated by regexes
1222+
(cons "=>" " => ") ;in hashes
1223+
(cons "->(" " ->(") ;stabby lambda with arguments
1224+
(cons "->" " -> ") ;stabby lambda (without arguments)
1225+
(cons ";" "; ") ;statement separator
1226+
(cons "<<" " << ")
1227+
(cons ">>" " >> ")
1228+
(cons "===" " === ") ;case equality operator
1229+
(cons "=~" " =~ ") ;regex equality
1230+
(cons "!~" " !~ ") ;regex inequality
1231+
(cons "<=>" " <=> ") ;spaceship operator (comparison)
1232+
(cons "**=" " **= ")
1233+
(cons "%=" " %= ")
1234+
(cons "<<=" " <<= ")
1235+
(cons ">>=" " >>= ")
1236+
(cons "&&=" " &&= ")
1237+
(cons "&=" " &= ")
1238+
(cons "||=" " ||= ")
1239+
(cons "|=" " |= ")
1240+
(cons "^=" " ^= ")
1241+
)
1242+
1243+
1244+
1245+
;;; Other major mode tweaks
1246+
11621247

11631248
;; This is based on a syntax guide and hasn't been tested.
11641249
(apply #'electric-operator-add-rules-for-mode 'java-mode (electric-operator-get-rules-for-mode 'prog-mode))

test/electric-operator-test.el

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
(require 'electric-operator)
22

3+
(defconst electric-operator--test-dir
4+
(file-name-directory (or load-file-name buffer-file-name)))
5+
36
(ert-deftest trie-create ()
47
(let ((trie (make-electric-operator--trie)))
58
(should (equal (electric-operator--trie-value trie) nil))))
@@ -63,6 +66,10 @@ Aenean in sem ac leo mollis blandit. xyz /=" trie) " /= "))
6366
(nreverse '(" * " ("1" "2" "3") "6" "xyz" "bar"))))))
6467

6568

69+
(ert-deftest electric-operator-ruby ()
70+
(ert-test-erts-file (expand-file-name "ruby.erts" electric-operator--test-dir)))
71+
72+
6673
;; Local Variables:
6774
;; nameless-current-name: "electric-operator"
6875
;; End:

0 commit comments

Comments
 (0)