From c6f979b4e097d0b8950ef209a03ba86ab579eba4 Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Tue, 1 Apr 2025 03:14:24 -0400 Subject: [PATCH] refactor(workspaces): projectile integration Simplifies +workspaces-switch-to-project-h and refactors out the dependency on external state. --- modules/ui/workspaces/autoload/workspaces.el | 67 ++++++++------------ modules/ui/workspaces/config.el | 2 +- 2 files changed, 29 insertions(+), 40 deletions(-) diff --git a/modules/ui/workspaces/autoload/workspaces.el b/modules/ui/workspaces/autoload/workspaces.el index 41759958a..faef4507a 100644 --- a/modules/ui/workspaces/autoload/workspaces.el +++ b/modules/ui/workspaces/autoload/workspaces.el @@ -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 (_) diff --git a/modules/ui/workspaces/config.el b/modules/ui/workspaces/config.el index 941786282..a6e022b6a 100644 --- a/modules/ui/workspaces/config.el +++ b/modules/ui/workspaces/config.el @@ -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")