emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master f0e8f4a4ca: Support indentation of Ruby pattern matching expressi


From: Dmitry Gutov
Subject: master f0e8f4a4ca: Support indentation of Ruby pattern matching expressions
Date: Sun, 20 Mar 2022 21:22:34 -0400 (EDT)

branch: master
commit f0e8f4a4ca34c6510d87266951948f527b289631
Author: Dmitry Gutov <dgutov@yandex.ru>
Commit: Dmitry Gutov <dgutov@yandex.ru>

    Support indentation of Ruby pattern matching expressions
    
    * lisp/progmodes/ruby-mode.el (ruby-smie-grammar, ruby-smie-rules)
    (ruby-block-mid-keywords): Treat 'in' token similarly to 'when'.
    
    * test/lisp/progmodes/ruby-mode-resources/ruby.rb:
    Add indentation example.
---
 lisp/progmodes/ruby-mode.el                     | 12 +++++++-----
 test/lisp/progmodes/ruby-mode-resources/ruby.rb |  8 ++++++++
 2 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/lisp/progmodes/ruby-mode.el b/lisp/progmodes/ruby-mode.el
index eb54ffe05a..fdc8164dc0 100644
--- a/lisp/progmodes/ruby-mode.el
+++ b/lisp/progmodes/ruby-mode.el
@@ -70,7 +70,7 @@
   "Regexp to match modifiers.")
 
 (defconst ruby-block-mid-keywords
-  '("then" "else" "elsif" "when" "rescue" "ensure")
+  '("then" "else" "elsif" "when" "in" "rescue" "ensure")
   "Keywords where the indentation gets shallower in middle of block 
statements.")
 
 (defconst ruby-block-mid-re
@@ -369,7 +369,9 @@ This only affects the output of the command 
`ruby-toggle-block'."
        (for-body (for-head ";" insts))
        (for-head (id "in" exp))
        (cases (exp "then" insts)
-              (cases "when" cases) (insts "else" insts))
+              (cases "when" cases)
+              (cases "in" cases)
+              (insts "else" insts))
        (expseq (exp) );;(expseq "," expseq)
        (hashvals (exp1 "=>" exp1) (hashvals "," hashvals))
        (insts-rescue-insts (insts)
@@ -380,7 +382,7 @@ This only affects the output of the command 
`ruby-toggle-block'."
        (if-body (ielsei) (if-body "elsif" if-body)))
      '((nonassoc "in") (assoc ";") (right " @ ")
        (assoc ",") (right "="))
-     '((assoc "when"))
+     '((assoc "when" "in"))
      '((assoc "elsif"))
      '((assoc "rescue" "ensure"))
      '((assoc ",")))
@@ -595,7 +597,7 @@ This only affects the output of the command 
`ruby-toggle-block'."
      (cond
       ((smie-rule-parent-p "def" "begin" "do" "class" "module" "for"
                            "while" "until" "unless"
-                           "if" "then" "elsif" "else" "when"
+                           "if" "then" "elsif" "else" "when" "in"
                            "rescue" "ensure" "{")
        (smie-rule-parent ruby-indent-level))
       ;; For (invalid) code between switch and case.
@@ -659,7 +661,7 @@ This only affects the output of the command 
`ruby-toggle-block'."
                         ruby-indent-level))))
     (`(:before . ,(or "else" "then" "elsif" "rescue" "ensure"))
      (smie-rule-parent))
-    ('(:before . "when")
+    (`(:before . ,(or "when" "in"))
      ;; Align to the previous `when', but look up the virtual
      ;; indentation of `case'.
      (if (smie-rule-sibling-p) 0 (smie-rule-parent)))
diff --git a/test/lisp/progmodes/ruby-mode-resources/ruby.rb 
b/test/lisp/progmodes/ruby-mode-resources/ruby.rb
index 8c698e4fac..f31cea86a5 100644
--- a/test/lisp/progmodes/ruby-mode-resources/ruby.rb
+++ b/test/lisp/progmodes/ruby-mode-resources/ruby.rb
@@ -483,3 +483,11 @@ foo bar, {
 2 = 3
 :foo= if true
 {:abc=>4} # not indented, and '=' is not highlighted
+
+# Pattern matching
+case translation
+in ['th', orig_text, 'en', trans_text]
+  puts "English translation: #{orig_text} => #{trans_text}"
+in {'th' => orig_text, 'ja' => trans_text}
+  puts "Japanese translation: #{orig_text} => #{trans_text}"
+end



reply via email to

[Prev in Thread] Current Thread [Next in Thread]