What is the correct way to retain a reference to a specific tab? #480
Replies: 2 comments 2 replies
-
We have introduced the |
Beta Was this translation helpful? Give feedback.
1 reply
-
Closing due to inactivity. Reopen if needed. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
I am writing a feature for an extension where I want to display some counterpart content based on whatever tab is active in another tab group. I want to reuse the same tab to display different counterpart content as the active tab changes. The counterpart content displayed may be a TextEditor or it may be a Webview. (Discussion #74 is vaguely relevant here as my first thought was to create a custom editor that embeds a TextEditor, but I guess that is also not possible.)
Since I don’t see any VSCode API for explicitly managing the content of a tab, I currently open a new
TextEditor
byshowTextDocument
orWebviewPanel
bycreateWebviewPanel
, listen ononDidChangeTabs
until I see a newly opened tab to retain, and finally callclose
on the previously stored tab so it is “as if” there is only a single tab.This is not a great approach, particularly since the UI ends up drawing two tabs for a frame (which is at least better than VSCode collapsing the view column for a frame by removing the old tab first), but it seems to be the only possible way to do this and it works “fine”.
Where this approach fails completely, though, is when a user opens a new tab group. For some reason I do not understand (since it shouldn’t be necessary to do this—even if backing objects change, the properties are all accessors?), this action causes multiple invalidations of existing
Tab
objects, even though none of the tabs have been closed (which is the only thing I can find in the documentation that’s stated to cause invalidation).The first invalidation of
Tab
objects seems to occur immediately when the new tab group is added. But then, after detecting that the retained Tab was invalidated (by receiving aTabGroupChangeEvent
withopened.length
) and recovering a replacement (by crossing my fingers that indexes intotabGroups.all
are stable), the recoveredTab
object is itself invalidated by some later event in the sequence of events.What are extension authors supposed to do to track a tab like this? I can give the
WebviewPanel
a unique ID and recover its tab with a brute force search oftabGroups.all
, but there’s no way to recover the tab from aTextEditor
: it doesn’t expose any reference to its tab, and multipleTextEditor
s can share documents with the same URI, so it’s also possible for multiple tabs to have the sameinput.uri
. I don’t think I can use a custom URI scheme or extra fragment to do that either, because if the user already has the counterpart content open in another tab, that would cause two uniqueTextDocument
s to be open instead of one for the same file, which is not desired. I don’t think I can avoid retaining theTab
object, because if a user manually closes the tracked tab, the only way to tell that is by seeing a tab closed event and checking to see if the tab is the same as the retained one by identity. I can’t access the internaltabId
. So I am pretty much out of ideas, short of crossing my fingers that tab group indexes intabGroups.all
are stable and also retaining the tab group’s index.Thanks,
Beta Was this translation helpful? Give feedback.
All reactions