Skip to content

Commit

Permalink
Add cache candidates mechanism, avoid the duplicate calculation of ot…
Browse files Browse the repository at this point in the history
…her completion backends caused by update of some completion backend.
  • Loading branch information
manateelazycat committed Aug 4, 2023
1 parent ba34436 commit 2801130
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 75 deletions.
14 changes: 10 additions & 4 deletions acm/acm-backend-codeium.el
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,14 @@
(lisp-data-mode . 60)))

(defun acm-backend-codeium-candidates (keyword)
(when (and acm-backend-codeium-items
(>= (length keyword) acm-backend-codeium-candidate-min-length))
acm-backend-codeium-items))
(if (and (boundp 'acm-backend-codeium-cache-candiates)
acm-backend-codeium-cache-candiates)
acm-backend-codeium-cache-candiates
(when (and acm-backend-codeium-items
(>= (length keyword) acm-backend-codeium-candidate-min-length))
(setq-local acm-backend-codeium-cache-candiates acm-backend-codeium-items)

acm-backend-codeium-items)))

(defun acm-backend-codeium-candidate-expand (candidate-info bound-start &optional preview)
;; We need replace whole area with codeium label.
Expand All @@ -172,7 +177,8 @@
(plist-get candidate :documentation))

(defun acm-backend-codeium-clean ()
(setq-local acm-backend-codeium-items nil))
(setq-local acm-backend-codeium-items nil)
(setq-local acm-backend-codeium-cache-candiates nil))

