[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Emacs-diffs] /srv/bzr/emacs/trunk r109948: * lisp/progmodes/ruby-mode.e
From: |
Dmitry Gutov |
Subject: |
[Emacs-diffs] /srv/bzr/emacs/trunk r109948: * lisp/progmodes/ruby-mode.el (ruby-toggle-block): Guess the current block, |
Date: |
Sun, 09 Sep 2012 03:32:25 +0400 |
User-agent: |
Bazaar (2.5.0) |
------------------------------------------------------------
revno: 109948
committer: Dmitry Gutov <address@hidden>
branch nick: trunk
timestamp: Sun 2012-09-09 03:32:25 +0400
message:
* 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.
modified:
lisp/ChangeLog
lisp/progmodes/ruby-mode.el
test/ChangeLog
test/automated/ruby-mode-tests.el
=== modified file 'lisp/ChangeLog'
--- a/lisp/ChangeLog 2012-09-08 20:09:06 +0000
+++ b/lisp/ChangeLog 2012-09-08 23:32:25 +0000
@@ -1,3 +1,11 @@
+2012-09-08 Dmitry Gutov <address@hidden>
+
+ * 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 <address@hidden>
* register.el (register): New group.
=== modified file 'lisp/progmodes/ruby-mode.el'
--- a/lisp/progmodes/ruby-mode.el 2012-09-08 15:34:41 +0000
+++ b/lisp/progmodes/ruby-mode.el 2012-09-08 23:32:25 +0000
@@ -1107,46 +1107,65 @@
(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-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 ()
- (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-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))
=== modified file 'test/ChangeLog'
--- a/test/ChangeLog 2012-09-07 04:15:56 +0000
+++ b/test/ChangeLog 2012-09-08 23:32:25 +0000
@@ -1,3 +1,10 @@
+2012-09-08 Dmitry Gutov <address@hidden>
+
+ * 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 <address@hidden>
* automated/ruby-mode-tests.el: New tests (Bug#11613).
=== modified file 'test/automated/ruby-mode-tests.el'
--- a/test/automated/ruby-mode-tests.el 2012-09-08 15:13:14 +0000
+++ b/test/automated/ruby-mode-tests.el 2012-09-08 23:32:25 +0000
@@ -40,8 +40,7 @@
(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 @@
(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")
- (ruby-mode)
- (search-backward "do")
- (ruby-toggle-block)
- (should (string= "foo {|b|\n}\n" (buffer-substring-no-properties
- (point-min) (point-max))))))
+ (insert "foo do |b|\nend")
+ (ruby-mode)
+ (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 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))
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Emacs-diffs] /srv/bzr/emacs/trunk r109948: * lisp/progmodes/ruby-mode.el (ruby-toggle-block): Guess the current block,,
Dmitry Gutov <=