From 32fb816220e7a9f5450f276b94e3300c0b3baf4e Mon Sep 17 00:00:00 2001 From: Dmitry Gutov Date: Wed, 19 Sep 2012 02:10:19 +0400 Subject: [PATCH] * lisp/progmodes/ruby-mode.el (ruby-brace-to-do-end): Don't add extra whitespace after "end". (ruby-do-end-to-brace): Collapse block to one line if it fits within fill-column. --- lisp/ChangeLog | 7 +++++ lisp/progmodes/ruby-mode.el | 44 ++++++++++++++++++++++--------- test/automated/ruby-mode-tests.el | 16 ++++++----- 3 files changed, 49 insertions(+), 18 deletions(-) diff --git a/lisp/ChangeLog b/lisp/ChangeLog index e8986acdd1f..c17eff81100 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,10 @@ +2012-09-18 Dmitry Gutov + + * progmodes/ruby-mode.el (ruby-brace-to-do-end): Don't add extra + whitespace after "end". + (ruby-do-end-to-brace): Collapse block to one line if it fits + within fill-column. + 2012-09-18 Martin Rudalics * emacs-lisp/debug.el (debugger-bury-or-kill): Fix customization diff --git a/lisp/progmodes/ruby-mode.el b/lisp/progmodes/ruby-mode.el index 77ec8084ea2..0f02e81cbad 100644 --- a/lisp/progmodes/ruby-mode.el +++ b/lisp/progmodes/ruby-mode.el @@ -1112,8 +1112,9 @@ See `add-log-current-defun-function'." (goto-char end) (when (eq (char-before) ?\}) (delete-char -1) - (skip-chars-backward " \t") - (when (not (bolp)) + (when (save-excursion + (skip-chars-backward " \t") + (not (bolp))) (insert "\n")) (insert "end") (setq end-marker (point-marker)) @@ -1137,16 +1138,35 @@ See `add-log-current-defun-function'." t))) (defun ruby-do-end-to-brace (orig end) - (goto-char (- end 3)) - (when (looking-at ruby-block-end-re) - (delete-char 3) - (insert "}") - (goto-char orig) - (delete-char 2) - (insert "{") - (if (looking-at "\\s +|") - (delete-char (- (match-end 0) (match-beginning 0) 1))) - t)) + (let (beg-marker end-marker beg-pos end-pos) + (goto-char (- end 3)) + (when (looking-at ruby-block-end-re) + (delete-char 3) + (setq end-marker (point-marker)) + (insert "}") + (goto-char orig) + (delete-char 2) + (insert "{") + (setq beg-marker (point-marker)) + (when (looking-at "\\s +|") + (delete-char (- (match-end 0) (match-beginning 0) 1)) + (forward-char) + (re-search-forward "|" (line-end-position) t)) + (save-excursion + (skip-chars-forward " \t\n\r") + (setq beg-pos (point)) + (goto-char end-marker) + (skip-chars-backward " \t\n\r") + (setq end-pos (point))) + (when (or + (< end-pos beg-pos) + (and (= (line-number-at-pos beg-pos) (line-number-at-pos end-pos)) + (< (+ (current-column) (- end-pos beg-pos) 2) fill-column))) + (just-one-space -1) + (goto-char end-marker) + (just-one-space -1)) + (goto-char beg-marker) + t))) (defun ruby-toggle-block () "Toggle block type from do-end to braces or back. diff --git a/test/automated/ruby-mode-tests.el b/test/automated/ruby-mode-tests.el index e711b52fb9c..ba3040577b1 100644 --- a/test/automated/ruby-mode-tests.el +++ b/test/automated/ruby-mode-tests.el @@ -219,12 +219,16 @@ VALUES-PLIST is a list with alternating index and value elements." (should (string= "foo do |b|\nend" (buffer-string))))) (ert-deftest ruby-toggle-block-to-brace () - (with-temp-buffer - (insert "foo do |b|\nend") - (ruby-mode) - (beginning-of-line) - (ruby-toggle-block) - (should (string= "foo {|b|\n}" (buffer-string))))) + (let ((pairs '((16 . "foo {|b| b + 2 }") + (15 . "foo {|b|\n b + 2\n}")))) + (dolist (pair pairs) + (with-temp-buffer + (let ((fill-column (car pair))) + (insert "foo do |b|\n b + 2\nend") + (ruby-mode) + (beginning-of-line) + (ruby-toggle-block) + (should (string= (cdr pair) (buffer-string)))))))) (ert-deftest ruby-toggle-block-to-multiline () (with-temp-buffer -- 2.39.2