From 0d9e2599ec037fe0582c36ea5e965cd6fa65d9dc Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Sun, 12 Aug 2012 18:06:56 -0400 Subject: [PATCH] Merge from upstream ruby-mode.el * lisp/progmodes/ruby-mode.el (ruby-mode-map): Remove unnecessary binding for `newline'. (ruby-move-to-block): When moving backward, stop at block opening, not indentation. * progmodes/ruby-mode.el (ruby-brace-to-do-end) (ruby-do-end-to-brace, ruby-toggle-block): New functions. * progmodes/ruby-mode.el (ruby-mode-map): Add binding for `ruby-toggle-block'. * test/automated/ruby-mode-tests.el (ruby-move-to-block-stops-at-opening) (ruby-toggle-block-to-do-end, ruby-toggle-block-to-brace): New test. --- lisp/ChangeLog | 27 +++++++++---- lisp/progmodes/ruby-mode.el | 66 +++++++++++++++++++++++++++---- test/ChangeLog | 9 +++++ test/automated/ruby-mode-tests.el | 26 ++++++++++++ 4 files changed, 114 insertions(+), 14 deletions(-) diff --git a/lisp/ChangeLog b/lisp/ChangeLog index bcaf4a328f5..6f54b9f7c0e 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,14 @@ +2012-08-12 Nobuyoshi Nakada + + * progmodes/ruby-mode.el (ruby-mode-map): Remove unnecessary + binding for `newline'. + (ruby-move-to-block): When moving backward, stop at block opening, + not indentation. + * progmodes/ruby-mode.el (ruby-brace-to-do-end) + (ruby-do-end-to-brace, ruby-toggle-block): New functions. + * progmodes/ruby-mode.el (ruby-mode-map): Add binding for + `ruby-toggle-block'. + 2012-08-12 Stefan Monnier * ibuffer.el (ibuffer-do-toggle-read-only): @@ -40,16 +51,18 @@ 2012-08-09 Dmitry Gutov - Merge stuff from upsteam ruby-mode, part 1 (bug#12169). + * progmodes/ruby-mode.el (ruby-expr-beg, ruby-parse-partial): + ?, _, and : are symbol constituents, ! is not (but kinda should be). + (ruby-syntax-propertize-heredoc): Use ruby-singleton-class-p. + (ruby-syntax-propertize-function): Adjust for changes in + `ruby-syntax-propertize-heredoc'. + +2012-08-09 Nobuyoshi Nakada + * progmodes/ruby-mode.el (ruby-mode-map): Remove deprecated binding (use `M-;' instead). - (ruby-expr-beg, ruby-parse-partial): ?, _, and : are symbol - constituents, ! is not (but kinda should be). (ruby-singleton-class-p): New function. - (ruby-expr-beg, ruby-in-here-doc-p) - (ruby-syntax-propertize-heredoc): Use it. - (ruby-syntax-propertize-function): Adjust for changes in - `ruby-syntax-propertize-heredoc'. + (ruby-expr-beg, ruby-in-here-doc-p) Use it. 2012-08-10 Stefan Monnier diff --git a/lisp/progmodes/ruby-mode.el b/lisp/progmodes/ruby-mode.el index 68abaff21d2..744dd430658 100644 --- a/lisp/progmodes/ruby-mode.el +++ b/lisp/progmodes/ruby-mode.el @@ -150,7 +150,7 @@ This should only be called after matching against `ruby-here-doc-beg-re'." (define-key map (kbd "M-C-q") 'ruby-indent-exp) (define-key map (kbd "C-M-h") 'backward-kill-word) (define-key map (kbd "C-j") 'reindent-then-newline-and-indent) - (define-key map (kbd "C-m") 'newline) + (define-key map (kbd "C-c {") 'ruby-toggle-block) map) "Keymap used in Ruby mode.") @@ -881,10 +881,11 @@ or blocks containing the current block." ;; TODO: Make this work for n > 1, ;; make it not loop for n = 0, ;; document body - (let (start pos done down) - (setq start (ruby-calculate-indent)) - (setq down (looking-at (if (< n 0) ruby-block-end-re - (concat "\\<\\(" ruby-block-beg-re "\\)\\>")))) + (let ((orig (point)) + (start (ruby-calculate-indent)) + (down (looking-at (if (< n 0) ruby-block-end-re + (concat "\\<\\(" ruby-block-beg-re "\\)\\>")))) + pos done) (while (and (not done) (not (if (< n 0) (bobp) (eobp)))) (forward-line n) (cond @@ -907,8 +908,18 @@ or blocks containing the current block." (save-excursion (back-to-indentation) (if (looking-at (concat "\\<\\(" ruby-block-mid-re "\\)\\>")) - (setq done nil)))))) - (back-to-indentation)) + (setq done nil))))) + (back-to-indentation) + (when (< n 0) + (let ((eol (point-at-eol)) state next) + (if (< orig eol) (setq eol orig)) + (setq orig (point)) + (while (and (setq next (apply 'ruby-parse-partial eol state)) + (< (point) eol)) + (setq state next)) + (when (cdaadr state) + (goto-char (cdaadr state))) + (backward-word))))) (defun ruby-beginning-of-block (&optional arg) "Move backward to the beginning of the current block. @@ -1116,6 +1127,47 @@ See `add-log-current-defun-function'." (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 "\\ + + * automated/ruby-mode-tests.el (ruby-move-to-block-stops-at-opening) + (ruby-toggle-block-to-do-end, ruby-toggle-block-to-brace): New test. + 2012-08-11 Glenn Morris * automated/files.el: New file. @@ -12,6 +17,10 @@ Add tests for `ruby-deep-indent-paren' behavior. Port all tests from test/misc/test_ruby_mode.rb in Ruby repo. +2012-08-10 Nobuyoshi Nakada + + Original tests in test_ruby_mode.rb in upstream (author). + 2012-08-09 Dmitry Gutov * automated/ruby-mode-tests.el (ruby-should-indent) diff --git a/test/automated/ruby-mode-tests.el b/test/automated/ruby-mode-tests.el index f91b6e44b22..df51aa0d15a 100644 --- a/test/automated/ruby-mode-tests.el +++ b/test/automated/ruby-mode-tests.el @@ -191,6 +191,32 @@ VALUES-PLIST is a list with alternating index and value elements." | end |")) +(ert-deftest ruby-move-to-block-stops-at-opening () + (with-temp-buffer + (insert "def f\nend") + (beginning-of-line) + (ruby-mode) + (ruby-move-to-block -1) + (should (looking-at "f$")))) + +(ert-deftest ruby-toggle-block-to-do-end () + (with-temp-buffer + (insert "foo {|b|\n}\n") + (ruby-mode) + (search-backward "{") + (ruby-toggle-block) + (should (string= "foo do |b|\nend\n" (buffer-substring-no-properties + (point-min) (point-max)))))) + +(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)))))) + (provide 'ruby-mode-tests) ;;; ruby-mode-tests.el ends here -- 2.39.2