diff --git a/cider-completion.el b/cider-completion.el index 9f83e4715..db3aae41d 100644 --- a/cider-completion.el +++ b/cider-completion.el @@ -142,6 +142,25 @@ completion functionality." (mapcar #'cider-completion--parse-candidate-map (cider-sync-request:completion prefix))) (t nil))) +(defvar cider-complete-last-result nil + "Stores a cons where car is a list of (PREFIX BUFFER POINT), and the cdr is +the last completion result.") + +(defun cider-complete-with-cache (prefix) + "Call `cider-complete', optionally returning the result from cache. +This function will use cached result if prefix and other completion +conditions have not changed." + (when (cider-connected-p) + (let ((cached (with-current-buffer (cider-current-repl) + cider-complete-last-result)) + (key (list prefix (current-buffer) (point)))) + (if (equal key (car cached)) + (cdr cached) + (let* ((result (cider-complete prefix))) + (with-current-buffer (cider-current-repl) + (setq-local cider-complete-last-result (cons key result))) + result))))) + (defun cider-completion--get-candidate-type (symbol) "Get candidate type for SYMBOL." (let ((type (get-text-property 0 'type symbol))) @@ -189,16 +208,17 @@ performed by `cider-annotate-completion-function'." ;; When the 'action is 'metadata, this lambda returns metadata about this ;; capf, when action is (boundaries . suffix), it returns nil. With every ;; other value of 'action (t, nil, or lambda), 'action is forwarded to - ;; (complete-with-action), together with (cider-complete), prefix and pred. - ;; And that function performs the completion based on those arguments. + ;; (complete-with-action), together with (cider-complete-with-cache), + ;; prefix and pred. And that function performs the completion based + ;; on those arguments. ;; ;; This api is better described in the section ;; '21.6.7 Programmed Completion' of the elisp manual. (cond ((eq action 'metadata) `(metadata (category . cider))) ;; defines a completion category named 'cider, used later in our `completion-category-overrides` logic. ((eq (car-safe action) 'boundaries) nil) (t (with-current-buffer (current-buffer) - (complete-with-action action - (cider-complete prefix) prefix pred))))) + (complete-with-action + action (cider-complete-with-cache prefix) prefix pred))))) :annotation-function #'cider-annotate-symbol :company-kind #'cider-company-symbol-kind :company-doc-buffer #'cider-create-compact-doc-buffer