refactor: rethink doom-module-*-path functions

- Rename doom-module-path -> doom-module-expand-path, to better reflect
  its purpose.
- Optimize doom-module-locate-path to try caches and
  locate-file-internal, before looping through doom-modules-dirs.
- Rely on file-name-concat to join paths, rather than string
  concatenation. file-name-concat is more robust for the purpose and
  has lower overhead than expand-file-name.
This commit is contained in:
Henrik Lissner 2022-09-12 22:41:09 +02:00
parent a67b212b99
commit 775ee2f04a
No known key found for this signature in database
GPG key ID: B60957CA074D39A3
4 changed files with 28 additions and 26 deletions

View file

@ -295,9 +295,9 @@ SEE ALSO:
(let ((cli-file "cli")) (let ((cli-file "cli"))
(defcli-group! "Module commands" (defcli-group! "Module commands"
(dolist (key (hash-table-keys doom-modules)) (dolist (key (hash-table-keys doom-modules))
(when-let (path (plist-get (gethash key doom-modules) :path)) (when-let (path (doom-module-expand-path (car key) (cdr key) cli-file))
(defcli-group! :prefix (format "+%s" (cdr key)) (defcli-group! :prefix (format "+%s" (cdr key))
(load! cli-file path t))))) (doom-load path t)))))
(load! cli-file doom-user-dir t)) (load! cli-file doom-user-dir t))

View file

@ -251,8 +251,8 @@ in."
(condition-case-unless-debug ex (condition-case-unless-debug ex
(let ((doom--current-module key) (let ((doom--current-module key)
(doom--current-flags (plist-get plist :flags)) (doom--current-flags (plist-get plist :flags))
(doctor-file (doom-module-path (car key) (cdr key) "doctor.el")) (doctor-file (doom-module-expand-path (car key) (cdr key) "doctor.el"))
(packages-file (doom-module-path (car key) (cdr key) "packages.el"))) (packages-file (doom-module-expand-path (car key) (cdr key) "packages.el")))
(cl-loop with doom-output-indent = 6 (cl-loop with doom-output-indent = 6
for name in (let (doom-packages for name in (let (doom-packages
doom-disabled-packages) doom-disabled-packages)

View file

@ -180,16 +180,15 @@ Example:
(doom-module-set :lang 'haskell :flags '(+lsp))" (doom-module-set :lang 'haskell :flags '(+lsp))"
(puthash (cons category module) plist doom-modules)) (puthash (cons category module) plist doom-modules))
(defun doom-module-path (category module &optional file) (defun doom-module-expand-path (category module &optional file)
"Like `expand-file-name', but expands FILE relative to CATEGORY (keywordp) and "Expands a path to FILE relative to CATEGORY and MODULE.
MODULE (symbol).
If the category isn't enabled this will always return nil. For finding disabled CATEGORY is a keyword. MODULE is a symbol. FILE is an optional string path.
modules use `doom-module-locate-path'." If the category isn't enabled this returns nil. For finding disabled modules use
(let ((path (doom-module-get category module :path))) `doom-module-locate-path'."
(when-let (path (doom-module-get category module :path))
(if file (if file
(let (file-name-handler-alist) (file-name-concat path file)
(expand-file-name file path))
path))) path)))
(defun doom-module-locate-path (category &optional module file) (defun doom-module-locate-path (category &optional module file)
@ -197,19 +196,22 @@ modules use `doom-module-locate-path'."
CATEGORY is a keyword (e.g. :lang) and MODULE is a symbol (e.g. 'python). FILE CATEGORY is a keyword (e.g. :lang) and MODULE is a symbol (e.g. 'python). FILE
is a string that will be appended to the resulting path. If no path exists, this is a string that will be appended to the resulting path. If no path exists, this
returns nil, otherwise an absolute path. returns nil, otherwise an absolute path."
(let (file-name-handler-alist)
This doesn't require modules to be enabled. For enabled modules us (if-let (path (doom-module-expand-path category module file))
`doom-module-path'." (if (or (null file)
(when (keywordp category) (file-exists-p path))
(setq category (doom-keyword-name category))) path)
(when (and module (symbolp module)) (let* ((category (doom-keyword-name category))
(setq module (symbol-name module))) (module (if module (symbol-name module)))
(cl-loop with file-name-handler-alist = nil (path (file-name-concat category module file)))
for default-directory in doom-modules-dirs (if file
for path = (concat category "/" module "/" file) ;; PERF: locate-file-internal is a little faster for finding files,
;; but its interface for finding directories is clumsy.
(locate-file-internal path doom-modules-dirs)
(cl-loop for default-directory in doom-modules-dirs
if (file-exists-p path) if (file-exists-p path)
return (expand-file-name path))) return (expand-file-name path)))))))
(defun doom-module-from-path (path &optional enabled-only) (defun doom-module-from-path (path &optional enabled-only)
"Returns a cons cell (CATEGORY . MODULE) derived from PATH (a file path). "Returns a cons cell (CATEGORY . MODULE) derived from PATH (a file path).

View file

@ -466,7 +466,7 @@ ones."
(let (doom-packages) (let (doom-packages)
(doom--read-packages private-packages nil 'noerror)) (doom--read-packages private-packages nil 'noerror))
(cl-loop for key being the hash-keys of doom-modules (cl-loop for key being the hash-keys of doom-modules
for path = (doom-module-path (car key) (cdr key) packages-file) for path = (doom-module-expand-path (car key) (cdr key) packages-file)
for doom--current-module = key for doom--current-module = key
for doom--current-flags = (doom-module-get (car key) (cdr key) :flags) for doom--current-flags = (doom-module-get (car key) (cdr key) :flags)
do (doom--read-packages path nil 'noerror))) do (doom--read-packages path nil 'noerror)))