refactor(workspaces): projectile integration

Simplifies +workspaces-switch-to-project-h and refactors out the
dependency on external state.
This commit is contained in:
Henrik Lissner
2025-04-01 03:14:24 -04:00
parent 7941dfa766
commit c6f979b4e0
2 changed files with 29 additions and 40 deletions

View File

@ -543,13 +543,6 @@ created."
(persp-set-frame-buffer-predicate frame))
(run-at-time 0.1 nil #'+workspace/display))))
(defvar +workspaces--project-dir nil)
;;;###autoload
(defun +workspaces-set-project-action-fn ()
"A `projectile-switch-project-action' that sets the project directory for
`+workspaces-switch-to-project-h'."
(setq +workspaces--project-dir default-directory))
;;;###autoload
(defun +workspaces-switch-to-project-h (&optional dir)
"Creates a workspace dedicated to a new project. If one already exists, switch
@ -560,40 +553,36 @@ Afterwords, runs `+workspaces-switch-project-function'. By default, this prompts
the user to open a file in the new project.
This be hooked to `projectile-after-switch-project-hook'."
(when dir
(setq +workspaces--project-dir dir))
;; HACK: Clear projectile-project-root or cached roots could interfere with
;; project switching (see #3166).
(let (projectile-project-root)
(when (and persp-mode +workspaces--project-dir)
(unwind-protect
(if (and (not (null +workspaces-on-switch-project-behavior))
(or (eq +workspaces-on-switch-project-behavior t)
(+workspace--protected-p (safe-persp-name (get-current-persp)))
(+workspace-buffer-list)))
(let* ((persp
(let ((project-name (doom-project-name +workspaces--project-dir)))
(or (+workspace-get project-name t)
(+workspace-new project-name))))
(new-name (persp-name persp)))
(+workspace-switch new-name)
(with-current-buffer (doom-fallback-buffer)
(setq default-directory +workspaces--project-dir)
(hack-dir-local-variables-non-file-buffer))
(unless current-prefix-arg
(funcall +workspaces-switch-project-function +workspaces--project-dir))
(+workspace-message
(format "Switched to '%s' in new workspace" new-name)
'success))
(let* ((default-directory (or dir default-directory))
(pname (doom-project-name))
(proot (file-truename default-directory))
;; HACK: Clear projectile-project-root or cached roots could interfere
;; with project switching (see #3166).
projectile-project-root)
(when persp-mode
(if (and (not (null +workspaces-on-switch-project-behavior))
(or (eq +workspaces-on-switch-project-behavior t)
(+workspace--protected-p (safe-persp-name (get-current-persp)))
(+workspace-buffer-list)))
(let ((persp (or (+workspace-get pname t)
(+workspace-new pname))))
(+workspace-switch pname)
(with-current-buffer (doom-fallback-buffer)
(setq default-directory +workspaces--project-dir)
(hack-dir-local-variables-non-file-buffer)
(message "Switched to '%s'" (doom-project-name +workspaces--project-dir)))
(with-demoted-errors "Workspace error: %s"
(+workspace-rename (+workspace-current-name) (doom-project-name +workspaces--project-dir)))
(setq-local default-directory proot)
(hack-dir-local-variables-non-file-buffer))
(unless current-prefix-arg
(funcall +workspaces-switch-project-function +workspaces--project-dir)))
(setq +workspaces--project-dir nil)))))
(funcall +workspaces-switch-project-function proot))
(+workspace-message
(format "Switched to '%s' in new workspace" pname)
'success))
(with-current-buffer (doom-fallback-buffer)
(setq-local default-directory proot)
(hack-dir-local-variables-non-file-buffer)
(message "Switched to '%s'" pname))
(with-demoted-errors "Workspace error: %s"
(+workspace-rename (+workspace-current-name) pname))
(unless current-prefix-arg
(funcall +workspaces-switch-project-function proot))))))
;;;###autoload
(defun +workspaces-save-tab-bar-data-h (_)

View File

@ -178,7 +178,7 @@ stored in `persp-save-dir'.")
;; per-project workspaces, but reuse current workspace if empty
;; HACK?? needs review
(setq projectile-switch-project-action (lambda () (+workspaces-set-project-action-fn) (+workspaces-switch-to-project-h))
(setq projectile-switch-project-action #'+workspaces-switch-to-project-h
counsel-projectile-switch-project-action
'(1 ("o" +workspaces-switch-to-project-h "open project in new workspace")
("O" counsel-projectile-switch-project-action "jump to a project buffer or file")