Skip to content

Commit

Permalink
[completion] Cache last result of cider-complete
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-yakushev committed May 9, 2024
1 parent 1cd6ab7 commit ba0d97e
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions cider-completion.el
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ba0d97e

Please sign in to comment.