]> code.delx.au - gnu-emacs/commitdiff
* lisp/progmodes/ruby-mode.el (ruby-add-log-current-method): Print the
authorDmitry Gutov <dgutov@yandex.ru>
Tue, 13 Nov 2012 09:30:16 +0000 (13:30 +0400)
committerDmitry Gutov <dgutov@yandex.ru>
Tue, 13 Nov 2012 09:30:16 +0000 (13:30 +0400)
period before class method names, not after.  Remove handling of
one impossible case.  Add comments.

* test/automated/ruby-mode-tests.el
(ruby-add-log-current-method-examples): New test.
(ruby-test-string): Extract from ruby-should-indent-buffer.

lisp/ChangeLog
lisp/progmodes/ruby-mode.el
test/ChangeLog
test/automated/ruby-mode-tests.el

index bab44db48a13507a5c778557280fcc51047679b1..fc69b8643b63c1f446660d673947fab87d7e435a 100644 (file)
@@ -1,3 +1,9 @@
+2012-11-13  Dmitry Gutov  <dgutov@yandex.ru>
+
+       * progmodes/ruby-mode.el (ruby-add-log-current-method): Print the
+       period before class method names, not after.  Remove handling of
+       one impossible case.  Add comments.
+
 2012-11-13  Stefan Monnier  <monnier@iro.umontreal.ca>
 
        * emacs-lisp/advice.el: Remove support for freezing.
index 9d2c6fa51f7a42e2d5e0023ac2dbeff24a326f35..7c72b73a879741744a454ddbaf76e2d219d7aacd 100644 (file)
@@ -1033,21 +1033,19 @@ For example:
   #exit
   String#gsub
   Net::HTTP#active?
-  File::open.
+  File.open
 
 See `add-log-current-defun-function'."
-  ;; TODO: Document body
-  ;; Why does this append a period to class methods?
   (condition-case nil
       (save-excursion
         (let (mname mlist (indent 0))
-          ;; get current method (or class/module)
+          ;; Get the current method definition (or class/module).
           (if (re-search-backward
                (concat "^[ \t]*" ruby-defun-beg-re "[ \t]+"
                        "\\("
-                       ;; \\. and :: for class method
-                        "\\([A-Za-z_]" ruby-symbol-re "*\\|\\.\\|::" "\\)"
-                        "+\\)")
+                       ;; \\. and :: for class methods
+                       "\\([A-Za-z_]" ruby-symbol-re "*\\|\\.\\|::" "\\)"
+                       "+\\)")
                nil t)
               (progn
                 (setq mname (match-string 2))
@@ -1056,7 +1054,7 @@ See `add-log-current-defun-function'."
                 (goto-char (match-beginning 1))
                 (setq indent (current-column))
                 (beginning-of-line)))
-          ;; nest class/module
+          ;; Walk up the class/module nesting.
           (while (and (> indent 0)
                       (re-search-backward
                        (concat
@@ -1069,28 +1067,26 @@ See `add-log-current-defun-function'."
                   (setq mlist (cons (match-string 2) mlist))
                   (setq indent (current-column))
                   (beginning-of-line))))
+          ;; Process the method name.
           (when mname
             (let ((mn (split-string mname "\\.\\|::")))
               (if (cdr mn)
                   (progn
-                    (cond
-                     ((string-equal "" (car mn))
-                      (setq mn (cdr mn) mlist nil))
-                     ((string-equal "self" (car mn))
-                      (setq mn (cdr mn)))
-                     ((let ((ml (nreverse mlist)))
+                    (unless (string-equal "self" (car mn)) ; def self.foo
+                      ;; def C.foo
+                      (let ((ml (nreverse mlist)))
+                        ;; If the method name references one of the
+                        ;; containing modules, drop the more nested ones.
                         (while ml
                           (if (string-equal (car ml) (car mn))
                               (setq mlist (nreverse (cdr ml)) ml nil))
-                          (or (setq ml (cdr ml)) (nreverse mlist))))))
-                    (if mlist
-                        (setcdr (last mlist) mn)
-                      (setq mlist mn))
-                    (setq mn (last mn 2))
-                    (setq mname (concat "." (cadr mn)))
-                    (setcdr mn nil))
+                          (or (setq ml (cdr ml)) (nreverse mlist))))
+                      (if mlist
+                          (setcdr (last mlist) (butlast mn))
+                        (setq mlist (butlast mn))))
+                    (setq mname (concat "." (car (last mn)))))
                 (setq mname (concat "#" mname)))))
-          ;; generate string
+          ;; Generate the string.
           (if (consp mlist)
               (setq mlist (mapconcat (function identity) mlist "::")))
           (if mname
index ad136117c1d0d990e48ff45cb028b141b0224c47..44c013e9887b580598745a5213d9f5322b2f23a0 100644 (file)
@@ -1,7 +1,9 @@
 2012-11-13  Dmitry Gutov  <dgutov@yandex.ru>
 
        * automated/ruby-mode-tests.el (ruby-heredoc-font-lock)
-       (ruby-singleton-class-no-heredoc-font-lock): New tests.
+       (ruby-singleton-class-no-heredoc-font-lock)
+       (ruby-add-log-current-method-examples): New tests.
+       (ruby-test-string): Extract from ruby-should-indent-buffer.
 
 2012-11-12  Stefan Monnier  <monnier@iro.umontreal.ca>
 
index 741692a07f7846edf7b9d98cfef79e541ab9f8c6..0e41b2ba1e23b2c490ab07c0313a6e82d847936c 100644 (file)
 
 The whitespace before and including \"|\" on each line is removed."
   (with-temp-buffer
-    (cl-flet ((fix-indent (s) (replace-regexp-in-string "^[ \t]*|" "" s)))
-      (insert (fix-indent content))
-      (ruby-mode)
-      (indent-region (point-min) (point-max))
-      (should (string= (fix-indent expected) (buffer-string))))))
+    (insert (ruby-test-string content))
+    (ruby-mode)
+    (indent-region (point-min) (point-max))
+    (should (string= (ruby-test-string expected) (buffer-string)))))
+
+(defun ruby-test-string (s &rest args)
+  (apply 'format (replace-regexp-in-string "^[ \t]*|" "" s) args))
 
 (defun ruby-assert-state (content &rest values-plist)
   "Assert syntax state values at the end of CONTENT.
@@ -261,6 +263,26 @@ VALUES-PLIST is a list with alternating index and value elements."
   (ruby-assert-face "# #{comment}\n \"#{interpolation}\"" 16
                     'font-lock-variable-name-face))
 
+(ert-deftest ruby-add-log-current-method-examples ()
+  (let ((pairs '(("foo" . "#foo")
+                 ("C.foo" . ".foo")
+                 ("self.foo" . ".foo"))))
+    (loop for (name . value) in pairs
+          do (with-temp-buffer
+               (insert (ruby-test-string
+                        "module M
+                        |  class C
+                        |    def %s
+                        |    end
+                        |  end
+                        |end"
+                        name))
+               (ruby-mode)
+               (search-backward "def")
+               (forward-line)
+               (should (string= (ruby-add-log-current-method)
+                                (format "M::C%s" value)))))))
+
 (provide 'ruby-mode-tests)
 
 ;;; ruby-mode-tests.el ends here