Skip to content

Commit

Permalink
Add #'projectile-track-known-projects-find-file-hook to 'buffer-list-…
Browse files Browse the repository at this point in the history
…update-hook
  • Loading branch information
jtamagnan committed Jun 28, 2024
1 parent 0163b33 commit f0bedae
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
* [#1895](https://github.com/bbatsov/projectile/pull/1895): 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
Expand Down
11 changes: 8 additions & 3 deletions projectile.el
Original file line number Diff line number Diff line change
Expand Up @@ -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 t)
(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))))

Expand Down
34 changes: 34 additions & 0 deletions test/projectile-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -1949,6 +1949,40 @@ 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)))))

(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")
(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)
;; Sadly this behavior is contigent on the existance of
;; `buffer-list-update-hook' so it will behave
;; differently on older versions of Emacs that do not
;; have this variable.
:to-equal (if (version<= "28.1" emacs-version)
'("project1" "project2")
'("project2" "project1")))))))

(describe "projectile--impl-name-for-test-name"
:var ((mock-projectile-project-types
'((foo test-suffix "Test")
Expand Down

0 comments on commit f0bedae

Please sign in to comment.