use a different mechanism to switch project
Use the newly defined `counsel-projectile-switch-project-by-name` instead of `projectile-switch-project-by-name`. Fix #48 Fix #39 Fix #32 Fix #24
This commit is contained in:
parent
363c875df6
commit
cc32e2055a
1 changed files with 52 additions and 28 deletions
|
|
@ -278,80 +278,104 @@ hitting \"M-n\" in the minibuffer.")
|
||||||
|
|
||||||
;;; counsel-projectile-switch-project
|
;;; counsel-projectile-switch-project
|
||||||
|
|
||||||
|
(defun counsel-projectile-switch-project-by-name (project-to-switch)
|
||||||
|
"Switch to project by project name PROJECT-TO-SWITCH.
|
||||||
|
Invokes the command referenced by `projectile-switch-project-action' on switch.
|
||||||
|
|
||||||
|
This is a replacement for `projectile-switch-project-by-name'
|
||||||
|
with a different switching mechanism: the switch-project action
|
||||||
|
is called from a dedicated buffer rather than the initial buffer.
|
||||||
|
Also, PROJECT's dir-local variables are loaded before calling the
|
||||||
|
action."
|
||||||
|
(run-hooks 'projectile-before-switch-project-hook)
|
||||||
|
;; Kill and recreate the switch buffer to get rid of any local
|
||||||
|
;; variable
|
||||||
|
(ignore-errors (kill-buffer " *counsel-projectile*"))
|
||||||
|
(set-buffer (get-buffer-create " *counsel-projectile*"))
|
||||||
|
(setq default-directory project-to-switch)
|
||||||
|
;; Load the project dir-local variables into the switch buffer, so
|
||||||
|
;; the action can make use of them
|
||||||
|
(hack-dir-local-variables-non-file-buffer)
|
||||||
|
(funcall projectile-switch-project-action)
|
||||||
|
;; If the action relies on `ivy-read' then, after one of its
|
||||||
|
;; `ivy-read' actions is executed, the current buffer will be set
|
||||||
|
;; back to the initial buffer. Hence we make sure tu evaluate
|
||||||
|
;; `projectile-after-switch-project-hook' from the switch buffer.
|
||||||
|
(with-current-buffer " *counsel-projectile*"
|
||||||
|
(run-hooks 'projectile-after-switch-project-hook)))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun counsel-projectile-switch-project (&optional arg)
|
(defun counsel-projectile-switch-project ()
|
||||||
"Switch to a project we have visited before.
|
"Switch to a project we have visited before.
|
||||||
|
|
||||||
Invokes the command referenced by
|
Invokes the command referenced by
|
||||||
`projectile-switch-project-action' on switch. With a prefix ARG
|
`projectile-switch-project-action' on switch."
|
||||||
invokes `projectile-commander' instead of
|
(interactive)
|
||||||
`projectile-switch-project-action.'"
|
|
||||||
(interactive "P")
|
|
||||||
(ivy-read (projectile-prepend-project-name "Switch to project: ")
|
(ivy-read (projectile-prepend-project-name "Switch to project: ")
|
||||||
projectile-known-projects
|
projectile-known-projects
|
||||||
:preselect (and (projectile-project-p)
|
:preselect (and (projectile-project-p)
|
||||||
(abbreviate-file-name (projectile-project-root)))
|
(abbreviate-file-name (projectile-project-root)))
|
||||||
:action (lambda (dir)
|
:action #'counsel-projectile-switch-project-by-name
|
||||||
(projectile-switch-project-by-name dir arg))
|
|
||||||
:require-match t
|
:require-match t
|
||||||
:caller 'counsel-projectile-switch-project))
|
:caller 'counsel-projectile-switch-project))
|
||||||
|
|
||||||
(ivy-set-actions
|
(ivy-set-actions
|
||||||
'counsel-projectile-switch-project
|
'counsel-projectile-switch-project
|
||||||
'(("f" (lambda (dir)
|
'(("f" (lambda (dir)
|
||||||
(let ((projectile-switch-project-action 'counsel-projectile-find-file))
|
(let ((projectile-switch-project-action
|
||||||
(projectile-switch-project-by-name dir arg)))
|
(lambda ()
|
||||||
|
(counsel-projectile-find-file ivy-current-prefix-arg))))
|
||||||
|
(counsel-projectile-switch-project-by-name dir)))
|
||||||
"find file")
|
"find file")
|
||||||
("F" (lambda (dir)
|
("F" (lambda (dir)
|
||||||
(let ((projectile-switch-project-action
|
(let ((projectile-switch-project-action
|
||||||
(lambda ()
|
(lambda ()
|
||||||
(counsel-find-file dir))))
|
(counsel-find-file dir))))
|
||||||
(projectile-switch-project-by-name dir arg)))
|
(counsel-projectile-switch-project-by-name dir)))
|
||||||
"find file manually")
|
"find file manually")
|
||||||
("d" (lambda (dir)
|
("d" (lambda (dir)
|
||||||
(let ((projectile-switch-project-action 'counsel-projectile-find-dir))
|
(let ((projectile-switch-project-action
|
||||||
(projectile-switch-project-by-name dir arg)))
|
(lambda ()
|
||||||
|
(counsel-projectile-find-dir ivy-current-prefix-arg))))
|
||||||
|
(counsel-projectile-switch-project-by-name dir)))
|
||||||
"find directory")
|
"find directory")
|
||||||
("b" (lambda (dir)
|
("b" (lambda (dir)
|
||||||
(let ((projectile-switch-project-action 'counsel-projectile-switch-to-buffer))
|
(let ((projectile-switch-project-action 'counsel-projectile-switch-to-buffer))
|
||||||
(projectile-switch-project-by-name dir arg)))
|
(counsel-projectile-switch-project-by-name dir)))
|
||||||
"switch to buffer")
|
"switch to buffer")
|
||||||
("s" (lambda (dir)
|
("s" (lambda (dir)
|
||||||
(let ((projectile-switch-project-action 'projectile-save-project-buffers))
|
(let ((projectile-switch-project-action 'projectile-save-project-buffers))
|
||||||
(projectile-switch-project-by-name dir arg)))
|
(counsel-projectile-switch-project-by-name dir)))
|
||||||
"save all buffers")
|
"save all buffers")
|
||||||
("k" (lambda (dir)
|
("k" (lambda (dir)
|
||||||
(let ((projectile-switch-project-action 'projectile-kill-buffers))
|
(let ((projectile-switch-project-action 'projectile-kill-buffers))
|
||||||
(projectile-switch-project-by-name dir arg)))
|
(counsel-projectile-switch-project-by-name dir)))
|
||||||
"kill all buffers")
|
"kill all buffers")
|
||||||
("r" (lambda (dir)
|
("r" (lambda (dir)
|
||||||
(let ((projectile-switch-project-action
|
|
||||||
(lambda ()
|
|
||||||
(projectile-remove-known-project dir)
|
(projectile-remove-known-project dir)
|
||||||
(setq ivy--all-candidates
|
(setq ivy--all-candidates
|
||||||
(delete dir ivy--all-candidates))
|
(delete dir ivy--all-candidates))
|
||||||
(ivy--reset-state ivy-last))))
|
(ivy--reset-state ivy-last))
|
||||||
(projectile-switch-project-by-name dir arg)))
|
|
||||||
"remove from known projects")
|
"remove from known projects")
|
||||||
("l" (lambda (dir)
|
("l" (lambda (dir)
|
||||||
(let ((projectile-switch-project-action 'projectile-edit-dir-locals))
|
(let ((projectile-switch-project-action 'projectile-edit-dir-locals))
|
||||||
(projectile-switch-project-by-name dir arg)))
|
(counsel-projectile-switch-project-by-name dir)))
|
||||||
"edit dir-locals")
|
"edit dir-locals")
|
||||||
("g" (lambda (dir)
|
("g" (lambda (dir)
|
||||||
(let ((projectile-switch-project-action 'projectile-vc))
|
(let ((projectile-switch-project-action 'projectile-vc))
|
||||||
(projectile-switch-project-by-name dir arg)))
|
(counsel-projectile-switch-project-by-name dir)))
|
||||||
"open in vc-dir / magit / monky")
|
"open in vc-dir / magit / monky")
|
||||||
("e" (lambda (dir)
|
("e" (lambda (dir)
|
||||||
(let ((projectile-switch-project-action 'projectile-run-eshell))
|
(let ((projectile-switch-project-action 'projectile-run-eshell))
|
||||||
(projectile-switch-project-by-name dir arg)))
|
(counsel-projectile-switch-project-by-name dir)))
|
||||||
"start eshell")
|
"start eshell")
|
||||||
("a" (lambda (dir)
|
("a" (lambda (dir)
|
||||||
(let ((projectile-switch-project-action 'counsel-projectile-ag))
|
(let ((projectile-switch-project-action 'counsel-projectile-ag))
|
||||||
(projectile-switch-project-by-name dir arg)))
|
(counsel-projectile-switch-project-by-name dir)))
|
||||||
"search with ag")
|
"search with ag")
|
||||||
("R" (lambda (dir)
|
("R" (lambda (dir)
|
||||||
(let ((projectile-switch-project-action 'counsel-projectile-rg))
|
(let ((projectile-switch-project-action 'counsel-projectile-rg))
|
||||||
(projectile-switch-project-by-name dir arg)))
|
(counsel-projectile-switch-project-by-name dir)))
|
||||||
"search with rg")))
|
"search with rg")))
|
||||||
|
|
||||||
;;; counsel-projectile
|
;;; counsel-projectile
|
||||||
|
|
@ -422,7 +446,7 @@ Relies on `ivy--switch-buffer-matcher` and
|
||||||
With a prefix ARG invalidates the cache first."
|
With a prefix ARG invalidates the cache first."
|
||||||
(interactive "P")
|
(interactive "P")
|
||||||
(if (not (projectile-project-p))
|
(if (not (projectile-project-p))
|
||||||
(counsel-projectile-switch-project arg)
|
(counsel-projectile-switch-project)
|
||||||
(projectile-maybe-invalidate-cache arg)
|
(projectile-maybe-invalidate-cache arg)
|
||||||
(ivy-read (projectile-prepend-project-name "Load buffer or file: ")
|
(ivy-read (projectile-prepend-project-name "Load buffer or file: ")
|
||||||
(counsel-projectile--global-list)
|
(counsel-projectile--global-list)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue