aboutsummaryrefslogtreecommitdiffstats
path: root/misc
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-12-06 07:47:47 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-12-06 07:47:47 +0000
commit378d20f80d74a142aa30c27e1f7412d48c650f84 (patch)
tree5bb9b48080da16d74858744c1ef3b3777995a42e /misc
parent761e9c518f5326799d7714ffd253d1ad1d5e33a9 (diff)
downloadruby-378d20f80d74a142aa30c27e1f7412d48c650f84.tar.gz
ruby-mode.el: expand/unexpand block
* misc/ruby-mode.el (ruby-brace-to-do-end): split single line block. * misc/ruby-mode.el (ruby-do-end-to-brace): shrink single line block to one line. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44033 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'misc')
-rw-r--r--misc/ruby-mode.el48
1 files changed, 44 insertions, 4 deletions
diff --git a/misc/ruby-mode.el b/misc/ruby-mode.el
index e244ae4827..553591f3c9 100644
--- a/misc/ruby-mode.el
+++ b/misc/ruby-mode.el
@@ -1198,11 +1198,17 @@ balanced expression is found."
(defun ruby-brace-to-do-end ()
(when (looking-at "{")
- (let ((orig (point)) (end (progn (ruby-forward-sexp) (point))))
+ (let ((orig (point)) (end (progn (ruby-forward-sexp) (point)))
+ oneline (end (make-marker)))
+ (setq oneline (and (eolp) (<= (point-at-bol) orig)))
(when (eq (char-before) ?\})
(delete-char -1)
- (if (eq (char-syntax (preceding-char)) ?w)
- (insert " "))
+ (cond
+ (oneline
+ (insert "\n")
+ (set-marker end (point)))
+ ((eq (char-syntax (preceding-char)) ?w)
+ (insert " ")))
(insert "end")
(if (eq (char-syntax (following-char)) ?w)
(insert " "))
@@ -1214,20 +1220,54 @@ balanced expression is found."
(when (looking-at "\\sw\\||")
(insert " ")
(backward-char))
+ (when oneline
+ (setq orig (point))
+ (when (cond
+ ((looking-at "\\s *|")
+ (goto-char (match-end 0))
+ (and (search-forward "|" (point-at-eol) 'move)
+ (not (eolp))))
+ (t))
+ (while (progn
+ (insert "\n")
+ (ruby-forward-sexp)
+ (looking-at "\\s *;\\s *"))
+ (delete-char (- (match-end 0) (match-beginning 0))))
+ (goto-char orig)
+ (beginning-of-line 2)
+ (indent-region (point) end))
+ (goto-char orig))
t))))
(defun ruby-do-end-to-brace ()
(when (and (or (bolp)
(not (memq (char-syntax (preceding-char)) '(?w ?_))))
(looking-at "\\<do\\(\\s \\|$\\)"))
- (let ((orig (point)) (end (progn (ruby-forward-sexp) (point))))
+ (let ((orig (point)) (end (progn (ruby-forward-sexp) (point)))
+ first last)
(backward-char 3)
(when (looking-at ruby-block-end-re)
(delete-char 3)
(insert "}")
+ (setq last (and (eolp)
+ (progn (backward-char 1)
+ (skip-syntax-backward " ")
+ (bolp))
+ (1- (point-at-eol -1))))
(goto-char orig)
(delete-char 2)
(insert "{")
+ (setq orig (point))
+ (when (and last (<= last (point))
+ (not (search-forward "#" (setq first (point-at-eol)) t)))
+ (goto-char (- end 4))
+ (end-of-line 0)
+ (if (looking-at "\n\\s *")
+ (delete-char (- (match-end 0) (match-beginning 0))) t)
+ (goto-char first)
+ (if (looking-at "\n\\s *")
+ (delete-char (- (match-end 0) (match-beginning 0))) t))
+ (goto-char orig)
(if (looking-at "\\s +|")
(delete-char (- (match-end 0) (match-beginning 0) 1)))
t))))