treemacs icon indicating copy to clipboard operation
treemacs copied to clipboard

About the workspace API

Open ksqsf opened this issue 5 years ago • 15 comments
trafficstars

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 avatar Nov 09 '20 14:11 ksqsf

  (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.

ksqsf avatar Nov 09 '20 15:11 ksqsf

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:

  • [x] Get workspace by name
  • [x] Find workspace for path
  • [x] Delete workspace by name
  • [ ] Add project to workspace other than the current one
  • [x] with-workspace macro

Anything else?

Alexander-Miller avatar Nov 09 '20 19:11 Alexander-Miller

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

ksqsf avatar Nov 10 '20 06:11 ksqsf

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 avatar Nov 11 '20 19:11 Alexander-Miller

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

Alexander-Miller avatar Nov 18 '20 19:11 Alexander-Miller

treemacs-with-workspace macro is pushed as well.

Alexander-Miller avatar Dec 27 '20 13:12 Alexander-Miller

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.

stale[bot] avatar Oct 07 '21 16:10 stale[bot]

Stayin alive.

Alexander-Miller avatar Oct 09 '21 10:10 Alexander-Miller

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.

stale[bot] avatar Dec 08 '21 11:12 stale[bot]

Stayin alive.

Alexander-Miller avatar Dec 25 '21 15:12 Alexander-Miller

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.

stale[bot] avatar Feb 23 '22 15:02 stale[bot]

Stayin alive.

Alexander-Miller avatar Feb 23 '22 18:02 Alexander-Miller

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.

stale[bot] avatar Apr 24 '22 18:04 stale[bot]

Stayin alive.

Alexander-Miller avatar Apr 25 '22 10:04 Alexander-Miller

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

stale[bot] avatar Jun 24 '22 12:06 stale[bot]