+2012-09-18 Dmitry Gutov <dgutov@yandex.ru>
+
+ * 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 <rudalics@gmx.at>
* emacs-lisp/debug.el (debugger-bury-or-kill): Fix customization
(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))
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.
(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