diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ae0f800f..0583835dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ * [#1875](https://github.com/bbatsov/projectile/pull/1875): Add support for Sapling VCS. * [#1876](https://github.com/bbatsov/projectile/pull/1876): Add support for Jujutsu VCS. * [#1877](https://github.com/bbatsov/projectile/pull/1877): Add custom variable `projectile-cmd-hist-ignoredups`. +* [#xxxx](https://github.com/bbatsov/projectile/pull/xxxx): Modify projectile-mode to add a hook to `buffer-list-update-hook` such that any change in the buffer list will update the selected project. * Add support for Eask projects. ### Bugs fixed diff --git a/projectile.el b/projectile.el index d89ecc45c..97f89e208 100644 --- a/projectile.el +++ b/projectile.el @@ -6272,14 +6272,19 @@ Otherwise behave as if called interactively. (projectile-discover-projects-in-search-path)) (add-hook 'project-find-functions #'project-projectile) (add-hook 'find-file-hook 'projectile-find-file-hook-function) - (add-hook 'projectile-find-dir-hook #'projectile-track-known-projects-find-file-hook t) - (add-hook 'dired-before-readin-hook #'projectile-track-known-projects-find-file-hook t t) + (if (version<= "28.1" emacs-version) + (add-hook 'buffer-list-update-hook 'projectile-track-known-projects-find-file-hook) + (add-hook 'projectile-find-dir-hook #'projectile-track-known-projects-find-file-hook t) + (add-hook 'dired-before-readin-hook #'projectile-track-known-projects-find-file-hook t t)) + (advice-add 'compilation-find-file :around #'compilation-find-file-projectile-find-compilation-buffer) (advice-add 'delete-file :before #'delete-file-projectile-remove-from-cache)) (t (remove-hook 'project-find-functions #'project-projectile) (remove-hook 'find-file-hook #'projectile-find-file-hook-function) - (remove-hook 'dired-before-readin-hook #'projectile-track-known-projects-find-file-hook t) + (if (version<= "28.1" emacs-version) + (remove-hook 'buffer-list-update-hook 'projectile-track-known-projects-find-file-hook) + (remove-hook 'dired-before-readin-hook #'projectile-track-known-projects-find-file-hook t)) (advice-remove 'compilation-find-file #'compilation-find-file-projectile-find-compilation-buffer) (advice-remove 'delete-file #'delete-file-projectile-remove-from-cache)))) diff --git a/test/projectile-test.el b/test/projectile-test.el index 7a2f10c03..2ff3ea49b 100644 --- a/test/projectile-test.el +++ b/test/projectile-test.el @@ -1949,6 +1949,36 @@ projectile-process-current-project-buffers-current to have similar behaviour" (with-current-buffer (find-file-noselect "foo" t)) (expect (length (projectile-project-buffers)) :to-equal 1))))) +(when (version<= "28.1" emacs-version) + (describe "projectile-mode buffer-list-update-hook" + (it "check that the hooks properly update the known projects when changing files or windows" + (projectile-test-with-sandbox + (projectile-test-with-files + ("project1/" + "project1/.projectile" + "project1/foo" + "project2/" + "project2/.projectile" + "project2/bar") + (find-file "project1/") + (projectile-mode 1) + + ;; Check that opening a file leads to the known-projects being updated + (find-file "project1/foo") + (expect (mapcar (lambda (x) (file-name-base (directory-file-name x))) projectile-known-projects) + :to-equal '("project1")) + + ;; Check that opening a file in a different project leads to the known-projects being updated + (find-file-other-window "../../project2/bar") + (pp (buffer-file-name)) + (expect (mapcar (lambda (x) (file-name-base (directory-file-name x))) projectile-known-projects) + :to-equal '("project2" "project1")) + + ;; Check that selecting a different buffer also updates the known-projects + (other-window 1) + (expect (mapcar (lambda (x) (file-name-base (directory-file-name x))) projectile-known-projects) + :to-equal '("project1" "project2"))))))) + (describe "projectile--impl-name-for-test-name" :var ((mock-projectile-project-types '((foo test-suffix "Test")