Commit b15e8800 authored by Fabián Ezequiel Gallina's avatar Fabián Ezequiel Gallina Committed by Fabián Ezequiel Gallina

`python-nav-list-defun-positions' now caches defuns positions (#75)

This is a simplified version of @dandavison pull request (thanks dan!)

`python-nav-list-defun-positions' now uses
`python-nav-list-defun-positions-cache' buffer local variable to store
cached values of defun positions.

`python-nav-jump-to-defun' now benefits from this new cache and if
called with prefix argument it will invalidate it so new defuns are
scanned.

New Vars:
 + `python-nav-list-defun-positions-cache'
parent 0d85f465
......@@ -1134,30 +1134,39 @@ With negative argument, move backward repeatedly to start of sentence."
(forward-line -1)
(setq arg (1+ arg))))
(defun python-nav-list-defun-positions (&optional include-type)
(defvar python-nav-list-defun-positions-cache nil)
(make-variable-buffer-local 'python-nav-list-defun-positions-cache)
(defun python-nav-list-defun-positions (&optional include-type rescan)
"Make an Alist of defun names and point markers for current buffer.
When optional argument INCLUDE-TYPE is non-nil the type is
included the defun name."
(let ((defs))
(save-restriction
(widen)
(save-excursion
(goto-char (point-max))
(while (re-search-backward python-nav-beginning-of-defun-regexp nil t)
(when (and (not (python-info-ppss-context 'string))
(not (python-info-ppss-context 'comment))
(not (python-info-ppss-context 'parent)))
(add-to-list
'defs (cons
(python-info-current-defun include-type)
(point-marker)))))
defs))))
(defun python-nav-read-defun ()
included the defun name. With optional argument RESCAN the
`python-nav-list-defun-positions-cache' is invalidated and the
list of defun is regenerated again."
(if (and python-nav-list-defun-positions-cache (not rescan))
python-nav-list-defun-positions-cache
(let ((defs))
(save-restriction
(widen)
(save-excursion
(goto-char (point-max))
(while (re-search-backward python-nav-beginning-of-defun-regexp nil t)
(when (and (not (python-info-ppss-context 'string))
(not (python-info-ppss-context 'comment))
(not (python-info-ppss-context 'parent)))
(add-to-list
'defs (cons
(python-info-current-defun include-type)
(point-marker)))))
(setq python-nav-list-defun-positions-cache defs))))))
(defun python-nav-read-defun (&optional rescan)
"Read a defun name of current buffer and return its point marker.
A cons cell with the form (DEFUN-NAME . POINT-MARKER) is returned
when defun is completed, else nil."
(let ((defs (python-nav-list-defun-positions)))
when defun is completed, else nil. With optional argument RESCAN
forces `python-nav-list-defun-positions' to invalidate its
cache."
(let ((defs (python-nav-list-defun-positions nil rescan)))
(minibuffer-with-setup-hook
(lambda ()
(setq minibuffer-completion-table (mapcar 'car defs)))
......@@ -1169,9 +1178,11 @@ when defun is completed, else nil."
(assoc-string stringdef defs))))))
(defun python-nav-jump-to-defun (def)
"Jump to the definition of DEF in current file."
"Jump to the definition of DEF in current file.
Locations are cached; use a C-u prefix argument to force a
rescan."
(interactive
(list (python-nav-read-defun)))
(list (python-nav-read-defun current-prefix-arg)))
(when (not (called-interactively-p 'interactive))
(setq def (assoc-string def (python-nav-list-defun-positions))))
(let ((def-marker (cdr def)))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment