Refactor Project API to reflect changes upstream

projectile-project-root no longer returns `default-directory` if not in
a project (it returns nil). As such, doom-project-* functions (and their
uses) have been refactored.

+ doom-project-p & doom-project-root are aliases for
  projectile-project-p & projectile-project-root.
+ doom-project-{p,root,name,expand} now has a DIR argument (for
  consistency, since projectile-project-name and
  projectile-project-expand do not).
+ The nocache parameter is no longer necessary, as projectile's caching
  behavior is now more sane.
+ Removed some projectile advice/hacks that are no longer necessary.
+ Updated unit tests
This commit is contained in:
Henrik Lissner 2018-09-28 13:54:20 -04:00
parent 3961ba1662
commit 53fe7a1f04
22 changed files with 53 additions and 84 deletions

View file

@ -65,7 +65,7 @@ scratch buffer. See `doom-fallback-buffer-name' to change this."
If no project is active, return all buffers."
(let ((buffers (doom-buffer-list)))
(if-let* ((project-root (if (doom-project-p) (doom-project-root))))
(if-let* ((project-root (doom-project-root)))
(cl-loop for buf in buffers
if (projectile-project-buffer-p buf project-root)
collect buf)

View file

@ -88,8 +88,8 @@ MATCH is a string regexp. Only entries that match it will be included."
(recentf-add-file new-path))
(recentf-remove-if-non-kept old-path))
(when (and (bound-and-true-p projectile-mode)
(projectile-project-p)
(projectile-file-cached-p old-path (doom-project-root 'nocache)))
(doom-project-p)
(projectile-file-cached-p old-path (doom-project-root)))
(projectile-purge-file-from-cache old-path))
(when (bound-and-true-p save-place-mode)
(save-place-forget-unreadable-files)))
@ -111,7 +111,7 @@ MATCH is a string regexp. Only entries that match it will be included."
(list new-path))))
(new-path-dir (file-name-directory new-path))
(project-root (doom-project-root))
(short-new-name (if (file-in-directory-p new-path project-root)
(short-new-name (if (and project-root (file-in-directory-p new-path project-root))
(file-relative-name new-path project-root)
(abbreviate-file-name new-path))))
(unless (file-directory-p new-path-dir)

View file

@ -38,33 +38,23 @@ they are absolute."
;; Library
;;;###autoload
(defun doom-project-p (&optional nocache)
"Return t if this buffer is currently in a project.
If NOCACHE, don't fetch a cached answer."
(if nocache
(without-project-cache! (doom-project-p nil))
(let ((projectile-require-project-root t))
(and (projectile-project-p) t))))
(defalias 'doom-project-p #'projectile-project-p)
;;;###autoload
(defun doom-project-name (&optional nocache)
"Return the name of the current project.
If NOCACHE, don't fetch a cached answer."
(if nocache
(without-project-cache! (doom-project-name nil))
(projectile-project-name)))
(defalias 'doom-project-root #'projectile-project-root)
;;;###autoload
(defun doom-project-root (&optional nocache)
"Returns the root of your project, or `default-directory' if none was found.
If NOCACHE, don't fetch a cached answer."
(if nocache
(without-project-cache! (doom-project-root nil))
(let (projectile-require-project-root)
(projectile-project-root))))
(defun doom-project-name (&optional dir)
"Return the name of the current project."
(let ((project-root (projectile-project-root dir)))
(if project-root
(funcall projectile-project-name-function project-root)
"-")))
;;;###autoload
(defalias 'doom-project-expand #'projectile-expand-root)
(defun doom-project-expand (name &optional dir)
"Expand NAME to project root."
(expand-file-name name (projectile-project-root dir)))
;;;###autoload
(defun doom-project-find-file (dir)

View file

@ -45,30 +45,14 @@
projectile-project-root-files)
projectile-project-root-files-bottom-up nil)))
;; Restores the old behavior of `projectile-project-root', where it returns
;; `default-directory' if not in a project, at least until I update Doom to
;; deal with this.
;; FIXME Check again after https://github.com/bbatsov/projectile/issues/1296
(defun doom*projectile-project-root (project-root)
(or project-root
default-directory))
(advice-add #'projectile-project-root :filter-return #'doom*projectile-project-root)
;; Projectile root-searching functions can cause an infinite loop on TRAMP
;; connections, so disable them.
;; TODO Is this still necessary?
(defun doom*projectile-locate-dominating-file (orig-fn file name)
"Don't traverse the file system if on a remote connection."
(unless (file-remote-p default-directory)
(funcall orig-fn file name)))
(advice-add #'projectile-locate-dominating-file :around #'doom*projectile-locate-dominating-file)
(defun doom*projectile-cache-current-file (orig-fn)
"Don't cache ignored files."
(unless (cl-loop for path in (projectile-ignored-directories)
if (string-prefix-p (or buffer-file-name "") (expand-file-name path))
return t)
(funcall orig-fn)))
(advice-add #'projectile-cache-current-file :around #'doom*projectile-cache-current-file))
(advice-add #'projectile-locate-dominating-file :around #'doom*projectile-locate-dominating-file))
;;

View file

@ -10,29 +10,21 @@
(describe "project-p"
(it "Should detect when in a valid project"
(let ((buffer-file-name (expand-file-name "init.el" doom-emacs-dir))
(default-directory doom-emacs-dir))
(expect (doom-project-p))))
(expect (doom-project-p doom-emacs-dir)))
(it "Should detect when not in a valid project"
(let ((buffer-file-name (expand-file-name "test" "~"))
(default-directory (expand-file-name "~")))
(expect (doom-project-p) :to-be nil))))
(expect (doom-project-p (expand-file-name "~")) :to-be nil)))
(describe "project-root"
(it "should resolve to the project's root"
(let ((buffer-file-name (expand-file-name "core.el" doom-core-dir))
(default-directory doom-core-dir))
(expect (doom-project-root) :to-equal doom-emacs-dir)))
(expect (doom-project-root doom-core-dir) :to-equal doom-emacs-dir))
(it "should resolve to the `default-directory'"
(let ((buffer-file-name (expand-file-name "test" "/"))
(default-directory (expand-file-name "/")))
(expect (doom-project-root) :to-equal default-directory))))
(expect (doom-project-root (expand-file-name "~"))
:to-equal (expand-file-name "~"))))
(describe "project-expand"
(it "expands to a path relative to the project root"
(let ((default-directory doom-core-dir))
(expect (doom-project-expand "init.el")
:to-equal (expand-file-name "init.el" (doom-project-root))))))
(expect (doom-project-expand "init.el" doom-core-dir)
:to-equal (expand-file-name "init.el" (doom-project-root doom-core-dir)))))
(describe "project-file-exists-p!"
(let ((default-directory doom-core-dir))