[前][次][番号順一覧][スレッド一覧]

ruby-changes:31954

From: nobu <ko1@a...>
Date: Fri, 6 Dec 2013 16:47:52 +0900 (JST)
Subject: [ruby-changes:31954] nobu:r44033 (trunk): ruby-mode.el: expand/unexpand block

nobu	2013-12-06 16:47:47 +0900 (Fri, 06 Dec 2013)

  New Revision: 44033

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=44033

  Log:
    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.

  Modified files:
    trunk/ChangeLog
    trunk/misc/ruby-mode.el
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 44032)
+++ ChangeLog	(revision 44033)
@@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri Dec  6 16:47:45 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* 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.
+
 Fri Dec  6 16:16:30 2013  Koichi Sasada  <ko1@a...>
 
 	* gc.c (gc_start_internal): do not use rb_gc_start() and rb_gc().
Index: misc/ruby-mode.el
===================================================================
--- misc/ruby-mode.el	(revision 44032)
+++ misc/ruby-mode.el	(revision 44033)
@@ -1198,11 +1198,17 @@ balanced expression is found." https://github.com/ruby/ruby/blob/trunk/misc/ruby-mode.el#L1198
 
 (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." https://github.com/ruby/ruby/blob/trunk/misc/ruby-mode.el#L1220
 	(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))))

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]