Refactor projects api

Improves correctness of projectile project state. Namely, prevents
projectile-project-root from poisoning the return value of
doom-project-root or doom-project-p, which may be causing an elusive bug
where doom-project-find-file is searching the wrong directory.
This commit is contained in:
Henrik Lissner 2019-04-09 03:13:18 -04:00
parent 98e5ddc1e1
commit d3a12a3914
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -13,6 +13,7 @@
you want to interactive with a project other than the one you're in." you want to interactive with a project other than the one you're in."
`(let ((projectile-project-root-cache (make-hash-table :test 'equal)) `(let ((projectile-project-root-cache (make-hash-table :test 'equal))
projectile-project-name projectile-project-name
projectile-project-root
projectile-require-project-root) projectile-require-project-root)
,@body)) ,@body))
@ -55,24 +56,33 @@ they are absolute."
;; Library ;; Library
;;;###autoload ;;;###autoload
(defalias 'doom-project-p #'projectile-project-p) (defun doom-project-p (&optional dir)
"Return t if DIR (defaults to `default-directory') is a valid project."
(and (doom-project-root dir)
t))
;;;###autoload ;;;###autoload
(defalias 'doom-project-root #'projectile-project-root) (defun doom-project-root (&optional dir)
"Return the project root of DIR (defaults to `default-directory').
Returns nil if not in a project."
(let ((projectile-project-root (unless dir projectile-project-root))
projectile-require-project-root)
(projectile-project-root dir)))
;;;###autoload ;;;###autoload
(defun doom-project-name (&optional dir) (defun doom-project-name (&optional dir)
"Return the name of the current project." "Return the name of the current project.
(let ((project-root (or (projectile-project-root dir)
(if dir (expand-file-name dir))))) Returns '-' if not in a valid project."
(if project-root (if-let* ((project-root (or (doom-project-root dir)
(funcall projectile-project-name-function project-root) (if dir (expand-file-name dir)))))
"-"))) (funcall projectile-project-name-function project-root)
"-"))
;;;###autoload ;;;###autoload
(defun doom-project-expand (name &optional dir) (defun doom-project-expand (name &optional dir)
"Expand NAME to project root." "Expand NAME to project root."
(expand-file-name name (projectile-project-root dir))) (expand-file-name name (doom-project-root dir)))
;;;###autoload ;;;###autoload
(defun doom-project-find-file (dir) (defun doom-project-find-file (dir)
@ -82,10 +92,10 @@ Will resolve to the nearest project root above DIR. If no project can be found,
the search will be rooted from DIR." the search will be rooted from DIR."
(unless (file-directory-p dir) (unless (file-directory-p dir)
(error "Directory %S does not exist" dir)) (error "Directory %S does not exist" dir))
(let* ((default-directory (file-truename (expand-file-name dir))) (let ((default-directory (file-truename (expand-file-name dir)))
(projectile-project-root (projectile-project-root
(or (projectile-project-root) (or (doom-project-root dir)
default-directory))) default-directory)))
(call-interactively (call-interactively
;; Intentionally avoid `helm-projectile-find-file', because it runs ;; Intentionally avoid `helm-projectile-find-file', because it runs
;; asynchronously, and thus doesn't see the lexical `default-directory' ;; asynchronously, and thus doesn't see the lexical `default-directory'