Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add #'projectile-track-known-projects-find-file-hook to 'buffer-list-update-hook #1895

Merged
merged 1 commit into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* [#1877](https://github.com/bbatsov/projectile/pull/1877): Add custom variable `projectile-cmd-hist-ignoredups`.
* Add support for Eask projects.
* [#1892](https://github.com/bbatsov/projectile/pull/1892): Add category metadata to `completing-read`. (it's used by packages like `marginalia` and `embark`)
* [#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.

### Bugs fixed

Expand Down
16 changes: 13 additions & 3 deletions projectile.el
Original file line number Diff line number Diff line change
Expand Up @@ -6284,14 +6284,24 @@ 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)
;; Add hooks to track which buffer is currently active.
;; Note - In Emacs 28.1 `buffer-list-update-hook' was modified to no
;; longer run on temporary buffers, this allows us to use it to track
;; changes to the active buffer instead of relying on more specific hooks
;; such as `dired-before-readin-hook'.
(if (version<= "28.1" emacs-version)
jtamagnan marked this conversation as resolved.
Show resolved Hide resolved
(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 @@ -1957,6 +1957,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
Loading