emacs-diffs
[Top][All Lists]
Advanced

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

[Emacs-diffs] trunk r115624: * lisp/progmodes/ruby-mode.el (ruby-align-t


From: Dmitry Gutov
Subject: [Emacs-diffs] trunk r115624: * lisp/progmodes/ruby-mode.el (ruby-align-to-stmt-keywords): New
Date: Fri, 20 Dec 2013 05:20:40 +0000
User-agent: Bazaar (2.6b2)

------------------------------------------------------------
revno: 115624
revision-id: address@hidden
parent: address@hidden
fixes bug: http://debbugs.gnu.org/16182
committer: Dmitry Gutov <address@hidden>
branch nick: trunk
timestamp: Fri 2013-12-20 07:20:33 +0200
message:
  * lisp/progmodes/ruby-mode.el (ruby-align-to-stmt-keywords): New
  option. 
  (ruby-smie--indent-to-stmt-p): Use it.
  (ruby-smie-rules): Revert the logic in the handling of `when'.
  Expand the `begin' clause to handle `ruby-align-to-stmt-keywords'.
  (ruby-deep-arglist, ruby-deep-indent-paren)
  (ruby-deep-indent-paren-style): Update docstrings to note that the
  vars don't have any effect with SMIE.
  
  * test/automated/ruby-mode-tests.el: Add tests for
  `ruby-align-to-stmt-keywords'.
  
  * test/indent/ruby.rb: Update examples to reflect the lack of change in
  default indentation of `begin' blocks.
modified:
  etc/NEWS                       news-20100311060928-aoit31wvzf25yr1z-1
  lisp/ChangeLog                 changelog-20091113204419-o5vbwnq5f7feedwu-1432
  lisp/progmodes/ruby-mode.el    
rubymode.el-20091113204419-o5vbwnq5f7feedwu-8804
  test/ChangeLog                 changelog-20091113204419-o5vbwnq5f7feedwu-8588
  test/automated/ruby-mode-tests.el 
rubymodetests.el-20120720101201-mn39s4kfopdxj3ek-1
  test/indent/ruby.rb            ruby.rb-20120424165921-h044139hbrd7snvw-1
=== modified file 'etc/NEWS'
--- a/etc/NEWS  2013-12-19 21:02:46 +0000
+++ b/etc/NEWS  2013-12-20 05:20:33 +0000
@@ -714,6 +714,8 @@
 
 *** Add more Ruby file types to `auto-mode-alist'.
 
+*** New option `ruby-align-to-stmt-keywords'.
+
 ** JS Mode
 
 *** Better indentation of multiple-variable declarations.

=== modified file 'lisp/ChangeLog'
--- a/lisp/ChangeLog    2013-12-20 04:53:24 +0000
+++ b/lisp/ChangeLog    2013-12-20 05:20:33 +0000
@@ -1,3 +1,14 @@
+2013-12-20  Dmitry Gutov  <address@hidden>
+
+       * progmodes/ruby-mode.el (ruby-align-to-stmt-keywords): New
+       option.  (Bug#16182)
+       (ruby-smie--indent-to-stmt-p): Use it.
+       (ruby-smie-rules): Revert the logic in the handling of `when'.
+       Expand the begin clause to handle `ruby-align-to-stmt-keywords'.
+       (ruby-deep-arglist, ruby-deep-indent-paren)
+       (ruby-deep-indent-paren-style): Update docstrings to note that the
+       vars don't have any effect with SMIE.
+
 2013-12-20  Jay Belanger  <address@hidden>
 
        * calc/calc.el (calc-enter, calc-pop): Use the variable

=== modified file 'lisp/progmodes/ruby-mode.el'
--- a/lisp/progmodes/ruby-mode.el       2013-12-19 05:06:24 +0000
+++ b/lisp/progmodes/ruby-mode.el       2013-12-20 05:20:33 +0000
@@ -226,9 +226,48 @@
   :group 'ruby
   :safe 'integerp)
 
+(defcustom ruby-align-to-stmt-keywords nil
+  "Keywords to align their expression body to statement.
+When nil, an expression that begins with one these keywords is
+indented to the column of the keyword.  Example:
+
+  tee = if foo
+          bar
+        else
+          qux
+        end
+
+If this value is t or contains a symbol with the name of given
+keyword, the expression is indented to align to the beginning of
+the statement:
+
+  tee = if foo
+    bar
+  else
+    qux
+  end
+
+Only has effect when `ruby-use-smie' is t.
+"
+  :type '(choice
+          (const :tag "None" nil)
+          (const :tag "All" t)
+          (repeat :tag "User defined"
+                  (choice (const if)
+                          (const while)
+                          (const unless)
+                          (const until)
+                          (const begin)
+                          (const case)
+                          (const for))))
+  :group 'ruby
+  :safe 'listp
+  :version "24.4")
+
 (defcustom ruby-deep-arglist t
   "Deep indent lists in parenthesis when non-nil.
-Also ignores spaces after parenthesis when 'space."
+Also ignores spaces after parenthesis when `space'.
+Only has effect when `ruby-use-smie' is nil."
   :type 'boolean
   :group 'ruby
   :safe 'booleanp)
@@ -236,11 +275,13 @@
 (defcustom ruby-deep-indent-paren '(?\( ?\[ ?\] t)
   "Deep indent lists in parenthesis when non-nil.
 The value t means continuous line.
-Also ignores spaces after parenthesis when 'space."
+Also ignores spaces after parenthesis when `space'.
+Only has effect when `ruby-use-smie' is nil."
   :group 'ruby)
 
 (defcustom ruby-deep-indent-paren-style 'space
-  "Default deep indent style."
+  "Default deep indent style.
+Only has effect when `ruby-use-smie' is nil."
   :options '(t nil space) :group 'ruby)
 
 (defcustom ruby-encoding-map
@@ -520,6 +561,10 @@
     (smie-backward-sexp ";")
     (cons 'column (smie-indent-virtual))))
 
+(defun ruby-smie--indent-to-stmt-p (keyword)
+  (or (eq t ruby-align-to-stmt-keywords)
+      (memq (intern keyword) ruby-align-to-stmt-keywords)))
+
 (defun ruby-smie-rules (kind token)
   (pcase (cons kind token)
     (`(:elem . basic) ruby-indent-level)
@@ -572,7 +617,9 @@
     (`(:before . ,(or `"else" `"then" `"elsif" `"rescue" `"ensure"))
      (smie-rule-parent))
     (`(:before . "when")
-     (if (not (smie-rule-sibling-p)) 0)) ;; ruby-indent-level
+     ;; Align to the previous `when', but look up the virtual
+     ;; indentation of `case'.
+     (if (smie-rule-sibling-p) 0 (smie-rule-parent)))
     (`(:after . ,(or "=" "iuwu-mod" "+" "-" "*" "/" "&&" "||" "%" "**" "^" "&"
                      "<=>" ">" "<" ">=" "<=" "==" "===" "!=" "<<" ">>"
                      "+=" "-=" "*=" "/=" "%=" "**=" "&=" "|=" "^=" "|"
@@ -581,9 +628,11 @@
           (smie-indent--hanging-p)
           ruby-indent-level))
     (`(:after . ,(or "?" ":")) ruby-indent-level)
-    (`(:before . "begin")
-     (unless (save-excursion (skip-chars-backward " \t") (bolp))
-       (smie-rule-parent)))
+    (`(:before . ,(or "if" "while" "unless" "until" "begin" "case" "for"))
+     (when (not (save-excursion (skip-chars-backward " \t") (bolp)))
+       (if (ruby-smie--indent-to-stmt-p token)
+           (ruby-smie--indent-to-stmt)
+         (cons 'column (current-column)))))
     ))
 
 (defun ruby-imenu-create-index-in-block (prefix beg end)

=== modified file 'test/ChangeLog'
--- a/test/ChangeLog    2013-12-17 01:31:55 +0000
+++ b/test/ChangeLog    2013-12-20 05:20:33 +0000
@@ -1,3 +1,11 @@
+2013-12-20  Dmitry Gutov  <address@hidden>
+
+       * automated/ruby-mode-tests.el: Add tests for
+       `ruby-align-to-stmt-keywords'.
+
+       * indent/ruby.rb: Update examples to reflect the lack of change in
+       default indentation of `begin' blocks.
+
 2013-12-17  Dmitry Gutov  <address@hidden>
 
        * indent/ruby.rb: Update examples according to the change

=== modified file 'test/automated/ruby-mode-tests.el'
--- a/test/automated/ruby-mode-tests.el 2013-12-06 04:22:08 +0000
+++ b/test/automated/ruby-mode-tests.el 2013-12-20 05:20:33 +0000
@@ -282,6 +282,57 @@
      |  3)
      |")))
 
+(ert-deftest ruby-align-to-stmt-keywords-t ()
+  (let ((ruby-align-to-stmt-keywords t))
+    (ruby-should-indent-buffer
+     "foo = if bar?
+     |  1
+     |else
+     |  2
+     |end
+     |
+     |foo || begin
+     |  bar
+     |end
+     |
+     |foo ||
+     |  begin
+     |    bar
+     |  end
+     |"
+     "foo = if bar?
+     |       1
+     |else
+     |  2
+     | end
+     |
+     | foo || begin
+     |    bar
+     |end
+     |
+     |  foo ||
+     | begin
+     |bar
+     |  end
+     |")
+    ))
+
+(ert-deftest ruby-align-to-stmt-keywords-case ()
+  (let ((ruby-align-to-stmt-keywords '(case)))
+    (ruby-should-indent-buffer
+     "b = case a
+     |when 13
+     |  6
+     |else
+     |  42
+     |end"
+     "b = case a
+     |    when 13
+     |  6
+     |    else
+     |      42
+     |    end")))
+
 (ert-deftest ruby-move-to-block-stops-at-indentation ()
   (ruby-with-temp-buffer "def f\nend"
     (beginning-of-line)

=== modified file 'test/indent/ruby.rb'
--- a/test/indent/ruby.rb       2013-12-19 05:06:24 +0000
+++ b/test/indent/ruby.rb       2013-12-20 05:20:33 +0000
@@ -114,17 +114,17 @@
     puts "there"
   end
 
-  case a
-  when "a"
-    6
-  # Support for this syntax was removed in Ruby 1.9, so we
-  # probably don't need to handle it either.
-  # when "b" :
-  #   7
-  # when "c" : 2
-  when "d"  then 4
-  else 5
-  end
+  b = case a
+      when "a"
+        6
+      # Support for this syntax was removed in Ruby 1.9, so we
+      # probably don't need to handle it either.
+      # when "b" :
+      #   7
+      # when "c" : 2
+      when "d"  then 4
+      else 5
+      end
 end
 
 # Some Cucumber code:
@@ -321,18 +321,13 @@
 foo |
   bar
 
-foo ||
-  begin
-    bar
-  end
-
 def qux
   foo ||= begin
-    bar
-    tee
-  rescue
-    oomph
-  end
+            bar
+            tee
+          rescue
+            oomph
+          end
 end
 
 %^abc^


reply via email to

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