Skip to content

Commit

Permalink
Use acm-with-cache-candidates refactor code.
Browse files Browse the repository at this point in the history
  • Loading branch information
manateelazycat committed Aug 5, 2023
1 parent cfbda35 commit d4529f6
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 108 deletions.
13 changes: 5 additions & 8 deletions acm/acm-backend-codeium.el
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,11 @@
(lisp-data-mode . 60)))

(defun acm-backend-codeium-candidates (keyword)
(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)))
(acm-with-cache-candidates
acm-backend-codeium-cache-candiates
(when (and acm-backend-codeium-items
(>= (length keyword) acm-backend-codeium-candidate-min-length))
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 Down
9 changes: 3 additions & 6 deletions acm/acm-backend-copilot.el
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,9 @@ in the proxy plist. For example:
(defvar-local acm-backend-copilot-items nil)

(defun acm-backend-copilot-candidates (keyword)
(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))
(acm-with-cache-candidates
acm-backend-copilot-cache-candiates
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 Down
30 changes: 12 additions & 18 deletions acm/acm-backend-elisp.el
Original file line number Diff line number Diff line change
Expand Up @@ -111,24 +111,18 @@
(defvar acm-backend-elisp-symbols-update-size 0)

(defun acm-backend-elisp-candidates (keyword)
(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)))
(acm-with-cache-candidates
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)))

(defun acm-backend-elisp-candidate-doc (candidate)
(let* ((symbol (intern (plist-get candidate :label)))
Expand Down
43 changes: 20 additions & 23 deletions acm/acm-backend-lsp.el
Original file line number Diff line number Diff line change
Expand Up @@ -113,29 +113,26 @@

(defun acm-backend-lsp-candidates (keyword)
(let ((match-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))))
(acm-with-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))))

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

;; When some LSP server very slow and other completion backend is fast,
;; acm menu will render all backend candidates.
Expand Down
11 changes: 4 additions & 7 deletions acm/acm-backend-path.el
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,10 @@
(defvar-local acm-backend-path-items nil)

(defun acm-backend-path-candidates (keyword)
(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)))
(acm-with-cache-candidates
acm-backend-path-cache-candiates
(when acm-enable-path
acm-backend-path-items)))

(defun acm-backend-path-candidate-expand (candidate-info bound-start &optional preview)
(let* ((keyword (acm-get-input-prefix))
Expand Down
31 changes: 13 additions & 18 deletions acm/acm-backend-search-file-words.el
Original file line number Diff line number Diff line change
Expand Up @@ -108,24 +108,19 @@
(defvar acm-backend-search-file-words-bound-regex "^[\"' ]")

(defun acm-backend-search-file-words-candidates (keyword)
(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)))
(acm-with-cache-candidates
acm-backend-search-file-words-cache-candiates
(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))))

(defun acm-backend-search-file-words-candidate-expand (candidate-info bound-start &optional preview)
(let ((beg (if (acm-is-elisp-mode-p)
Expand Down
9 changes: 3 additions & 6 deletions acm/acm-backend-search-sdcv-words.el
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,9 @@ 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)
(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))
(acm-with-cache-candidates
acm-backend-search-sdcv-words-cache-candiates
acm-backend-search-sdcv-words-items))

(defun acm-backend-search-sdcv-words-candidate-expand (candidate-info bound-start &optional preview)
(if preview
Expand Down
11 changes: 4 additions & 7 deletions acm/acm-backend-tabnine.el
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@
(defvar-local acm-backend-tabnine-items nil)

(defun acm-backend-tabnine-candidates (keyword)
(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)))
(acm-with-cache-candidates
acm-backend-tabnine-cache-candiates
(when acm-backend-tabnine-items
acm-backend-tabnine-items)))

(defun acm-backend-tabnine-candidate-expand (candidate-info bound-start &optional preview)
;; Insert TabNine suggestion.
Expand Down
26 changes: 11 additions & 15 deletions acm/acm-backend-tailwind.el
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,17 @@
(defvar-local acm-backend-tailwind-items nil)

(defun acm-backend-tailwind-candidates (keyword)
(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))
(acm-with-cache-candidates
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)))

(defun acm-backend-tailwind-clean ()
(setq-local acm-backend-tailwind-items nil)
Expand Down
7 changes: 7 additions & 0 deletions acm/acm.el
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,13 @@
(setq ,timer
(run-with-idle-timer ,idle t #'(lambda () (funcall ,func))))))

(cl-defmacro acm-with-cache-candidates (cache-candidates &rest body)
`(if (and (boundp ',cache-candidates)
,cache-candidates)
,cache-candidates
(setq-local ,cache-candidates ,@body)
,cache-candidates))

(defvar acm-mode-map
(let ((map (make-sparse-keymap)))
(define-key map [remap next-line] #'acm-select-next)
Expand Down

0 comments on commit d4529f6

Please sign in to comment.