projectile icon indicating copy to clipboard operation
projectile copied to clipboard

[Feature] Make it possible to add and switch to a new directory (like "choose a directory" in project.el)

Open deejayem opened this issue 4 years ago • 1 comments

With project.el, project-switch-project behaves similarly to projectile-switch-project. However, in addition to the list of known projects to choose from, there is also an option to choose a directory: image

It would be nice if we could do something similar with Projectile: either in the same way that project.el does, or with a separate command.

Running projectile-discover-projects-in-search-path is close to providing this functionality, but 1) it can clutter up the list of known projects, and 2) doing it that way makes discovering and switching to the project a two step process.

Currently I am doing it this way:

(defun projectile-add-new-project (project-root)
  (interactive (list (read-directory-name "Select project directory: " "~/src/"))) ;; TODO var for default directory
  (projectile-add-known-project project-root)
  (projectile-persp-switch-project project-root)) ;; TODO var for switch command

I don't know if that's the best approach, but I'm happy to submit a PR with a tidied up version if you think it's a good idea.

deejayem avatar Aug 10 '21 17:08 deejayem

Alternatively, something along the lines of what project.el does would also work, e.g.

(defun projectile-switch-project (&optional arg)
  "Switch to a project we have visited before.
Invokes the command referenced by `projectile-switch-project-action' on switch.
With a prefix ARG invokes `projectile-commander' instead of
`projectile-switch-project-action.'"
  (interactive "P")
  (let* ((choose-dir "...")
         (projects (append (projectile-relevant-known-projects) `(,choose-dir))))
    (projectile-completing-read
     "Switch to project: " projects
     :action (lambda (project)
               (projectile-switch-project-by-name
                (if (equal project choose-dir)
                    (read-directory-name "Select directory: " "~/src" nil t) ;; TODO
                  project)
                arg)))))

One downside to this is that it doesn't work with persp-projectile, as projectile-persp-switch-project would need modifying too. I think I prefer the other approach.

deejayem avatar Aug 12 '21 06:08 deejayem