From a6bf9c56e9ff43241a0aa3231660c87e0c848ed6 Mon Sep 17 00:00:00 2001 From: Jen-Chieh Shen Date: Sat, 17 Dec 2022 00:31:47 +0800 Subject: [PATCH] Slightly improve performance --- company-fuzzy.el | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/company-fuzzy.el b/company-fuzzy.el index 9257f50..17c5015 100644 --- a/company-fuzzy.el +++ b/company-fuzzy.el @@ -294,13 +294,6 @@ See function `string-prefix-p' for arguments PREFIX, STRING and IGNORE-CASE." (setq result-lst (reverse result-lst)) (cl-remove-duplicates result-lst))) -(defun company-fuzzy--get-backend-by-candidate (candidate) - "Return the backend symbol by using CANDIDATE as search index." - (let ((match (ht-find (lambda (_backend cands) - (member candidate cands)) - company-fuzzy--candidates))) - (car match))) - (defun company-fuzzy--call-backend (backend command key) "Safely call BACKEND by COMMAND and KEY." (ignore-errors (funcall backend command key))) @@ -308,7 +301,7 @@ See function `string-prefix-p' for arguments PREFIX, STRING and IGNORE-CASE." (defun company-fuzzy--backend-command (candidate command) "Find the backend from the CANDIDATE then call the COMMAND." (unless (string-empty-p candidate) - (when-let ((backend (company-fuzzy--get-backend-by-candidate candidate))) + (when-let ((backend (ht-get company-fuzzy--candidates candidate))) (company-fuzzy--call-backend backend command candidate)))) ;; @@ -337,7 +330,7 @@ See function `string-prefix-p' for arguments PREFIX, STRING and IGNORE-CASE." (defun company-fuzzy--extract-annotation (candidate) "Extract annotation from CANDIDATE." - (let* ((backend (company-fuzzy--get-backend-by-candidate candidate)) + (let* ((backend (ht-get company-fuzzy--candidates candidate)) (backend-str (company-fuzzy--backend-string candidate backend)) (orig-anno (company-fuzzy--source-anno-string candidate backend))) (concat orig-anno backend-str))) @@ -469,7 +462,7 @@ If optional argument FLIP is non-nil, reverse query and pattern order." (when company-fuzzy-mode ;; NOTE: Here we force to change `company-prefix' so the completion ;; will do what we expected. - (let ((backend (company-fuzzy--get-backend-by-candidate candidate))) + (let ((backend (ht-get company-fuzzy--candidates candidate))) (setq company-prefix (company-fuzzy--backend-prefix backend 'complete))))) ;; @@ -572,7 +565,7 @@ P.S. Not all backend work this way." (defun company-fuzzy--backend-prefix-candidate (cand type) "Get the backend prefix by CAND and TYPE." - (let ((backend (company-fuzzy--get-backend-by-candidate cand))) + (let ((backend (ht-get company-fuzzy--candidates cand))) (company-fuzzy--backend-prefix backend type))) (defun company-fuzzy--backend-prefix (backend type) @@ -624,11 +617,7 @@ Insert .* between each char." (defun company-fuzzy--ht-all-candidates () "Return all candidates from the data." - (let (all-candidates) - (ht-map (lambda (_backend cands) - (setq all-candidates (append all-candidates cands))) - company-fuzzy--candidates) - (delete-dups all-candidates))) + (ht-keys company-fuzzy--candidates)) (defun company-fuzzy-all-candidates () "Return the list of all candidates." @@ -679,7 +668,8 @@ Insert .* between each char." (defun company-fuzzy--register-candidates (backend candidates) "Register CANDIDATES with BACKEND id." (delete-dups candidates) - (ht-set company-fuzzy--candidates backend (copy-sequence candidates))) + (dolist (cand candidates) + (ht-set company-fuzzy--candidates cand backend))) (defun company-fuzzy--collect-candidates (backend candidates) "Collect BACKEND's CANDIDATES by it's type."