Alexander-Miller / treemacs

GNU General Public License v3.0
2.08k stars 152 forks source link

About the workspace API #741

Open ksqsf opened 3 years ago

ksqsf commented 3 years ago

Hi,

I want to enhance my treemacs experience by adding the current project automatically to an appropriate workspace, that is

  1. If it's already a project in a workspace: jump to that workspace and locate that project
  2. If it's not: add that project to a default workspace exclusively

I have figured out that I can use treemacs-workspaces, treemacs-workspace->projects, etc. to query information about workspaces, but what confuses me is the "adding" part. It seems that I can't simply add a project to a workspace object, but have to switch to that workspace first, and then add it to "the current workspace".

Ok, so I keep on reading the doc, only to find that there's no API that can get a workspace object by its name. I replicated some bits of the code, and it may be useful to others.

  (defun -treemacs-get-workspace (name)
    (let ((workspaces (->> treemacs--workspaces
                           (--reject (eq it (treemacs-current-workspace)))
                           (--map (cons (treemacs-workspace->name it) it)))))
      (cdr (--first (string= (car it) name) workspaces))))

  (defun -treemacs-create-workspace (name)
    (treemacs-block
     (treemacs-return-if (treemacs--is-name-invalid? name)
       `(invalid-name ,name))
     (-when-let (ws (--first (string= name (treemacs-workspace->name it))
                             treemacs--workspaces))
       (treemacs-return `(duplicate-name ,ws)))
     (-let [workspace (treemacs-workspace->create! :name name)]
       (add-to-list 'treemacs--workspaces workspace :append)
       (treemacs--persist)
       (run-hook-with-args 'treemacs-create-workspace-functions workspace)
       `(success ,workspace))))

  (defun -treemacs-get-or-create-workspace (name)
    (or (--first (string= name (treemacs-workspace->name it))
                 treemacs--workspaces)
        (let (res (-treemacs-create-workspace name))
          (if (equal (car res) 'success)
              (cdr res)
            (error "Couldn't create workspace")))))

  (defun -treemacs-switch-to-workspace (ws)
    (setf (treemacs-current-workspace) ws)
    (treemacs--invalidate-buffer-project-cache)
    (treemacs--rerender-after-workspace-change)
    (run-hooks 'treemacs-switch-workspace-hook))

  (defun -treemacs-which-workspace ()
    "Which workspace does the current file belong to?"
    (--first (treemacs-is-path (buffer-file-name) :in-workspace it) (treemacs-workspaces)))

Hopefully there will be some better API to serve the users' needs. :-)

ksqsf commented 3 years ago
  (defun -treemacs-add-project-to-workspace-or-switch ()
    "Add the current project to the default workspace, or locate it if it's already known in a workspace.

The default workspace is specificied by `prelude-treemacs-default-workspace'"
    (let (current-file-ws (-treemacs-which-workspace))
      (if current-file-ws
          (-treemacs-switch-to-workspace current-file-ws)
        (let (default-ws (-treemacs-get-or-create-workspace prelude-treemacs-default-workspace))
          (-treemacs-switch-to-workspace default-ws)
          (treemacs-display-current-project-exclusively)))))

And this doesn't seem to work? It reports Wrong type argument: arrayp, nil.

edit: oops I missed a pair of parens.

Alexander-Miller commented 3 years ago

Hopefully there will be some better API to serve the users' needs. :-)

Apis need to be used to find their gaps. This stuff has never come up before and the first step to getting new features is to ask :shrug:

The things you're asking for sound all reasonable and doable to me, so let's make a list:

Anything else?

ksqsf commented 3 years ago

Maybe create and remove? I think something like with-treemacs-workspace can be helpful too.

Alexander-Miller commented 3 years ago

Pushed api to find workspaces now.

Maybe create and remove?

Those already exist with treemcs-do-create/remove-workspace. When there's a split for interactive and non-interactive code the former is called treemacs-X and the latter is called treemacs-do-X.

I think something like with-treemacs-workspace can be helpful too.

There's already treemacs-override-workspace, but yes, it's better to have a well-named macro.

Alexander-Miller commented 3 years ago

Deleting workspace by name wasn't on the list, but it's pushed now anyway.

Alexander-Miller commented 3 years ago

treemacs-with-workspace macro is pushed as well.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Alexander-Miller commented 2 years ago

Stayin alive.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Alexander-Miller commented 2 years ago

Stayin alive.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Alexander-Miller commented 2 years ago

Stayin alive.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Alexander-Miller commented 2 years ago

Stayin alive.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity.