From d4529f6a3f85cb9e41b5a72a2bd2f8f877cbc620 Mon Sep 17 00:00:00 2001 From: Andy Stewart Date: Sat, 5 Aug 2023 10:27:37 +0800 Subject: [PATCH] Use acm-with-cache-candidates refactor code. --- acm/acm-backend-codeium.el | 13 ++++----- acm/acm-backend-copilot.el | 9 ++---- acm/acm-backend-elisp.el | 30 ++++++++----------- acm/acm-backend-lsp.el | 43 +++++++++++++--------------- acm/acm-backend-path.el | 11 +++---- acm/acm-backend-search-file-words.el | 31 +++++++++----------- acm/acm-backend-search-sdcv-words.el | 9 ++---- acm/acm-backend-tabnine.el | 11 +++---- acm/acm-backend-tailwind.el | 26 +++++++---------- acm/acm.el | 7 +++++ 10 files changed, 82 insertions(+), 108 deletions(-) diff --git a/acm/acm-backend-codeium.el b/acm/acm-backend-codeium.el index 254e343d61..c736a45656 100644 --- a/acm/acm-backend-codeium.el +++ b/acm/acm-backend-codeium.el @@ -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. diff --git a/acm/acm-backend-copilot.el b/acm/acm-backend-copilot.el index 0345a02d40..5d79e6b2bf 100644 --- a/acm/acm-backend-copilot.el +++ b/acm/acm-backend-copilot.el @@ -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. diff --git a/acm/acm-backend-elisp.el b/acm/acm-backend-elisp.el index 384482de1f..83a931295d 100644 --- a/acm/acm-backend-elisp.el +++ b/acm/acm-backend-elisp.el @@ -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))) diff --git a/acm/acm-backend-lsp.el b/acm/acm-backend-lsp.el index db9014208f..4f513bf95b 100644 --- a/acm/acm-backend-lsp.el +++ b/acm/acm-backend-lsp.el @@ -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. diff --git a/acm/acm-backend-path.el b/acm/acm-backend-path.el index c562b4a5b9..380ee73d2c 100644 --- a/acm/acm-backend-path.el +++ b/acm/acm-backend-path.el @@ -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)) diff --git a/acm/acm-backend-search-file-words.el b/acm/acm-backend-search-file-words.el index c656b99d18..3f08cdfc38 100644 --- a/acm/acm-backend-search-file-words.el +++ b/acm/acm-backend-search-file-words.el @@ -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) diff --git a/acm/acm-backend-search-sdcv-words.el b/acm/acm-backend-search-sdcv-words.el index c318196e85..e18789c50a 100644 --- a/acm/acm-backend-search-sdcv-words.el +++ b/acm/acm-backend-search-sdcv-words.el @@ -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 diff --git a/acm/acm-backend-tabnine.el b/acm/acm-backend-tabnine.el index 61cd6e7acf..31e0427a79 100644 --- a/acm/acm-backend-tabnine.el +++ b/acm/acm-backend-tabnine.el @@ -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. diff --git a/acm/acm-backend-tailwind.el b/acm/acm-backend-tailwind.el index cb7ade206d..a0ff349ae1 100644 --- a/acm/acm-backend-tailwind.el +++ b/acm/acm-backend-tailwind.el @@ -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) diff --git a/acm/acm.el b/acm/acm.el index 217983bea9..42b1cd75ca 100644 --- a/acm/acm.el +++ b/acm/acm.el @@ -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)