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)) (persp-set-frame-buffer-predicate frame))
(run-at-time 0.1 nil #'+workspace/display)))) (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 ;;;###autoload
(defun +workspaces-switch-to-project-h (&optional dir) (defun +workspaces-switch-to-project-h (&optional dir)
"Creates a workspace dedicated to a new project. If one already exists, switch "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. the user to open a file in the new project.
This be hooked to `projectile-after-switch-project-hook'." This be hooked to `projectile-after-switch-project-hook'."
(when dir (let* ((default-directory (or dir default-directory))
(setq +workspaces--project-dir dir)) (pname (doom-project-name))
;; HACK: Clear projectile-project-root or cached roots could interfere with (proot (file-truename default-directory))
;; project switching (see #3166). ;; HACK: Clear projectile-project-root or cached roots could interfere
(let (projectile-project-root) ;; with project switching (see #3166).
(when (and persp-mode +workspaces--project-dir) projectile-project-root)
(unwind-protect (when persp-mode
(if (and (not (null +workspaces-on-switch-project-behavior)) (if (and (not (null +workspaces-on-switch-project-behavior))
(or (eq +workspaces-on-switch-project-behavior t) (or (eq +workspaces-on-switch-project-behavior t)
(+workspace--protected-p (safe-persp-name (get-current-persp))) (+workspace--protected-p (safe-persp-name (get-current-persp)))
(+workspace-buffer-list))) (+workspace-buffer-list)))
(let* ((persp (let ((persp (or (+workspace-get pname t)
(let ((project-name (doom-project-name +workspaces--project-dir))) (+workspace-new pname))))
(or (+workspace-get project-name t) (+workspace-switch pname)
(+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))
(with-current-buffer (doom-fallback-buffer) (with-current-buffer (doom-fallback-buffer)
(setq default-directory +workspaces--project-dir) (setq-local default-directory proot)
(hack-dir-local-variables-non-file-buffer) (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)))
(unless current-prefix-arg (unless current-prefix-arg
(funcall +workspaces-switch-project-function +workspaces--project-dir))) (funcall +workspaces-switch-project-function proot))
(setq +workspaces--project-dir nil))))) (+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 ;;;###autoload
(defun +workspaces-save-tab-bar-data-h (_) (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 ;; per-project workspaces, but reuse current workspace if empty
;; HACK?? needs review ;; 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 counsel-projectile-switch-project-action
'(1 ("o" +workspaces-switch-to-project-h "open project in new workspace") '(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") ("O" counsel-projectile-switch-project-action "jump to a project buffer or file")