From: Dmitry Gutov Date: Thu, 14 Feb 2013 05:45:33 +0000 (+0400) Subject: (ruby-add-log-current-method): Improve performance at the expense X-Git-Tag: emacs-24.3.90~173^2~7^2~46 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=71a048c16b1a3c8708c161b38e52155b77d0ea60;p=emacs.git (ruby-add-log-current-method): Improve performance at the expense of accuracy. `ruby-block-contains-point' is relatively slow, so only use it for method and singleton class blocks. * test/automated/ruby-mode-tests.el (ruby-add-log-current-method-after-inner-class): Lower expectations: move point inside a method, initially. --- diff --git a/lisp/ChangeLog b/lisp/ChangeLog index fd520361889..6daf062814e 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -4,6 +4,9 @@ depth for unfinished percent literal. Not using it in the caller. (ruby-move-to-block): Jump over multiline literals of all types, ignoring code-looking contents inside them. + (ruby-add-log-current-method): Improve performance at the expense + of accuracy. `ruby-block-contains-point' is relatively slow, so + only use it for method and singleton class blocks. 2013-02-13 Michael Albinus diff --git a/lisp/progmodes/ruby-mode.el b/lisp/progmodes/ruby-mode.el index 453d08fc47b..9b007c0063a 100644 --- a/lisp/progmodes/ruby-mode.el +++ b/lisp/progmodes/ruby-mode.el @@ -1073,29 +1073,33 @@ For example: See `add-log-current-defun-function'." (condition-case nil (save-excursion - (let ((indent 0) mname mlist - (start (point)) - (definition-re - (concat "^[ \t]*" ruby-defun-beg-re "[ \t]+" - "\\(" - ;; \\. and :: for class methods - "\\([A-Za-z_]" ruby-symbol-re "*\\|\\.\\|::" "\\)" - "+\\)"))) + (let* ((indent 0) mname mlist + (start (point)) + (make-definition-re + (lambda (re) + (concat "^[ \t]*" re "[ \t]+" + "\\(" + ;; \\. and :: for class methods + "\\([A-Za-z_]" ruby-symbol-re "*\\|\\.\\|::" "\\)" + "+\\)"))) + (definition-re (funcall make-definition-re ruby-defun-beg-re)) + (module-re (funcall make-definition-re "\\(class\\|module\\)"))) ;; Get the current method definition (or class/module). (when (re-search-backward definition-re nil t) (goto-char (match-beginning 1)) - (when (ruby-block-contains-point start) - ;; We're inside the method, class or module. - (setq mname (match-string 2)) - (unless (string-equal "def" (match-string 1)) - (setq mlist (list mname) mname nil))) + (if (not (string-equal "def" (match-string 1))) + (setq mlist (list (match-string 2))) + ;; We're inside the method. For classes and modules, + ;; this check is skipped for performance. + (when (ruby-block-contains-point start) + (setq mname (match-string 2)))) (setq indent (current-column)) (beginning-of-line)) ;; Walk up the class/module nesting. (while (and (> indent 0) - (re-search-backward definition-re nil t)) + (re-search-backward module-re nil t)) (goto-char (match-beginning 1)) - (when (ruby-block-contains-point start) + (when (< (current-column) indent) (setq mlist (cons (match-string 2) mlist)) (setq indent (current-column)) (beginning-of-line))) @@ -1121,6 +1125,13 @@ See `add-log-current-defun-function'." (let ((in-singleton-class (when (re-search-forward ruby-singleton-class-re start t) (goto-char (match-beginning 0)) + ;; FIXME: Optimize it out, too? + ;; This can be slow in a large file, but + ;; unlike class/module declaration + ;; indentations, method definitions can be + ;; intermixed with these, and may or may not + ;; be additionally indented after visibility + ;; keywords. (ruby-block-contains-point start)))) (setq mname (concat (if in-singleton-class "." "#") diff --git a/test/ChangeLog b/test/ChangeLog index 7e08eccc2e3..7fed4f29408 100644 --- a/test/ChangeLog +++ b/test/ChangeLog @@ -4,6 +4,8 @@ (ruby-move-to-block-skips-percent-literal): Add depth-affecting bits inside the examples. (ruby-move-to-block-skips-heredoc): New test. + (ruby-add-log-current-method-after-inner-class): Lower + expectations: move point inside a method, initially. 2013-02-13 Dmitry Gutov diff --git a/test/automated/ruby-mode-tests.el b/test/automated/ruby-mode-tests.el index 9ee6462f6ad..c67f92e6ed9 100644 --- a/test/automated/ruby-mode-tests.el +++ b/test/automated/ruby-mode-tests.el @@ -390,11 +390,13 @@ VALUES-PLIST is a list with alternating index and value elements." | class C | class D | end - | _ + | def foo + | _ + | end | end |end") (search-backward "_") - (should (string= (ruby-add-log-current-method) "M::C")))) + (should (string= (ruby-add-log-current-method) "M::C#foo")))) (defvar ruby-block-test-example (ruby-test-string