Skip to content

Commit ba0d97e

Browse files
[completion] Cache last result of cider-complete
1 parent 1cd6ab7 commit ba0d97e

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

cider-completion.el

+24-4
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,25 @@ completion functionality."
142142
(mapcar #'cider-completion--parse-candidate-map (cider-sync-request:completion prefix)))
143143
(t nil)))
144144

145+
(defvar cider-complete-last-result nil
146+
"Stores a cons where car is a list of (PREFIX BUFFER POINT), and the cdr is
147+
the last completion result.")
148+
149+
(defun cider-complete-with-cache (prefix)
150+
"Call `cider-complete', optionally returning the result from cache.
151+
This function will use cached result if prefix and other completion
152+
conditions have not changed."
153+
(when (cider-connected-p)
154+
(let ((cached (with-current-buffer (cider-current-repl)
155+
cider-complete-last-result))
156+
(key (list prefix (current-buffer) (point))))
157+
(if (equal key (car cached))
158+
(cdr cached)
159+
(let* ((result (cider-complete prefix)))
160+
(with-current-buffer (cider-current-repl)
161+
(setq-local cider-complete-last-result (cons key result)))
162+
result)))))
163+
145164
(defun cider-completion--get-candidate-type (symbol)
146165
"Get candidate type for SYMBOL."
147166
(let ((type (get-text-property 0 'type symbol)))
@@ -189,16 +208,17 @@ performed by `cider-annotate-completion-function'."
189208
;; When the 'action is 'metadata, this lambda returns metadata about this
190209
;; capf, when action is (boundaries . suffix), it returns nil. With every
191210
;; other value of 'action (t, nil, or lambda), 'action is forwarded to
192-
;; (complete-with-action), together with (cider-complete), prefix and pred.
193-
;; And that function performs the completion based on those arguments.
211+
;; (complete-with-action), together with (cider-complete-with-cache),
212+
;; prefix and pred. And that function performs the completion based
213+
;; on those arguments.
194214
;;
195215
;; This api is better described in the section
196216
;; '21.6.7 Programmed Completion' of the elisp manual.
197217
(cond ((eq action 'metadata) `(metadata (category . cider))) ;; defines a completion category named 'cider, used later in our `completion-category-overrides` logic.
198218
((eq (car-safe action) 'boundaries) nil)
199219
(t (with-current-buffer (current-buffer)
200-
(complete-with-action action
201-
(cider-complete prefix) prefix pred)))))
220+
(complete-with-action
221+
action (cider-complete-with-cache prefix) prefix pred)))))
202222
:annotation-function #'cider-annotate-symbol
203223
:company-kind #'cider-company-symbol-kind
204224
:company-doc-buffer #'cider-create-compact-doc-buffer

0 commit comments

Comments
 (0)