diff options
| author | Matti Niemenmaa <matti.niemenmaa+github@iki.fi> | 2020-12-09 19:08:02 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-12-09 19:08:02 +0200 |
| commit | c7a1d201f7dbf600e7f27eaa21c608a0a85a1abf (patch) | |
| tree | 35ba438a3c5a4da427ab6025a4b38d042c7e959a | |
| parent | 0ff73ecf7f25f47382e2d28c8ab232c18609d515 (diff) | |
[#Fix 1617] Switch to right buffer in projectile-switch-project-by-name (#1621)
| -rw-r--r-- | projectile.el | 35 | ||||
| -rw-r--r-- | test/projectile-test.el | 33 |
2 files changed, 52 insertions, 16 deletions
diff --git a/projectile.el b/projectile.el index dbbad35..cefc4af 100644 --- a/projectile.el +++ b/projectile.el @@ -4392,20 +4392,27 @@ With a prefix ARG invokes `projectile-commander' instead of 'projectile-commander projectile-switch-project-action))) (run-hooks 'projectile-before-switch-project-hook) - (let ((default-directory project-to-switch)) - ;; use a temporary buffer to load PROJECT-TO-SWITCH's dir-locals before calling SWITCH-PROJECT-ACTION - (with-temp-buffer - (hack-dir-local-variables-non-file-buffer) - ;; Normally the project name is determined from the current - ;; buffer. However, when we're switching projects, we want to - ;; show the name of the project being switched to, rather than - ;; the current project, in the minibuffer. This is a simple hack - ;; to tell the `projectile-project-name' function to ignore the - ;; current buffer and the caching mechanism, and just return the - ;; value of the `projectile-project-name' variable. - (let ((projectile-project-name (funcall projectile-project-name-function - project-to-switch))) - (funcall switch-project-action)))) + (let* ((default-directory project-to-switch) + (switched-buffer + ;; use a temporary buffer to load PROJECT-TO-SWITCH's dir-locals + ;; before calling SWITCH-PROJECT-ACTION + (with-temp-buffer + (hack-dir-local-variables-non-file-buffer) + ;; Normally the project name is determined from the current + ;; buffer. However, when we're switching projects, we want to + ;; show the name of the project being switched to, rather than + ;; the current project, in the minibuffer. This is a simple hack + ;; to tell the `projectile-project-name' function to ignore the + ;; current buffer and the caching mechanism, and just return the + ;; value of the `projectile-project-name' variable. + (let ((projectile-project-name (funcall projectile-project-name-function + project-to-switch))) + (funcall switch-project-action) + (current-buffer))))) + ;; If switch-project-action switched buffers then with-temp-buffer will + ;; have lost that change, so switch back to the correct buffer. + (when (buffer-live-p switched-buffer) + (switch-to-buffer switched-buffer))) (run-hooks 'projectile-after-switch-project-hook))) ;;;###autoload diff --git a/test/projectile-test.el b/test/projectile-test.el index 9de80cf..c85b75a 100644 --- a/test/projectile-test.el +++ b/test/projectile-test.el @@ -817,7 +817,7 @@ You'd normally combine this with `projectile-test-with-sandbox'." (expect (projectile-switch-project) :to-throw)))) (describe "projectile-switch-project-by-name" - (it "calls the switch project action with project-to-swtich's dir-locals loaded" + (it "calls the switch project action with project-to-switch's dir-locals loaded" (defvar switch-project-foo) (let ((foo 'bar) (switch-project-foo) @@ -833,7 +833,36 @@ You'd normally combine this with `projectile-test-with-sandbox'." (projectile-add-known-project (file-name-as-directory (expand-file-name "project"))) (projectile-switch-project-by-name (file-name-as-directory (expand-file-name "project"))) - (expect switch-project-foo :to-be 'baz)))))) + (expect switch-project-foo :to-be 'baz))))) + + (it "runs hooks from the project root directory" + (defvar hook-dir) + (let ((projectile-switch-project-action + (lambda () (switch-to-buffer (find-file-noselect "file" t)))) + (hook (lambda () (setq hook-dir default-directory)))) + (add-hook 'projectile-after-switch-project-hook hook) + (projectile-test-with-sandbox + (projectile-test-with-files + ("project/" + "project/file") + (let ((project-dir (file-name-as-directory (expand-file-name "project")))) + (projectile-add-known-project project-dir) + (projectile-switch-project-by-name project-dir) + (remove-hook 'projectile-after-switch-project-hook hook) + + (expect hook-dir :to-equal project-dir)))))) + + (it "ensures the buffer is switched immediately" + (let ((projectile-switch-project-action + (lambda () (switch-to-buffer (find-file-noselect "file" t))))) + (projectile-test-with-sandbox + (projectile-test-with-files + ("project/" + "project/file") + (projectile-add-known-project (file-name-as-directory (expand-file-name "project"))) + (projectile-switch-project-by-name (file-name-as-directory (expand-file-name "project"))) + + (expect (current-buffer) :to-be (get-file-buffer "project/file"))))))) (describe "projectile-ignored-buffer-p" (it "checks if buffer should be ignored" |
