commit:     bfb0fac0258f2fb2e561e7066378ecf88e84d413
Author:     Maciej Barć <xgqt <AT> gentoo <DOT> org>
AuthorDate: Tue Aug 30 18:21:31 2022 +0000
Commit:     Maciej Barć <xgqt <AT> gentoo <DOT> org>
CommitDate: Tue Aug 30 18:21:31 2022 +0000
URL:        
https://gitweb.gentoo.org/proj/company-ebuild.git/commit/?id=bfb0fac0

company-ebuild.el: cache eclasses

Signed-off-by: Maciej Barć <xgqt <AT> gentoo.org>

 company-ebuild.el | 54 ++++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 38 insertions(+), 16 deletions(-)

diff --git a/company-ebuild.el b/company-ebuild.el
index 5c2272a..bcee8f5 100644
--- a/company-ebuild.el
+++ b/company-ebuild.el
@@ -24,7 +24,7 @@
 ;; Version: 0.1.1
 ;; Keywords: languages
 ;; Homepage: https://gitweb.gentoo.org/proj/company-ebuild.git
-;; Package-Requires: ((emacs "25.1"))
+;; Package-Requires: ((emacs "26.2"))
 ;; SPDX-License-Identifier: GPL-2.0-or-later
 
 
@@ -150,26 +150,48 @@ REPO-ROOT is the location from which we start searching 
for Eclass files."
       (when (file-exists-p repo-eclass)
         (directory-files repo-eclass t ".*\\.eclass" t)))))
 
+(defvar company-ebuild--eclass-mtimes '()
+  "Cache to prevent accessing eclasses multiple times.
+
+This is a global value holding a list of pairs.
+The key is an eclass path and the value is it's last modification time.
+This variable primarily is used in
+`company-ebuild--regenerate-dynamic-keywords-eclass'.")
+
+(defun company-ebuild--mtime (file-path)
+  "Return the modification time of a file at FILE-PATH."
+  (file-attribute-modification-time (file-attributes file-path)))
+
 (defun company-ebuild--regenerate-dynamic-keywords-eclass ()
   "Set new content of the ‘company-ebuild--dynamic-keywords’ Eclass variables."
   (let ((repo-root
          (company-ebuild--find-repo-root buffer-file-name)))
     (when repo-root
-      (let ((eclass-files
-             (company-ebuild--find-eclass-files repo-root)))
-        (mapc
-         (lambda (eclass-file)
-           (mapc (lambda (str)
-                   (add-to-list 'company-ebuild--dynamic-keywords-eclasses
-                                (replace-regexp-in-string "\\.eclass" "" str)))
-                 (company-ebuild--get-tags eclass-file "ECLASS"))
-           (mapc (lambda (str)
-                   (add-to-list 'company-ebuild--dynamic-keywords-variables 
str))
-                 (company-ebuild--get-tags eclass-file "ECLASS_VARIABLE"))
-           (mapc (lambda (str)
-                   (add-to-list 'company-ebuild--dynamic-keywords-functions 
str))
-                 (company-ebuild--get-tags eclass-file "FUNCTION")))
-         eclass-files)))))
+      (mapc
+       (lambda (eclass-file)
+         (let ((eclass-file-mtime
+                (company-ebuild--mtime eclass-file)))
+           (unless (equal (cdr (assoc eclass-file
+                                      company-ebuild--eclass-mtimes))
+                          eclass-file-mtime)
+             (assoc-delete-all eclass-file company-ebuild--eclass-mtimes)
+             (push `(,eclass-file . ,eclass-file-mtime)
+                   company-ebuild--eclass-mtimes)
+             (mapc (lambda (str)
+                     (add-to-list 'company-ebuild--dynamic-keywords-eclasses
+                                  (replace-regexp-in-string "\\.eclass"
+                                                            ""
+                                                            str)))
+                   (company-ebuild--get-tags eclass-file "ECLASS"))
+             (mapc (lambda (str)
+                     (add-to-list 'company-ebuild--dynamic-keywords-variables
+                                  str))
+                   (company-ebuild--get-tags eclass-file "ECLASS_VARIABLE"))
+             (mapc (lambda (str)
+                     (add-to-list 'company-ebuild--dynamic-keywords-functions
+                                  str))
+                   (company-ebuild--get-tags eclass-file "FUNCTION")))))
+       (company-ebuild--find-eclass-files repo-root)))))
 
 (defun company-ebuild--regenerate-dynamic-keywords-use-flags ()
   "Set new content of the ‘company-ebuild--dynamic-keywords-use-flags’ 
variable."

Reply via email to