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." If no project is active, return all buffers."
(let ((buffers (doom-buffer-list))) (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 (cl-loop for buf in buffers
if (projectile-project-buffer-p buf project-root) if (projectile-project-buffer-p buf project-root)
collect buf) 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-add-file new-path))
(recentf-remove-if-non-kept old-path)) (recentf-remove-if-non-kept old-path))
(when (and (bound-and-true-p projectile-mode) (when (and (bound-and-true-p projectile-mode)
(projectile-project-p) (doom-project-p)
(projectile-file-cached-p old-path (doom-project-root 'nocache))) (projectile-file-cached-p old-path (doom-project-root)))
(projectile-purge-file-from-cache old-path)) (projectile-purge-file-from-cache old-path))
(when (bound-and-true-p save-place-mode) (when (bound-and-true-p save-place-mode)
(save-place-forget-unreadable-files))) (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)))) (list new-path))))
(new-path-dir (file-name-directory new-path)) (new-path-dir (file-name-directory new-path))
(project-root (doom-project-root)) (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) (file-relative-name new-path project-root)
(abbreviate-file-name new-path)))) (abbreviate-file-name new-path))))
(unless (file-directory-p new-path-dir) (unless (file-directory-p new-path-dir)

View file

@ -38,33 +38,23 @@ they are absolute."
;; Library ;; Library
;;;###autoload ;;;###autoload
(defun doom-project-p (&optional nocache) (defalias 'doom-project-p #'projectile-project-p)
"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))))
;;;###autoload ;;;###autoload
(defun doom-project-name (&optional nocache) (defalias 'doom-project-root #'projectile-project-root)
"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)))
;;;###autoload ;;;###autoload
(defun doom-project-root (&optional nocache) (defun doom-project-name (&optional dir)
"Returns the root of your project, or `default-directory' if none was found. "Return the name of the current project."
If NOCACHE, don't fetch a cached answer." (let ((project-root (projectile-project-root dir)))
(if nocache (if project-root
(without-project-cache! (doom-project-root nil)) (funcall projectile-project-name-function project-root)
(let (projectile-require-project-root) "-")))
(projectile-project-root))))
;;;###autoload ;;;###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 ;;;###autoload
(defun doom-project-find-file (dir) (defun doom-project-find-file (dir)

View file

@ -45,30 +45,14 @@
projectile-project-root-files) projectile-project-root-files)
projectile-project-root-files-bottom-up nil))) 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 ;; Projectile root-searching functions can cause an infinite loop on TRAMP
;; connections, so disable them. ;; connections, so disable them.
;; TODO Is this still necessary?
(defun doom*projectile-locate-dominating-file (orig-fn file name) (defun doom*projectile-locate-dominating-file (orig-fn file name)
"Don't traverse the file system if on a remote connection." "Don't traverse the file system if on a remote connection."
(unless (file-remote-p default-directory) (unless (file-remote-p default-directory)
(funcall orig-fn file name))) (funcall orig-fn file name)))
(advice-add #'projectile-locate-dominating-file :around #'doom*projectile-locate-dominating-file) (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))
;; ;;

View file

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

View file

@ -13,7 +13,7 @@
(interactive) (interactive)
(call-interactively (call-interactively
(if (or (file-equal-p default-directory "~") (if (or (file-equal-p default-directory "~")
(when-let* ((proot (doom-project-root 'nocache))) (when-let* ((proot (doom-project-root)))
(file-equal-p proot "~"))) (file-equal-p proot "~")))
#'helm-find-files #'helm-find-files
#'helm-projectile-find-file))) #'helm-projectile-find-file)))

View file

@ -217,11 +217,11 @@ The point of this is to avoid Emacs locking up indexing massive file trees."
(interactive) (interactive)
(call-interactively (call-interactively
(cond ((or (file-equal-p default-directory "~") (cond ((or (file-equal-p default-directory "~")
(when-let* ((proot (doom-project-root 'nocache))) (when-let* ((proot (doom-project-root)))
(file-equal-p proot "~"))) (file-equal-p proot "~")))
#'counsel-find-file) #'counsel-find-file)
((doom-project-p 'nocache) ((doom-project-p)
(let ((files (projectile-current-project-files))) (let ((files (projectile-current-project-files)))
(if (<= (length files) ivy-sort-max-size) (if (<= (length files) ivy-sort-max-size)
#'counsel-projectile-find-file #'counsel-projectile-find-file

View file

@ -3,9 +3,7 @@
;;;###autoload ;;;###autoload
(defun eshell/cd-to-project () (defun eshell/cd-to-project ()
"Change to the project root of the current directory." "Change to the project root of the current directory."
(let* ((default-directory (eshell/pwd)) (eshell/cd (doom-project-root (eshell/pwd))))
(project-root (doom-project-root 'nocache)))
(eshell/cd project-root)))
;;;###autoload ;;;###autoload
(defun eshell/quit-and-close (&rest _) (defun eshell/quit-and-close (&rest _)

View file

@ -7,7 +7,7 @@ non-nil, cd into the current project's root."
(interactive "P") (interactive "P")
(let ((default-directory (let ((default-directory
(if arg (if arg
(doom-project-root 'nocache) (or (doom-project-root) default-directory)
default-directory))) default-directory)))
;; Doom's switch-buffer hooks prevent themselves from triggering when ;; Doom's switch-buffer hooks prevent themselves from triggering when
;; switching from buffer A back to A. Because `multi-term' uses `set-buffer' ;; switching from buffer A back to A. Because `multi-term' uses `set-buffer'
@ -22,7 +22,7 @@ non-nil, cd into the current project's root."
(interactive "P") (interactive "P")
(let ((default-directory (let ((default-directory
(if arg (if arg
(doom-project-root 'nocache) (or (doom-project-root) default-directory)
default-directory))) default-directory)))
(pop-to-buffer (save-window-excursion (multi-term))))) (pop-to-buffer (save-window-excursion (multi-term)))))

View file

@ -11,7 +11,7 @@
(unless (buffer-live-p buffer) (unless (buffer-live-p buffer)
(remhash key +eval-repl-buffers))) (remhash key +eval-repl-buffers)))
+eval-repl-buffers) +eval-repl-buffers)
(let* ((project-root (doom-project-root 'nocache)) (let* ((project-root (doom-project-root))
(key (cons major-mode project-root)) (key (cons major-mode project-root))
(buffer (gethash key +eval-repl-buffers))) (buffer (gethash key +eval-repl-buffers)))
(cl-check-type buffer (or buffer null)) (cl-check-type buffer (or buffer null))

View file

@ -288,7 +288,7 @@ Otherwise, falls back on `find-file-at-point'."
(when (and buffer-file-name (file-equal-p fullpath buffer-file-name)) (when (and buffer-file-name (file-equal-p fullpath buffer-file-name))
(user-error "Already here")) (user-error "Already here"))
(let* ((insert-default-directory t) (let* ((insert-default-directory t)
(project-root (doom-project-root 'nocache)) (project-root (doom-project-root))
(ffap-file-finder (ffap-file-finder
(cond ((not (file-directory-p fullpath)) (cond ((not (file-directory-p fullpath))
#'find-file) #'find-file)

View file

@ -104,7 +104,7 @@ preceded by the opening brace or a comma (disregarding whitespace in between)."
(executable-find "rc")) (executable-find "rc"))
(with-temp-buffer (with-temp-buffer
(message "Reloaded compile commands for rtags daemon") (message "Reloaded compile commands for rtags daemon")
(rtags-call-rc :silent t "-J" (doom-project-root)))) (rtags-call-rc :silent t "-J" (or (doom-project-root) default-directory))))
;; then irony ;; then irony
(when (and (featurep 'irony) irony-mode) (when (and (featurep 'irony) irony-mode)
(+cc|irony-init-compile-options))) (+cc|irony-init-compile-options)))

View file

@ -82,7 +82,7 @@ you're done. This can be called from an external shell script."
filename filename
(or (locate-dominating-file (file-truename default-directory) (or (locate-dominating-file (file-truename default-directory)
filename) filename)
(if (doom-project-p 'nocache) (doom-project-root 'nocache)) (doom-project-root)
(user-error "Couldn't detect a project"))))) (user-error "Couldn't detect a project")))))
;;;###autoload ;;;###autoload

View file

@ -49,7 +49,8 @@
(setq phpactor-working-dir (setq phpactor-working-dir
(or phpactor-working-dir (or phpactor-working-dir
(php-project-get-root-dir) (php-project-get-root-dir)
(doom-project-root)))) (doom-project-root)
default-directory)))
(advice-add #'phpactor-get-working-dir :before #'+php*project-root) (advice-add #'phpactor-get-working-dir :before #'+php*project-root)
(map! :map php-mode-map (map! :map php-mode-map

View file

@ -38,7 +38,7 @@ started it."
(puthash proot (puthash proot
(+python--extract-version "Pipenv " v) (+python--extract-version "Pipenv " v)
+python-version-cache)) +python-version-cache))
(puthash (doom-project-root) (puthash (or (doom-project-root) default-directory)
(+python--extract-version "Python " (car (process-lines "python" "--version"))) (+python--extract-version "Python " (car (process-lines "python" "--version")))
+python-version-cache)) +python-version-cache))
(error "Python"))) (error "Python")))
@ -53,7 +53,8 @@ started it."
(setq +python--version (setq +python--version
(or (gethash (or (and (fboundp 'pipenv-project-p) (or (gethash (or (and (fboundp 'pipenv-project-p)
(pipenv-project-p)) (pipenv-project-p))
(doom-project-root)) (doom-project-root)
default-directory)
+python-version-cache) +python-version-cache)
(+python-version)))) (+python-version))))

View file

@ -27,7 +27,7 @@ pipenv, unless those tools have modified the PATH that Emacs picked up when you
started it." started it."
(condition-case _ (condition-case _
(let ((version-str (car (process-lines "ruby" "--version")))) (let ((version-str (car (process-lines "ruby" "--version"))))
(puthash (doom-project-root) (puthash (or (doom-project-root) default-directory)
(format "Ruby %s" (cadr (split-string version-str " "))) (format "Ruby %s" (cadr (split-string version-str " ")))
+ruby-version-cache)) +ruby-version-cache))
(error "Ruby"))) (error "Ruby")))
@ -40,7 +40,8 @@ started it."
(defun +ruby|update-version (&rest _) (defun +ruby|update-version (&rest _)
"Update `+ruby--version' by consulting `+ruby-version' function." "Update `+ruby--version' by consulting `+ruby-version' function."
(setq +ruby--version (setq +ruby--version
(or (gethash (doom-project-root) +python-version-cache) (or (gethash (or (doom-project-root) default-directory)
+python-version-cache)
(+ruby-version)))) (+ruby-version))))
;;;###autoload ;;;###autoload

View file

@ -31,7 +31,8 @@
(+macos!open-with reveal-in-finder "Finder" default-directory) (+macos!open-with reveal-in-finder "Finder" default-directory)
;;;###autoload (autoload '+macos/reveal-project-in-finder "tools/macos/autoload" nil t) ;;;###autoload (autoload '+macos/reveal-project-in-finder "tools/macos/autoload" nil t)
(+macos!open-with reveal-project-in-finder "Finder" (doom-project-root)) (+macos!open-with reveal-project-in-finder "Finder"
(or (doom-project-root) default-directory))
;;;###autoload (autoload '+macos/send-to-transmit "tools/macos/autoload" nil t) ;;;###autoload (autoload '+macos/send-to-transmit "tools/macos/autoload" nil t)
(+macos!open-with send-to-transmit "Transmit") (+macos!open-with send-to-transmit "Transmit")
@ -43,4 +44,5 @@
(+macos!open-with send-to-launchbar "LaunchBar") (+macos!open-with send-to-launchbar "LaunchBar")
;;;###autoload (autoload '+macos/send-project-to-launchbar "tools/macos/autoload" nil t) ;;;###autoload (autoload '+macos/send-project-to-launchbar "tools/macos/autoload" nil t)
(+macos!open-with send-project-to-launchbar "LaunchBar" (doom-project-root)) (+macos!open-with send-project-to-launchbar "LaunchBar"
(or (doom-project-root) default-directory))

View file

@ -7,7 +7,7 @@
"Run a make task in the current project. If multiple makefiles are available, "Run a make task in the current project. If multiple makefiles are available,
you'll be prompted to select one." you'll be prompted to select one."
(interactive) (interactive)
(if (doom-project-p 'nocache) (if (doom-project-p)
(makefile-executor-execute-project-target) (makefile-executor-execute-project-target)
(let ((makefile (cl-loop with buffer-file = (or buffer-file-name default-directory) (let ((makefile (cl-loop with buffer-file = (or buffer-file-name default-directory)
for file in (list "Makefile" "makefile") for file in (list "Makefile" "makefile")

View file

@ -44,7 +44,7 @@
(defun +prodigy*services (orig-fn &rest args) (defun +prodigy*services (orig-fn &rest args)
"Adds a new :project property to prodigy services, which hides the service "Adds a new :project property to prodigy services, which hides the service
unless invoked from the relevant project." unless invoked from the relevant project."
(let ((project-root (downcase (doom-project-root))) (let ((project-root (downcase (or (doom-project-root) default-directory)))
(services (apply orig-fn args))) (services (apply orig-fn args)))
(if current-prefix-arg (if current-prefix-arg
services services

View file

@ -314,7 +314,7 @@ e.g. ~/w/project/src/lib/file.c
Meant for `+modeline-buffer-path-function'." Meant for `+modeline-buffer-path-function'."
(pcase-let (pcase-let
((`(,root-parent ,root ,dir, file) ((`(,root-parent ,root ,dir, file)
(shrink-path-file-mixed (doom-project-root) (shrink-path-file-mixed (or (doom-project-root) default-directory)
(file-name-directory buffer-file-name) (file-name-directory buffer-file-name)
buffer-file-name))) buffer-file-name)))
(list (cons root-parent 'font-lock-comment-face) (list (cons root-parent 'font-lock-comment-face)

View file

@ -8,7 +8,7 @@
(defun +neotree/open () (defun +neotree/open ()
"Open the neotree window in the current project." "Open the neotree window in the current project."
(interactive) (interactive)
(let ((project-root (doom-project-root 'nocache))) (let ((project-root (doom-project-root)))
(require 'neotree) (require 'neotree)
(if (neo-global--window-exists-p) (if (neo-global--window-exists-p)
(neotree-hide) (neotree-hide)
@ -19,7 +19,7 @@
"Open the neotree window in the current project, and find the current file." "Open the neotree window in the current project, and find the current file."
(interactive) (interactive)
(let ((path buffer-file-name) (let ((path buffer-file-name)
(project-root (doom-project-root 'nocache))) (project-root (doom-project-root)))
(require 'neotree) (require 'neotree)
(cond ((and (neo-global--window-exists-p) (cond ((and (neo-global--window-exists-p)
(get-buffer-window neo-buffer-name t)) (get-buffer-window neo-buffer-name t))

View file

@ -10,8 +10,8 @@
(treemacs-do-remove-project-from-workspace project)) (treemacs-do-remove-project-from-workspace project))
(with-current-buffer origin-buffer (with-current-buffer origin-buffer
(treemacs-do-add-project-to-workspace (treemacs-do-add-project-to-workspace
(treemacs--canonical-path (doom-project-root 'nocache)) (treemacs--canonical-path (doom-project-root))
(doom-project-name 'nocache)) (doom-project-name))
(setq treemacs--ready-to-follow t) (setq treemacs--ready-to-follow t)
(when (or treemacs-follow-after-init treemacs-follow-mode) (when (or treemacs-follow-after-init treemacs-follow-mode)
(treemacs--follow))))) (treemacs--follow)))))