Skip to content

Commit 1bc0c31

Browse files
committed
Initial implementation of treeview/workspace switching.
Use the buffer-list-update hook to detect when the user changes to a file within another workspace and if the treeview is visible switch to the new workspace's treeview window.
1 parent 3680114 commit 1bc0c31

File tree

1 file changed

+39
-14
lines changed

1 file changed

+39
-14
lines changed

lsp-metals-treeview.el

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ lsp-metals-treeview to display the treeview explicitly."
6363
:group 'lsp-metals-treeview
6464
:type 'boolean)
6565

66+
(defcustom lsp-metals-treeview-workspace-switch-delay 0.2
67+
"Delay in seconds after buffer-list-update-hook is called before
68+
triggering a switch of treeview when navigating between buffers in
69+
different workspaces.")
70+
6671
(cl-defstruct lsp--metals-treeview-data
6772
(views nil)
6873
(buffers nil))
@@ -115,21 +120,33 @@ will hold the workspace associated with the instance.")
115120
is currently being displayed and whether we need to show
116121
an alternative workspace's treeview."
117122
(with-current-buffer (current-buffer)
118-
(-if-let ((workspaces (lsp-workspaces)))
123+
(-if-let (workspaces (lsp-workspaces))
119124
(progn
120-
(lsp-log "we have workspaces")
121125
(when (and lsp--metals-treeview-active-view-workspace
122126
(not (member lsp--metals-treeview-active-view-workspace
123127
workspaces)))
124-
;; hide current treeview
128+
;; hide current treeview and show new window associated with
129+
;; the current workspace of file in buffer.
125130
(lsp--metals-treeview-hide-window lsp--metals-treeview-active-view-workspace)
126-
(lsp--metals-treeview-show-window (car workspaces)))))
127-
(lsp-log "no workspaces")))
128-
129-
;; (defun lsp--metals-test-add-buffer-switch-hook ()
130-
;; (interactive)
131-
;; (add-hook 'buffer-list-update-hook #'lsp--metals-treeview-buffer-changed))
132-
131+
(lsp--metals-treeview-show-window (car workspaces)))))))
132+
133+
(defun lsp--metals-treeview-buffer-list-update ()
134+
(run-with-idle-timer lsp-metals-treeview-workspace-switch-delay
135+
nil
136+
#'lsp--metals-treeview-buffer-changed))
137+
138+
(defun lsp--metals-treeview-add-workspace-switch-hook ()
139+
"Add a buffer-list-update-hook to hide/show the active treeview
140+
(if currently displayed) when the user switches buffers that are
141+
within another workspace."
142+
(add-hook 'buffer-list-update-hook
143+
#'lsp--metals-treeview-buffer-list-update))
144+
145+
(defun lsp--metals-treeview-remove-workspace-switch-hook ()
146+
"Remove the buffer-list-update-hook for switching treeview between
147+
workspaces."
148+
(remove-hook 'buffer-list-update-hook
149+
#'lsp--metals-treeview-buffer-list-update))
133150

134151
(defun lsp--metals-treeview-log (format &rest args)
135152
"Log treeview tracing/debug messages to the lsp-log"
@@ -215,7 +232,9 @@ be live in the background."
215232
cur-workspace lsp--metals-view-id nil))
216233
(delete-window (get-buffer-window buffer)))
217234
(lsp--metals-treeview-get-buffers cur-workspace))
218-
(setq lsp--metals-treeview-active-view-workspace nil)))
235+
(setq lsp--metals-treeview-active-view-workspace nil)
236+
;; Only keep this treeview switching hook live when absolutely necessary
237+
(lsp--metals-treeview-remove-workspace-switch-hook)))
219238

220239
(defun lsp--metals-treeview-get-visible-buffers ()
221240
"Retrieve buffers associated with the current selected
@@ -268,9 +287,9 @@ then show the window."
268287
(views (if view-data
269288
(lsp--metals-treeview-data-views view-data)
270289
nil)))
271-
(if (or (eq 'hidden visibility) (eq 'none visibility))
272-
(lsp--metals-show-metals-views workspace
273-
views 0 select-window?))))
290+
(when (or (eq 'hidden visibility) (eq 'none visibility))
291+
(lsp--metals-show-metals-views workspace
292+
views 0 select-window?))))
274293

275294
(defun lsp--metals-treeview-delete-window (&optional workspace workspace-shutdown?)
276295
"Delete the metals treeview window associated with the WORKSPACE.
@@ -296,6 +315,8 @@ t."
296315
(lsp--metals-treeview-get-buffers cur-workspace))
297316
(lsp--metals-treeview-remove-buffers cur-workspace)
298317
(setq lsp--metals-treeview-active-view-workspace nil)
318+
;; Only keep this treeview switching hook live when absolutely necessary.
319+
(lsp--metals-treeview-remove-workspace-switch-hook)
299320
(remove-hook 'lsp-after-uninitialized-hook #'lsp--metals-treeview-delete-window)))
300321

301322
(defun lsp--metals-treeview-on-workspace-shutdown (workspace)
@@ -412,6 +433,10 @@ relative to the others. "
412433
(lsp--metals-treeview-select-window workspace))
413434

414435
(setq lsp--metals-treeview-active-view-workspace workspace)
436+
437+
;; When user switches between files in workspaces automatically switch
438+
;; the treeview to the appropriate one.
439+
(lsp--metals-treeview-add-workspace-switch-hook)
415440

416441
;; Add hook to close our treeview when the workspace is shutdown.
417442
(add-hook 'lsp-after-uninitialized-hook #'lsp--metals-treeview-on-workspace-shutdown))

0 commit comments

Comments
 (0)