(provide 'acm-backend-codeium)
;;; acm-backend-codeium.el ends here
11 changes: 10 additions & 1 deletion acm/acm-backend-copilot.el
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ in the proxy plist. For example:
(defvar-local acm-backend-copilot-items nil)

(defun acm-backend-copilot-candidates (keyword)
acm-backend-copilot-items)
(if (and (boundp 'acm-backend-copilot-cache-candiates)
acm-backend-copilot-cache-candiates)
acm-backend-copilot-cache-candiates
(setq-local acm-backend-copilot-cache-candiates acm-backend-copilot-items)

acm-backend-copilot-items))

(defun acm-backend-copilot-candidate-expand (candidate-info bound-start &optional preview)
;; We need replace whole area with copilot label.
Expand All @@ -71,5 +76,9 @@ in the proxy plist. For example:
(defun acm-backend-copilot-candidate-doc (candidate)
(plist-get candidate :documentation))

(defun acm-backend-copilot-clean ()
(setq-local acm-backend-copilot-items nil)
(setq-local acm-backend-copilot-cache-candiates nil))

(provide 'acm-backend-copilot)
;;; acm-backend-copilot.el ends here
34 changes: 22 additions & 12 deletions acm/acm-backend-elisp.el
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,24 @@
(defvar acm-backend-elisp-symbols-update-size 0)

(defun acm-backend-elisp-candidates (keyword)
(when (and (acm-is-elisp-mode-p)
(>= (length keyword) acm-backend-elisp-candidate-min-length))
(mapcar
(lambda (elisp-symbol)
(let ((symbol-type (acm-backend-elisp-symbol-type (intern elisp-symbol))))
(list :key elisp-symbol
:icon symbol-type
:label elisp-symbol
:display-label elisp-symbol
:annotation (capitalize symbol-type)
:backend "elisp")))
acm-backend-elisp-items)))
(if (and (boundp 'acm-backend-elisp-cache-candiates)
acm-backend-elisp-cache-candiates)
acm-backend-elisp-cache-candiates
(when (and (acm-is-elisp-mode-p)
(>= (length keyword) acm-backend-elisp-candidate-min-length))
(setq-local acm-backend-elisp-cache-candiates
(mapcar
(lambda (elisp-symbol)
(let ((symbol-type (acm-backend-elisp-symbol-type (intern elisp-symbol))))
(list :key elisp-symbol
:icon symbol-type
:label elisp-symbol
:display-label elisp-symbol
:annotation (capitalize symbol-type)
:backend "elisp")))
acm-backend-elisp-items))

acm-backend-elisp-cache-candiates)))

(defun acm-backend-elisp-candidate-doc (candidate)
(let* ((symbol (intern (plist-get candidate :label)))
Expand Down Expand Up @@ -221,6 +227,10 @@
(append (acm-backend-elisp-local-symbols)
(acm-backend-elisp-global-symbols)))

(defun acm-backend-elisp-clean ()
(setq-local acm-backend-elisp-items nil)
(setq-local acm-backend-elisp-cache-candiates nil))

(provide 'acm-backend-elisp)

;;; acm-backend-elisp.el ends here
45 changes: 25 additions & 20 deletions acm/acm-backend-lsp.el
Original file line number Diff line number Diff line change
Expand Up @@ -112,25 +112,29 @@
(defvar-local acm-backend-lsp-fetch-completion-item-ticker nil)

(defun acm-backend-lsp-candidates (keyword)
(let* ((candidates (list)))
(when (and
(>= (length keyword) acm-backend-lsp-candidate-min-length)
(boundp 'acm-backend-lsp-items)
acm-backend-lsp-items
(boundp 'acm-backend-lsp-server-names)
acm-backend-lsp-server-names
(hash-table-p acm-backend-lsp-items))
;; Sort multi-server items by
(dolist (server-name acm-backend-lsp-server-names)
(when-let* ((server-items (gethash server-name acm-backend-lsp-items)))
(maphash (lambda (k v)
(add-to-list 'candidates v t))
server-items))))

;; NOTE:
;; lsp-bridge has sort candidate at Python side,
;; please do not do secondary sorting here, elisp is very slow.
candidates))
(if (and (boundp 'acm-backend-lsp-cache-candidates)
acm-backend-lsp-cache-candidates)
acm-backend-lsp-cache-candidates
(let* ((candidates (list)))
(when (and
(>= (length keyword) acm-backend-lsp-candidate-min-length)
(boundp 'acm-backend-lsp-items)
acm-backend-lsp-items
(boundp 'acm-backend-lsp-server-names)
acm-backend-lsp-server-names
(hash-table-p acm-backend-lsp-items))
(dolist (server-name acm-backend-lsp-server-names)
(when-let* ((server-items (gethash server-name acm-backend-lsp-items)))
(maphash (lambda (k v)
(add-to-list 'candidates v t))
server-items))))

(setq-local acm-backend-lsp-cache-candidates candidates)

;; NOTE:
;; lsp-bridge has sort candidate at Python side,
;; please do not do secondary sorting here, elisp is very slow.
candidates)))

(defun acm-backend-lsp-candidate-expand (candidate-info bound-start &optional preview)
(let* ((label (plist-get candidate-info :label))
Expand Down Expand Up @@ -266,7 +270,8 @@ Doubles as an indicator of snippet support."
(insert new-text))))

(defun acm-backend-lsp-clean ()
(setq-local acm-backend-lsp-items (make-hash-table :test 'equal)))
(setq-local acm-backend-lsp-items (make-hash-table :test 'equal))
(setq-local acm-backend-lsp-cache-candidates nil))

(provide 'acm-backend-lsp)

Expand Down
13 changes: 11 additions & 2 deletions acm/acm-backend-path.el
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,13 @@
(defvar-local acm-backend-path-items nil)

(defun acm-backend-path-candidates (keyword)
(when acm-enable-path
acm-backend-path-items))
(if (and (boundp 'acm-backend-path-cache-candiates)
acm-backend-path-cache-candiates)
acm-backend-path-cache-candiates
(when acm-enable-path
(setq-local acm-backend-path-cache-candiates acm-backend-path-items)

acm-backend-path-items)))

(defun acm-backend-path-candidate-expand (candidate-info bound-start &optional preview)
(let* ((keyword (acm-get-input-prefix))
Expand All @@ -114,6 +119,10 @@
(delete-region bound-start (point))
(insert (concat parent-dir file-name)))))

(defun acm-backend-path-clean ()
(setq-local acm-backend-path-items nil)
(setq-local acm-backend-path-cache-candiates nil))

(provide 'acm-backend-path)

;;; acm-backend-path.el ends here
32 changes: 20 additions & 12 deletions acm/acm-backend-search-file-words.el
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,24 @@
(defvar acm-backend-search-file-words-bound-regex "^[\"' ]")

(defun acm-backend-search-file-words-candidates (keyword)
(when (and acm-enable-search-file-words
(>= (length keyword) acm-backend-search-file-words-candidate-min-length))
(mapcar
(lambda (candidate-label)
(list :key candidate-label
:icon "search"
:label candidate-label
:display-label candidate-label
:annotation "Search Word"
:backend "search-file-words"))
acm-backend-search-file-words-items)))
(if (and (boundp 'acm-backend-search-file-words-cache-candiates)
acm-backend-search-file-words-cache-candiates)
acm-backend-search-file-words-cache-candiates

(when (and acm-enable-search-file-words
(>= (length keyword) acm-backend-search-file-words-candidate-min-length))
(setq-local acm-backend-search-file-words-cache-candiates
(mapcar
(lambda (candidate-label)
(list :key candidate-label
:icon "search"
:label candidate-label
:display-label candidate-label
:annotation "Search Word"
:backend "search-file-words"))
acm-backend-search-file-words-items))

acm-backend-search-file-words-cache-candiates)))

(defun acm-backend-search-file-words-candidate-expand (candidate-info bound-start &optional preview)
(let ((beg (if (acm-is-elisp-mode-p)
Expand Down Expand Up @@ -147,7 +154,8 @@
)))

(defun acm-backend-search-file-words-clean ()
(setq-local acm-backend-search-file-words-items nil))
(setq-local acm-backend-search-file-words-items nil)
(setq-local acm-backend-search-file-words-cache-candiates nil))

(provide 'acm-backend-search-file-words)

Expand Down
10 changes: 8 additions & 2 deletions acm/acm-backend-search-sdcv-words.el
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,12 @@ you need set this value to `/usr/share/stardict/dic/stardict-oxford-gb-formated-
(defvar-local acm-backend-search-sdcv-words-items nil)

(defun acm-backend-search-sdcv-words-candidates (keyword)
acm-backend-search-sdcv-words-items)
(if (and (boundp 'acm-backend-search-sdcv-words-cache-candiates)
acm-backend-search-sdcv-words-cache-candiates)
acm-backend-search-sdcv-words-cache-candiates
(setq-local acm-backend-search-sdcv-words-cache-candiates acm-backend-search-sdcv-words-items)

acm-backend-search-sdcv-words-items))

(defun acm-backend-search-sdcv-words-candidate-expand (candidate-info bound-start &optional preview)
(if preview
Expand All @@ -115,7 +120,8 @@ you need set this value to `/usr/share/stardict/dic/stardict-oxford-gb-formated-
(insert (plist-get candidate-info :display-label))))

(defun acm-backend-search-sdcv-words-clean ()
(setq-local acm-backend-search-sdcv-words-items nil))
(setq-local acm-backend-search-sdcv-words-items nil)
(setq-local acm-backend-search-sdcv-words-cache-candiates nil))

(provide 'acm-backend-search-sdcv-words)

Expand Down
12 changes: 9 additions & 3 deletions acm/acm-backend-tabnine.el
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@
(defvar-local acm-backend-tabnine-items nil)

(defun acm-backend-tabnine-candidates (keyword)
(when acm-backend-tabnine-items
acm-backend-tabnine-items))
(if (and (boundp 'acm-backend-tabnine-cache-candiates)
acm-backend-tabnine-cache-candiates)
acm-backend-tabnine-cache-candiates
(when acm-backend-tabnine-items
(setq-local acm-backend-tabnine-cache-candiates acm-backend-tabnine-items)

acm-backend-tabnine-items)))

(defun acm-backend-tabnine-candidate-expand (candidate-info bound-start &optional preview)
;; Insert TabNine suggestion.
Expand All @@ -36,7 +41,8 @@
(insert new_suffix))))))

(defun acm-backend-tabnine-clean ()
(setq-local acm-backend-tabnine-items nil))
(setq-local acm-backend-tabnine-items nil)
(setq-local acm-backend-tabnine-cache-candiates nil))

(provide 'acm-backend-tabnine)
;;; acm-backend-tabnine.el ends here
28 changes: 19 additions & 9 deletions acm/acm-backend-tailwind.el
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,25 @@
(defvar-local acm-backend-tailwind-items nil)

(defun acm-backend-tailwind-candidates (keyword)
(mapcar
(lambda (tailwind-symbol)
(list :key tailwind-symbol
:icon "tailwind"
:label tailwind-symbol
:display-label tailwind-symbol
:annotation "Tailwind"
:backend "tailwind"))
acm-backend-tailwind-items))
(if (and (boundp 'acm-backend-tailwind-cache-candiates)
acm-backend-tailwind-cache-candiates)
acm-backend-tailwind-cache-candiates
(setq-local acm-backend-tailwind-cache-candiates
(mapcar
(lambda (tailwind-symbol)
(list :key tailwind-symbol
:icon "tailwind"
:label tailwind-symbol
:display-label tailwind-symbol
:annotation "Tailwind"
:backend "tailwind"))
acm-backend-tailwind-items))

acm-backend-tailwind-cache-candiates))

(defun acm-backend-tailwind-clean ()
(setq-local acm-backend-tailwind-items nil)
(setq-local acm-backend-tailwind-cache-candiates nil))

(provide 'acm-backend-tailwind)

Expand Down
38 changes: 28 additions & 10 deletions lsp-bridge.el
Original file line number Diff line number Diff line change
Expand Up @@ -1040,12 +1040,14 @@ So we build this macro to restore postion after code format."

(defun lsp-bridge-completion--record-items (filename
filehost
candidates position
candidates
position
server-name
completion-trigger-characters
server-names)
(lsp-bridge--with-file-buffer filename filehost
;; Save completion items.
(setq-local acm-backend-lsp-cache-candidates nil)
(setq-local acm-backend-lsp-completion-position position)
(setq-local acm-backend-lsp-completion-trigger-characters completion-trigger-characters)
(setq-local acm-backend-lsp-server-names server-names)
Expand Down Expand Up @@ -1434,6 +1436,7 @@ So we build this macro to restore postion after code format."

(defun lsp-bridge-elisp-symbols-record (candidates)
(setq-local acm-backend-elisp-items candidates)
(setq-local acm-backend-elisp-cache-candiates nil)
(lsp-bridge-try-completion))

(defun lsp-bridge-search-words-index-files ()
Expand Down Expand Up @@ -1853,11 +1856,12 @@ Default is `bottom-right', you can choose other value: `top-left', `top-right',
(not (file-exists-p (buffer-file-name))))
(save-buffer)))

(setq-local acm-backend-lsp-completion-trigger-characters nil)
(setq-local acm-backend-lsp-cache-candidates nil)
(setq-local acm-backend-lsp-completion-position nil)
(setq-local acm-backend-lsp-completion-trigger-characters nil)
(setq-local acm-backend-lsp-server-names nil)
(setq-local acm-backend-lsp-filepath (lsp-bridge-get-buffer-truename))
(setq-local acm-backend-lsp-items (make-hash-table :test 'equal))
(setq-local acm-backend-lsp-server-names nil)

(when lsp-bridge-enable-signature-help
(acm-run-idle-func lsp-bridge-signature-help-timer lsp-bridge-signature-help-fetch-idle 'lsp-bridge-signature-help-fetch))
Expand Down Expand Up @@ -2249,13 +2253,27 @@ We need exclude `markdown-code-fontification:*' buffer in `lsp-bridge-monitor-be

(defun lsp-bridge-search-backend--record-items (backend-name items)
(pcase backend-name
("codeium" (setq-local acm-backend-codeium-items items))
("copilot" (setq-local acm-backend-copilot-items items))
("file-words" (setq-local acm-backend-search-file-words-items items))
("sdcv-words" (setq-local acm-backend-search-sdcv-words-items items))
("tabnine" (setq-local acm-backend-tabnine-items items))
("tailwind-keywords" (setq-local acm-backend-tailwind-items items))
("paths" (setq-local acm-backend-path-items items)))
("codeium"
(setq-local acm-backend-codeium-items items)
(setq-local acm-backend-codeium-cache-candiates nil))
("copilot"
(setq-local acm-backend-copilot-items items)
(setq-local acm-backend-copilot-cache-candiates nil))
("file-words"
(setq-local acm-backend-search-file-words-items items)
(setq-local acm-backend-search-file-words-cache-candiates nil))
("sdcv-words"
(setq-local acm-backend-search-sdcv-words-items items)
(setq-local acm-backend-search-sdcv-words-cache-candiates nil))
("tabnine"
(setq-local acm-backend-tabnine-items items)
(setq-local acm-backend-tabnine-cache-candiates nil))
("tailwind-keywords"
(setq-local acm-backend-tailwind-items items)
(setq-local acm-backend-tailwind-cache-candiates nil))
("paths"
(setq-local acm-backend-path-items items)
(setq-local acm-backend-path-cache-candiates nil)))
(lsp-bridge-try-completion))


Expand Down

0 comments on commit 2801130

Please sign in to comment.