From f0e8f4a4ca34c6510d87266951948f527b289631 Mon Sep 17 00:00:00 2001 From: Dmitry Gutov Date: Mon, 21 Mar 2022 03:18:36 +0200 Subject: [PATCH] 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 eb54ffe05a8..fdc8164dc0b 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 8c698e4fac8..f31cea86a54 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 -- 2.39.2