From: Dmitry Gutov Date: Sat, 8 Sep 2012 23:32:25 +0000 (+0400) Subject: * lisp/progmodes/ruby-mode.el (ruby-toggle-block): Guess the current block, X-Git-Tag: emacs-24.2.90~354 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=c3268831411fd68ce4f6f84ecda5eda2814a59a8;p=emacs.git * lisp/progmodes/ruby-mode.el (ruby-toggle-block): Guess the current block, not just expect to be at its beginning. Adjust callees. Succeed when do-end block has no space before the pipe character. (ruby-brace-to-do-end): When the original block is one-liner, convert to multiline. Reindent the result. * test/automated/ruby-mode-tests.el: (ruby-toggle-block-to-multiline): New test. (ruby-should-indent-buffer, ruby-toggle-block-to-do-end) (ruby-toggle-block-to-brace): Use buffer-string. --- diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 7104f68e816..29fa06c10e5 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,11 @@ +2012-09-08 Dmitry Gutov + + * progmodes/ruby-mode.el (ruby-toggle-block): Guess the current block, + not just expect to be at its beginning. Adjust callees. + Succeed when do-end block has no space before the pipe character. + (ruby-brace-to-do-end): When the original block is one-liner, + convert to multiline. Reindent the result. + 2012-09-08 Jambunathan K * register.el (register): New group. diff --git a/lisp/progmodes/ruby-mode.el b/lisp/progmodes/ruby-mode.el index 947c12560a1..77ec8084ea2 100644 --- a/lisp/progmodes/ruby-mode.el +++ b/lisp/progmodes/ruby-mode.el @@ -1107,46 +1107,65 @@ See `add-log-current-defun-function'." (if mlist (concat mlist mname) mname) mlist))))) -(defun ruby-brace-to-do-end () - (when (looking-at "{") - (let ((orig (point)) (end (progn (ruby-forward-sexp) (point)))) - (when (eq (char-before) ?\}) - (delete-char -1) - (if (eq (char-syntax (char-before)) ?w) - (insert " ")) - (insert "end") - (if (eq (char-syntax (char-after)) ?w) - (insert " ")) - (goto-char orig) - (delete-char 1) - (if (eq (char-syntax (char-before)) ?w) - (insert " ")) - (insert "do") - (when (looking-at "\\sw\\||") - (insert " ") - (backward-char)) - t)))) - -(defun ruby-do-end-to-brace () - (when (and (or (bolp) - (not (memq (char-syntax (char-before)) '(?w ?_)))) - (looking-at "\\ end start))) + (if (match-beginning 1) + (ruby-brace-to-do-end beg end) + (ruby-do-end-to-brace beg end))) + (goto-char start)))) (declare-function ruby-syntax-propertize-heredoc "ruby-mode" (limit)) (declare-function ruby-syntax-enclosing-percent-literal "ruby-mode" (limit)) diff --git a/test/ChangeLog b/test/ChangeLog index 541937ec4e7..a7e22aa9ae1 100644 --- a/test/ChangeLog +++ b/test/ChangeLog @@ -1,3 +1,10 @@ +2012-09-08 Dmitry Gutov + + * automated/ruby-mode-tests.el: + (ruby-toggle-block-to-multiline): New test. + (ruby-should-indent-buffer, ruby-toggle-block-to-do-end) + (ruby-toggle-block-to-brace): Use buffer-string. + 2012-09-07 Dmitry Gutov * automated/ruby-mode-tests.el: New tests (Bug#11613). diff --git a/test/automated/ruby-mode-tests.el b/test/automated/ruby-mode-tests.el index 515353bc2d0..e711b52fb9c 100644 --- a/test/automated/ruby-mode-tests.el +++ b/test/automated/ruby-mode-tests.el @@ -40,8 +40,7 @@ The whitespace before and including \"|\" on each line is removed." (insert (fix-indent content)) (ruby-mode) (indent-region (point-min) (point-max)) - (should (string= (fix-indent expected) (buffer-substring-no-properties - (point-min) (point-max))))))) + (should (string= (fix-indent expected) (buffer-string)))))) (defun ruby-assert-state (content &rest values-plist) "Assert syntax state values at the end of CONTENT. @@ -213,21 +212,27 @@ VALUES-PLIST is a list with alternating index and value elements." (ert-deftest ruby-toggle-block-to-do-end () (with-temp-buffer - (insert "foo {|b|\n}\n") + (insert "foo {|b|\n}") (ruby-mode) - (search-backward "{") + (beginning-of-line) (ruby-toggle-block) - (should (string= "foo do |b|\nend\n" (buffer-substring-no-properties - (point-min) (point-max)))))) + (should (string= "foo do |b|\nend" (buffer-string))))) (ert-deftest ruby-toggle-block-to-brace () (with-temp-buffer - (insert "foo do |b|\nend\n") + (insert "foo do |b|\nend") (ruby-mode) - (search-backward "do") + (beginning-of-line) + (ruby-toggle-block) + (should (string= "foo {|b|\n}" (buffer-string))))) + +(ert-deftest ruby-toggle-block-to-multiline () + (with-temp-buffer + (insert "foo {|b| b + 1}") + (ruby-mode) + (beginning-of-line) (ruby-toggle-block) - (should (string= "foo {|b|\n}\n" (buffer-substring-no-properties - (point-min) (point-max)))))) + (should (string= "foo do |b|\n b + 1\nend" (buffer-string))))) (ert-deftest ruby-recognize-symbols-starting-with-at-character () (ruby-assert-face ":@abc" 3 'font-lock-constant-face))