Skip to content

Commit

Permalink
Improve support for Ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
AjaiKN committed Nov 11, 2024
1 parent 8cb4874 commit 733d56e
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 3 deletions.
91 changes: 88 additions & 3 deletions electric-operator.el
Original file line number Diff line number Diff line change
Expand Up @@ -1153,12 +1153,97 @@ Also handles C++ lambda capture by reference."



;;; Other major mode tweaks
;;; Ruby mode

(defun electric-operator-ruby-mode-| ()
(cond
;; Start of block params
;; arr.map { |a| a }
;; ^^
;; arr.map do |a| a end
;; ^^
((electric-operator-looking-back-locally (rx (or "do" "{") (* whitespace)))
" |")
;; End of block params
;; arr.map { |a| a }
;; ^^
;; arr.map do |a| a end
;; ^^
((electric-operator-looking-back-locally (rx (or "do" "{") (* whitespace) "|" (* (not (any "|" "\n")))))
"| ")
;; Regular operator
;; 3 | 4
;; ^^^
(t
" | ")))
(defun electric-operator-ruby-mode-* ()
(cond
;; splat
((electric-operator-probably-unary-operator?) nil)
((electric-operator-just-inside-bracket) "*")
;; binary operator
(t " * ")))
(defun electric-operator-ruby-mode-** ()
(cond
;; keyword argument splat
((electric-operator-probably-unary-operator?) nil)
((electric-operator-just-inside-bracket) "**")
;; binary operator
(t " ** ")))
(defun electric-operator-ruby-mode-% ()
(cond
;; e.g. %w(a b c)
((electric-operator-probably-unary-operator?) nil)
((electric-operator-just-inside-bracket) "%")
;; binary operator
(t " % ")))
(defun electric-operator-ruby-mode-<<- ()
;; heredoc
(cond
((electric-operator-just-inside-bracket) "<<-")
(t " <<-")))
(defun electric-operator-ruby-mode-<<~ ()
;; heredoc
(cond
((electric-operator-just-inside-bracket) "<<~")
(t " <<~")))

(apply #'electric-operator-add-rules-for-mode 'ruby-mode (electric-operator-get-rules-for-mode 'prog-mode))
(electric-operator-add-rules-for-mode 'ruby-mode
(cons "=~" " =~ ") ; regex equality
)
(cons "|" #'electric-operator-ruby-mode-|)
(cons "*" #'electric-operator-ruby-mode-*)
(cons "**" #'electric-operator-ruby-mode-**)
(cons "%" #'electric-operator-ruby-mode-%)
(cons "<<-" #'electric-operator-ruby-mode-<<-)
(cons "<<~" #'electric-operator-ruby-mode-<<~)
(cons "&" nil) ;complicated by &. &: &block
(cons "?" nil) ;can be boolean_method?() or ternary
(cons "/" nil) ;complicated by regexes
(cons "=>" " => ") ;in hashes
(cons "->(" " ->(") ;stabby lambda with arguments
(cons "->" " -> ") ;stabby lambda (without arguments)
(cons ";" "; ") ;statement separator
(cons "<<" " << ")
(cons ">>" " >> ")
(cons "===" " === ") ;case equality operator
(cons "=~" " =~ ") ;regex equality
(cons "!~" " !~ ") ;regex inequality
(cons "<=>" " <=> ") ;spaceship operator (comparison)
(cons "**=" " **= ")
(cons "%=" " %= ")
(cons "<<=" " <<= ")
(cons ">>=" " >>= ")
(cons "&&=" " &&= ")
(cons "&=" " &= ")
(cons "||=" " ||= ")
(cons "|=" " |= ")
(cons "^=" " ^= ")
)



;;; Other major mode tweaks


;; This is based on a syntax guide and hasn't been tested.
(apply #'electric-operator-add-rules-for-mode 'java-mode (electric-operator-get-rules-for-mode 'prog-mode))
Expand Down
7 changes: 7 additions & 0 deletions test/electric-operator-test.el
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
(require 'electric-operator)

(defconst electric-operator--test-dir
(file-name-directory (or load-file-name buffer-file-name)))

(ert-deftest trie-create ()
(let ((trie (make-electric-operator--trie)))
(should (equal (electric-operator--trie-value trie) nil))))
Expand Down Expand Up @@ -63,6 +66,10 @@ Aenean in sem ac leo mollis blandit. xyz /=" trie) " /= "))
(nreverse '(" * " ("1" "2" "3") "6" "xyz" "bar"))))))


(ert-deftest electric-operator-ruby ()
(ert-test-erts-file (expand-file-name "ruby.erts" electric-operator--test-dir)))


;; Local Variables:
;; nameless-current-name: "electric-operator"
;; End:
126 changes: 126 additions & 0 deletions test/ruby.erts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
Name: |-block-param-start
Code: (lambda ()
(ruby-mode)
(electric-operator-mode)
(abbrev-mode -1)
(self-insert-command 1 ?|)
(self-insert-command 1 ?q))
Point-Char: ^
=-=
arr.map {^
=-=
arr.map { |q
=-=-=

Name: |-block-param-end
=-=
arr.map { |element^
=-=
arr.map { |element| q
=-=-=

Name: |-do-block-param-start
=-=
arr.map do^
=-=
arr.map do |q
=-=-=

Name: |-do-block-param-end
=-=
arr.map do |element^
=-=
arr.map do |element| q
=-=-=

Name: |-operator
=-=
something^
=-=
something | q
=-=-=

Name: *-splat
Code: (lambda ()
(ruby-mode)
(electric-operator-mode)
(abbrev-mode -1)
(self-insert-command 1 ?*)
(self-insert-command 1 ?q))
Point-Char: ^
=-=
def something(^
=-=
def something(*q
=-=-=

Name: *-splat2
=-=
a = ^
=-=
a = *q
=-=-=

Name: *-operator
=-=
something^
=-=
something * q
=-=-=

Name: **-splat
Code: (lambda ()
(ruby-mode)
(electric-operator-mode)
(abbrev-mode -1)
(goto-char (1- (point-max)))
(self-insert-command 1 ?*)
(self-insert-command 1 ?*)
(self-insert-command 1 ?q))
Point-Char: ^
=-=
def something(^
=-=
def something(**q
=-=-=

Name: **-operator
=-=
something^
=-=
something ** q
=-=-=

Name: ->-stabby-lambda-without-args
Code: (lambda ()
(ruby-mode)
(electric-operator-mode)
(electric-pair-mode)
(abbrev-mode -1)
(goto-char (1- (point-max)))
(self-insert-command 1 ?\-)
(self-insert-command 1 ?\>)
(self-insert-command 1 ?\{))
Point-Char: ^
=-=
my_lambda = ^
=-=
my_lambda = -> {^}
=-=-=

Name: ->-stabby-lambda-with-args
Code: (lambda ()
(ruby-mode)
(electric-operator-mode)
(electric-pair-mode)
(abbrev-mode -1)
(goto-char (1- (point-max)))
(self-insert-command 1 ?\-)
(self-insert-command 1 ?\>)
(self-insert-command 1 ?\())
Point-Char: ^
=-=
my_lambda = ^
=-=
my_lambda = ->(^)
=-=-=

0 comments on commit 733d56e

Please sign in to comment.