Alexander-Miller / treemacs

GNU General Public License v3.0
2.12k stars 155 forks source link

Open the nearest parent directory in a new workspace. #970

Closed simurgh9 closed 2 years ago

simurgh9 commented 2 years ago

I wanted a command that will open the nearest parent directory in a new workspace. I wrote,

(defun open-in-new-workspace ()
  (interactive)
  (let* ((path default-directory)
         (name (file-name-directory path))
         (name (directory-file-name name))
         (name (file-name-nondirectory name)))
    (treemacs-do-create-workspace name)
    (treemacs-with-workspace
      (treemacs-find-workspace-by-name name)
      (treemacs-add-project-to-workspace path))
    (treemacs-do-switch-workspace name)))

It works. But, I was wondering if this is indeed the best approach or needed at all. Maybe there is a built-in function I am not aware of.

Alexander-Miller commented 2 years ago

The only thing I see is that you can simplify getting the name as (treemacs--filename (treemacs--parent-dir default-directory)). And for extra safety you can add a check that point is at a valid file node: (-some-> (treemacs-current-button) (treemacs-button-get :key) (file-exists-p)). Other than that it looks good to me.