+2012-09-08 Dmitry Gutov <dgutov@yandex.ru>
+
+ * 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 <kjambunathan@gmail.com>
* register.el (register): New group.
(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 "\\<do\\(\\s \\|$\\)"))
- (let ((orig (point)) (end (progn (ruby-forward-sexp) (point))))
- (backward-char 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))))
+(defun ruby-brace-to-do-end (orig end)
+ (let (beg-marker end-marker)
+ (goto-char end)
+ (when (eq (char-before) ?\})
+ (delete-char -1)
+ (skip-chars-backward " \t")
+ (when (not (bolp))
+ (insert "\n"))
+ (insert "end")
+ (setq end-marker (point-marker))
+ (when (and (not (eobp)) (eq (char-syntax (char-after)) ?w))
+ (insert " "))
+ (goto-char orig)
+ (delete-char 1)
+ (when (eq (char-syntax (char-before)) ?w)
+ (insert " "))
+ (insert "do")
+ (setq beg-marker (point-marker))
+ (when (looking-at "\\(\\s \\)*|")
+ (unless (match-beginning 1)
+ (insert " "))
+ (goto-char (1+ (match-end 0)))
+ (search-forward "|"))
+ (unless (looking-at "\\s *$")
+ (insert "\n"))
+ (indent-region beg-marker end-marker)
+ (goto-char beg-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))
(defun ruby-toggle-block ()
+ "Toggle block type from do-end to braces or back.
+The block must begin on the current line or above it and end after the point.
+If the result is do-end block, it will always be multiline."
(interactive)
- (or (ruby-brace-to-do-end)
- (ruby-do-end-to-brace)))
+ (let ((start (point)) beg end)
+ (end-of-line)
+ (unless
+ (if (and (re-search-backward "\\({\\)\\|\\_<do\\(\\s \\|$\\||\\)")
+ (progn
+ (setq beg (point))
+ (save-match-data (ruby-forward-sexp))
+ (setq end (point))
+ (> 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))
(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.
(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))