Skip to content

Commit

Permalink
feat: Add cache for faster speed
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs090218 committed Dec 23, 2023
1 parent b73add5 commit 9836253
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
6 changes: 3 additions & 3 deletions Eask
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

(script "test" "echo \"Error: no test specified\" && exit 1")

(source "gnu")
(source "jcs-elpa")
(source "melpa")
(source 'gnu)
(source 'jcs-elpa)
(source 'melpa)

(depends-on "emacs" "26.1")
(depends-on "company")
Expand Down
21 changes: 17 additions & 4 deletions company-c-headers.el
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ that paths in `company-c-headers-path-system' are implicitly appended."
(objc-mode . ,(rx ".h" line-end)))
"Assoc list of supported major modes and associated header file names.")

(defvar company-c-headers--cache nil
"Cache for candidates.")

(defun company-c-headers--call-if-function (path)
"If PATH is bound to a function, return the result of calling it.
Otherwise just return the value."
Expand Down Expand Up @@ -132,13 +135,17 @@ Filters on the appropriate regex for the current major mode."
candidates)
(while p
(when (file-directory-p (car p))
(setq candidates (append candidates (company-c-headers--candidates-for prefix (car p)))))

(setq candidates (append candidates
(company-c-headers--candidates-for prefix (car p)))))
(setq p (or (cdr p)
(let ((tmp next))
(setq next nil)
tmp))))
(cl-remove-duplicates candidates :test 'equal)))

;; Set cache
(setq company-c-headers--cache (cl-remove-duplicates candidates :test 'equal))

company-c-headers--cache))

(defun company-c-headers--meta (candidate)
"Return the metadata associated with CANDIDATE. Currently just the directory."
Expand All @@ -161,7 +168,8 @@ Filters on the appropriate regex for the current major mode."
(looking-back company-c-headers-include-declaration (line-beginning-position)))
(match-string-no-properties 1)))
(`sorted t)
(`candidates (company-c-headers--candidates arg))
(`candidates (or company-c-headers--cache
(company-c-headers--candidates arg)))
(`meta (company-c-headers--meta arg))
(`location (company-c-headers--location arg))
(`post-completion
Expand Down Expand Up @@ -223,5 +231,10 @@ Filters on the appropriate regex for the current major mode."
((eq system-type 'gnu/linux)
'("/usr/include/" "/usr/local/include/"))))

(defun company-c-headers-clear-cache ()
"Clear cache."
(interactive)
(setq company-c-headers--cache nil))

(provide 'company-c-headers)
;;; company-c-headers.el ends here

0 comments on commit 9836253

Please sign in to comment.