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"))
(defcli-group! "Module commands"
(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))
(load! cli-file path t)))))
(doom-load path t)))))
(load! cli-file doom-user-dir t))

View file

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

View file

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

View file

@ -466,7 +466,7 @@ ones."
(let (doom-packages)
(doom--read-packages private-packages nil 'noerror))
(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-flags = (doom-module-get (car key) (cdr key) :flags)
do (doom--read-packages path nil 'noerror)))