diff options
| author | Dmitry Gutov | 2013-12-20 07:20:33 +0200 |
|---|---|---|
| committer | Dmitry Gutov | 2013-12-20 07:20:33 +0200 |
| commit | b520f210b12911b747cd7cd725c310bef4790a48 (patch) | |
| tree | 77e79dd61f417764f4e32a0baa2c8a35548b35bc | |
| parent | 26b75b456e536c1fb2d3979e07e82455b2d9c1ca (diff) | |
| download | emacs-b520f210b12911b747cd7cd725c310bef4790a48.tar.gz emacs-b520f210b12911b747cd7cd725c310bef4790a48.zip | |
* 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.
Fixes: debbugs:16182
| -rw-r--r-- | etc/NEWS | 2 | ||||
| -rw-r--r-- | lisp/ChangeLog | 11 | ||||
| -rw-r--r-- | lisp/progmodes/ruby-mode.el | 63 | ||||
| -rw-r--r-- | test/ChangeLog | 8 | ||||
| -rw-r--r-- | test/automated/ruby-mode-tests.el | 51 | ||||
| -rw-r--r-- | test/indent/ruby.rb | 37 |
6 files changed, 144 insertions, 28 deletions
| @@ -714,6 +714,8 @@ whether it is safe to use Bash's --noediting option. These days | |||
| 714 | 714 | ||
| 715 | *** Add more Ruby file types to `auto-mode-alist'. | 715 | *** Add more Ruby file types to `auto-mode-alist'. |
| 716 | 716 | ||
| 717 | *** New option `ruby-align-to-stmt-keywords'. | ||
| 718 | |||
| 717 | ** JS Mode | 719 | ** JS Mode |
| 718 | 720 | ||
| 719 | *** Better indentation of multiple-variable declarations. | 721 | *** Better indentation of multiple-variable declarations. |
diff --git a/lisp/ChangeLog b/lisp/ChangeLog index b502aa946ad..bf446bc7b66 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog | |||
| @@ -1,3 +1,14 @@ | |||
| 1 | 2013-12-20 Dmitry Gutov <dgutov@yandex.ru> | ||
| 2 | |||
| 3 | * progmodes/ruby-mode.el (ruby-align-to-stmt-keywords): New | ||
| 4 | option. (Bug#16182) | ||
| 5 | (ruby-smie--indent-to-stmt-p): Use it. | ||
| 6 | (ruby-smie-rules): Revert the logic in the handling of `when'. | ||
| 7 | Expand the begin clause to handle `ruby-align-to-stmt-keywords'. | ||
| 8 | (ruby-deep-arglist, ruby-deep-indent-paren) | ||
| 9 | (ruby-deep-indent-paren-style): Update docstrings to note that the | ||
| 10 | vars don't have any effect with SMIE. | ||
| 11 | |||
| 1 | 2013-12-20 Jay Belanger <jay.p.belanger@gmail.com> | 12 | 2013-12-20 Jay Belanger <jay.p.belanger@gmail.com> |
| 2 | 13 | ||
| 3 | * calc/calc.el (calc-enter, calc-pop): Use the variable | 14 | * calc/calc.el (calc-enter, calc-pop): Use the variable |
diff --git a/lisp/progmodes/ruby-mode.el b/lisp/progmodes/ruby-mode.el index 449a10dc999..12fb8d2bf72 100644 --- a/lisp/progmodes/ruby-mode.el +++ b/lisp/progmodes/ruby-mode.el | |||
| @@ -226,9 +226,48 @@ This should only be called after matching against `ruby-here-doc-beg-re'." | |||
| 226 | :group 'ruby | 226 | :group 'ruby |
| 227 | :safe 'integerp) | 227 | :safe 'integerp) |
| 228 | 228 | ||
| 229 | (defcustom ruby-align-to-stmt-keywords nil | ||
| 230 | "Keywords to align their expression body to statement. | ||
| 231 | When nil, an expression that begins with one these keywords is | ||
| 232 | indented to the column of the keyword. Example: | ||
| 233 | |||
| 234 | tee = if foo | ||
| 235 | bar | ||
| 236 | else | ||
| 237 | qux | ||
| 238 | end | ||
| 239 | |||
| 240 | If this value is t or contains a symbol with the name of given | ||
| 241 | keyword, the expression is indented to align to the beginning of | ||
| 242 | the statement: | ||
| 243 | |||
| 244 | tee = if foo | ||
| 245 | bar | ||
| 246 | else | ||
| 247 | qux | ||
| 248 | end | ||
| 249 | |||
| 250 | Only has effect when `ruby-use-smie' is t. | ||
| 251 | " | ||
| 252 | :type '(choice | ||
| 253 | (const :tag "None" nil) | ||
| 254 | (const :tag "All" t) | ||
| 255 | (repeat :tag "User defined" | ||
| 256 | (choice (const if) | ||
| 257 | (const while) | ||
| 258 | (const unless) | ||
| 259 | (const until) | ||
| 260 | (const begin) | ||
| 261 | (const case) | ||
| 262 | (const for)))) | ||
| 263 | :group 'ruby | ||
| 264 | :safe 'listp | ||
| 265 | :version "24.4") | ||
| 266 | |||
| 229 | (defcustom ruby-deep-arglist t | 267 | (defcustom ruby-deep-arglist t |
| 230 | "Deep indent lists in parenthesis when non-nil. | 268 | "Deep indent lists in parenthesis when non-nil. |
| 231 | Also ignores spaces after parenthesis when 'space." | 269 | Also ignores spaces after parenthesis when `space'. |
| 270 | Only has effect when `ruby-use-smie' is nil." | ||
| 232 | :type 'boolean | 271 | :type 'boolean |
| 233 | :group 'ruby | 272 | :group 'ruby |
| 234 | :safe 'booleanp) | 273 | :safe 'booleanp) |
| @@ -236,11 +275,13 @@ Also ignores spaces after parenthesis when 'space." | |||
| 236 | (defcustom ruby-deep-indent-paren '(?\( ?\[ ?\] t) | 275 | (defcustom ruby-deep-indent-paren '(?\( ?\[ ?\] t) |
| 237 | "Deep indent lists in parenthesis when non-nil. | 276 | "Deep indent lists in parenthesis when non-nil. |
| 238 | The value t means continuous line. | 277 | The value t means continuous line. |
| 239 | Also ignores spaces after parenthesis when 'space." | 278 | Also ignores spaces after parenthesis when `space'. |
| 279 | Only has effect when `ruby-use-smie' is nil." | ||
| 240 | :group 'ruby) | 280 | :group 'ruby) |
| 241 | 281 | ||
| 242 | (defcustom ruby-deep-indent-paren-style 'space | 282 | (defcustom ruby-deep-indent-paren-style 'space |
| 243 | "Default deep indent style." | 283 | "Default deep indent style. |
| 284 | Only has effect when `ruby-use-smie' is nil." | ||
| 244 | :options '(t nil space) :group 'ruby) | 285 | :options '(t nil space) :group 'ruby) |
| 245 | 286 | ||
| 246 | (defcustom ruby-encoding-map | 287 | (defcustom ruby-encoding-map |
| @@ -520,6 +561,10 @@ It is used when `ruby-encoding-magic-comment-style' is set to `custom'." | |||
| 520 | (smie-backward-sexp ";") | 561 | (smie-backward-sexp ";") |
| 521 | (cons 'column (smie-indent-virtual)))) | 562 | (cons 'column (smie-indent-virtual)))) |
| 522 | 563 | ||
| 564 | (defun ruby-smie--indent-to-stmt-p (keyword) | ||
| 565 | (or (eq t ruby-align-to-stmt-keywords) | ||
| 566 | (memq (intern keyword) ruby-align-to-stmt-keywords))) | ||
| 567 | |||
| 523 | (defun ruby-smie-rules (kind token) | 568 | (defun ruby-smie-rules (kind token) |
| 524 | (pcase (cons kind token) | 569 | (pcase (cons kind token) |
| 525 | (`(:elem . basic) ruby-indent-level) | 570 | (`(:elem . basic) ruby-indent-level) |
| @@ -572,7 +617,9 @@ It is used when `ruby-encoding-magic-comment-style' is set to `custom'." | |||
| 572 | (`(:before . ,(or `"else" `"then" `"elsif" `"rescue" `"ensure")) | 617 | (`(:before . ,(or `"else" `"then" `"elsif" `"rescue" `"ensure")) |
| 573 | (smie-rule-parent)) | 618 | (smie-rule-parent)) |
| 574 | (`(:before . "when") | 619 | (`(:before . "when") |
| 575 | (if (not (smie-rule-sibling-p)) 0)) ;; ruby-indent-level | 620 | ;; Align to the previous `when', but look up the virtual |
| 621 | ;; indentation of `case'. | ||
| 622 | (if (smie-rule-sibling-p) 0 (smie-rule-parent))) | ||
| 576 | (`(:after . ,(or "=" "iuwu-mod" "+" "-" "*" "/" "&&" "||" "%" "**" "^" "&" | 623 | (`(:after . ,(or "=" "iuwu-mod" "+" "-" "*" "/" "&&" "||" "%" "**" "^" "&" |
| 577 | "<=>" ">" "<" ">=" "<=" "==" "===" "!=" "<<" ">>" | 624 | "<=>" ">" "<" ">=" "<=" "==" "===" "!=" "<<" ">>" |
| 578 | "+=" "-=" "*=" "/=" "%=" "**=" "&=" "|=" "^=" "|" | 625 | "+=" "-=" "*=" "/=" "%=" "**=" "&=" "|=" "^=" "|" |
| @@ -581,9 +628,11 @@ It is used when `ruby-encoding-magic-comment-style' is set to `custom'." | |||
| 581 | (smie-indent--hanging-p) | 628 | (smie-indent--hanging-p) |
| 582 | ruby-indent-level)) | 629 | ruby-indent-level)) |
| 583 | (`(:after . ,(or "?" ":")) ruby-indent-level) | 630 | (`(:after . ,(or "?" ":")) ruby-indent-level) |
| 584 | (`(:before . "begin") | 631 | (`(:before . ,(or "if" "while" "unless" "until" "begin" "case" "for")) |
| 585 | (unless (save-excursion (skip-chars-backward " \t") (bolp)) | 632 | (when (not (save-excursion (skip-chars-backward " \t") (bolp))) |
| 586 | (smie-rule-parent))) | 633 | (if (ruby-smie--indent-to-stmt-p token) |
| 634 | (ruby-smie--indent-to-stmt) | ||
| 635 | (cons 'column (current-column))))) | ||
| 587 | )) | 636 | )) |
| 588 | 637 | ||
| 589 | (defun ruby-imenu-create-index-in-block (prefix beg end) | 638 | (defun ruby-imenu-create-index-in-block (prefix beg end) |
diff --git a/test/ChangeLog b/test/ChangeLog index 374cae5a295..cefdd2de9fd 100644 --- a/test/ChangeLog +++ b/test/ChangeLog | |||
| @@ -1,3 +1,11 @@ | |||
| 1 | 2013-12-20 Dmitry Gutov <dgutov@yandex.ru> | ||
| 2 | |||
| 3 | * automated/ruby-mode-tests.el: Add tests for | ||
| 4 | `ruby-align-to-stmt-keywords'. | ||
| 5 | |||
| 6 | * indent/ruby.rb: Update examples to reflect the lack of change in | ||
| 7 | default indentation of `begin' blocks. | ||
| 8 | |||
| 1 | 2013-12-17 Dmitry Gutov <dgutov@yandex.ru> | 9 | 2013-12-17 Dmitry Gutov <dgutov@yandex.ru> |
| 2 | 10 | ||
| 3 | * indent/ruby.rb: Update examples according to the change | 11 | * indent/ruby.rb: Update examples according to the change |
diff --git a/test/automated/ruby-mode-tests.el b/test/automated/ruby-mode-tests.el index 9c121169756..de0c1bb9629 100644 --- a/test/automated/ruby-mode-tests.el +++ b/test/automated/ruby-mode-tests.el | |||
| @@ -282,6 +282,57 @@ VALUES-PLIST is a list with alternating index and value elements." | |||
| 282 | | 3) | 282 | | 3) |
| 283 | |"))) | 283 | |"))) |
| 284 | 284 | ||
| 285 | (ert-deftest ruby-align-to-stmt-keywords-t () | ||
| 286 | (let ((ruby-align-to-stmt-keywords t)) | ||
| 287 | (ruby-should-indent-buffer | ||
| 288 | "foo = if bar? | ||
| 289 | | 1 | ||
| 290 | |else | ||
| 291 | | 2 | ||
| 292 | |end | ||
| 293 | | | ||
| 294 | |foo || begin | ||
| 295 | | bar | ||
| 296 | |end | ||
| 297 | | | ||
| 298 | |foo || | ||
| 299 | | begin | ||
| 300 | | bar | ||
| 301 | | end | ||
| 302 | |" | ||
| 303 | "foo = if bar? | ||
| 304 | | 1 | ||
| 305 | |else | ||
| 306 | | 2 | ||
| 307 | | end | ||
| 308 | | | ||
| 309 | | foo || begin | ||
| 310 | | bar | ||
| 311 | |end | ||
| 312 | | | ||
| 313 | | foo || | ||
| 314 | | begin | ||
| 315 | |bar | ||
| 316 | | end | ||
| 317 | |") | ||
| 318 | )) | ||
| 319 | |||
| 320 | (ert-deftest ruby-align-to-stmt-keywords-case () | ||
| 321 | (let ((ruby-align-to-stmt-keywords '(case))) | ||
| 322 | (ruby-should-indent-buffer | ||
| 323 | "b = case a | ||
| 324 | |when 13 | ||
| 325 | | 6 | ||
| 326 | |else | ||
| 327 | | 42 | ||
| 328 | |end" | ||
| 329 | "b = case a | ||
| 330 | | when 13 | ||
| 331 | | 6 | ||
| 332 | | else | ||
| 333 | | 42 | ||
| 334 | | end"))) | ||
| 335 | |||
| 285 | (ert-deftest ruby-move-to-block-stops-at-indentation () | 336 | (ert-deftest ruby-move-to-block-stops-at-indentation () |
| 286 | (ruby-with-temp-buffer "def f\nend" | 337 | (ruby-with-temp-buffer "def f\nend" |
| 287 | (beginning-of-line) | 338 | (beginning-of-line) |
diff --git a/test/indent/ruby.rb b/test/indent/ruby.rb index 1993c29efb3..7a1a225834d 100644 --- a/test/indent/ruby.rb +++ b/test/indent/ruby.rb | |||
| @@ -114,17 +114,17 @@ def test2 (arg) | |||
| 114 | puts "there" | 114 | puts "there" |
| 115 | end | 115 | end |
| 116 | 116 | ||
| 117 | case a | 117 | b = case a |
| 118 | when "a" | 118 | when "a" |
| 119 | 6 | 119 | 6 |
| 120 | # Support for this syntax was removed in Ruby 1.9, so we | 120 | # Support for this syntax was removed in Ruby 1.9, so we |
| 121 | # probably don't need to handle it either. | 121 | # probably don't need to handle it either. |
| 122 | # when "b" : | 122 | # when "b" : |
| 123 | # 7 | 123 | # 7 |
| 124 | # when "c" : 2 | 124 | # when "c" : 2 |
| 125 | when "d" then 4 | 125 | when "d" then 4 |
| 126 | else 5 | 126 | else 5 |
| 127 | end | 127 | end |
| 128 | end | 128 | end |
| 129 | 129 | ||
| 130 | # Some Cucumber code: | 130 | # Some Cucumber code: |
| @@ -321,18 +321,13 @@ end | |||
| 321 | foo | | 321 | foo | |
| 322 | bar | 322 | bar |
| 323 | 323 | ||
| 324 | foo || | ||
| 325 | begin | ||
| 326 | bar | ||
| 327 | end | ||
| 328 | |||
| 329 | def qux | 324 | def qux |
| 330 | foo ||= begin | 325 | foo ||= begin |
| 331 | bar | 326 | bar |
| 332 | tee | 327 | tee |
| 333 | rescue | 328 | rescue |
| 334 | oomph | 329 | oomph |
| 335 | end | 330 | end |
| 336 | end | 331 | end |
| 337 | 332 | ||
| 338 | %^abc^ | 333 | %^abc^ |