You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow programatically un-selecting an item in a contributed tree #48754
We can select items in the tree node with reveal, but we can't un-select. For an outline view that follows the users cursor, it would be good to unselect when the user moves the cursor to some empty whitespace that doesn't have a matching node.
The existing explorer treeview also does this; if you open a file that's outside of the workspace folders (or create a new untitled file) then no items are selected in the tree.
Reacted by Danny Tuppeny, Sergei Dyshel, Dave, Michael Blout, LemonShaped, Haydar Metin, David Tejada, Catherine, SolovievEvgenii, Sergei Osadchuk and 11 more
Sandeep Somavarapu (@sandy081) If this isn't possible yet, do you know of any workarounds? I'm trying to keep a node selected based on where the user is, but I need to unselect it when they navigate somewhere I don't have a node for (it's really confusing to leave the last thing selected).
This would be useful, both for a contributed TreeView as well as for the standard file explorer. I am working on an extension that adds a second TreeView to the explorer view container, and when an item is selected in the second TreeView, I need to be able to un-select an item in the explorer. As far as I can tell, there's no way to do that.
Reacted by Dave, Aly Thobani, Mikhail Isaev aka iMike, onfireprojects and Phillip Klein
Ditto on this! I tried a few hacky workarounds (e.g. calling TreeView.reveal(undefined as unknown as T, {select: true, focus: false})) and almost none of them worked. Temporarily clearing/refreshing the tree view (or adding/selecting/removing a dummy TreeItem) did work—but the resulting UI flash / jumpiness wasn't ideal so I'd rather just let the item stay selected. So yeah, this feature would be very nice.
Reacted by Dave, Mikhail Isaev aka iMike, onfireprojects and rikzun
however it for some reason breaks command execution in updated TreeItem, but you still can call vscode.commands.executeCommand in treeView.onDidChangeSelection section
Thanks to rikzun (@rikzun)'s brilliant hint in this thread! We implemented it and can confirm it works perfectly.
The Problem
VS Code TreeView API has no clearSelection() method. You can select items with reveal(), but can't deselect them.
The Solution: Selection Epoch Hack
VS Code tracks selection by TreeItem.id. If you change all IDs, VS Code "forgets" the selection.
exportclassMyTreeDataProviderimplementsvscode.TreeDataProvider<MyItem>{private_onDidChangeTreeData=newvscode.EventEmitter<MyItem|undefined>();readonlyonDidChangeTreeData=this._onDidChangeTreeData.event;privateselectionEpoch=0;// ← the magic countergetChildren(): MyItem[]{return[newMyItem('item1',this.selectionEpoch,'First Item'),newMyItem('item2',this.selectionEpoch,'Second Item'),];}// Clears selection by invalidating all IDsclearSelection(): void{this.selectionEpoch++;this._onDidChangeTreeData.fire(undefined);}}classMyItemextendsvscode.TreeItem{constructor(itemKey: string,epoch: number,label: string){super(label,vscode.TreeItemCollapsibleState.None);this.id=`${itemKey}@${epoch}`;// ← epoch in ID is the key!}}
Why It Works
VS Code tracks selection by TreeItem.id
Incrementing epoch changes all IDs (item1@0 → item1@1)
Old IDs don't exist anymore → selection cleared
Note: Implement getParent() if you use reveal(), otherwise it throws an error.
Tested on VS Code 1.96.0 with 3 synchronized TreeView panels. Works great!
I've opened #331847 with a minimal proposal for this, in case it helps move the discussion along.
It adds TreeView.clearSelection() behind a treeViewClearSelection API proposal, forwarding to the internal ITreeView.setSelection([]) that $reveal already uses. It leaves the focused element alone, and does not open the view — clearing a selection isn't a request for the user's attention.
Two notes on scope:
I kept it separate from the existing treeViewReveal proposal on purpose. reveal(undefined) there means "show an empty tree view" (Allow to reveal an empty TreeView programmatically #90005) and is still being debated; adding a second meaning to it seemed likely to make that harder rather than easier.
On the id-rewriting workaround described above: it does work, but it costs a full onDidChangeTreeData refresh for a purely visual change, and it makes TreeItem.id unstable, so anything else keyed off the id has to be kept on a separate identity. That is a fair amount of machinery to maintain for "stop showing this row as selected".
Happy to change the name or shape, or close it if you'd rather solve this differently.
Activity
[-]Allow un-selected an item in a contributed tree[/-][+]Allow un-selecting an item in a contributed tree[/+]DanTup commentedon Sep 19, 2018
Sandeep Somavarapu (@sandy081) If this isn't possible yet, do you know of any workarounds? I'm trying to keep a node selected based on where the user is, but I need to unselect it when they navigate somewhere I don't have a node for (it's really confusing to leave the last thing selected).
Select dependencies tree node when moving around
sandy081 commentedon Sep 19, 2018
Danny Tuppeny (@DanTup) Sorry, there is no workaround. I will try to implement this soon.
Select dependencies tree node when moving around
DanTup commentedon Jan 29, 2019
Sandeep Somavarapu (@sandy081) Is this still planned?
sandy081 commentedon Jan 29, 2019
Danny Tuppeny (@DanTup) not for January.
DanTup commentedon Feb 14, 2019
Sandeep Somavarapu (@sandy081) Any chance for Feb? :)
sandy081 commentedon Feb 18, 2019
No tree features planned in Feb. Would be jumping back to them in March for sure.
38 remaining items
mjrist commentedon Aug 28, 2024
Need the same feature in my extension.
marcel-goldschen-ohm commentedon Dec 18, 2024
Yes, this would be useful.
dherman commentedon Jan 30, 2025
This would be useful, both for a contributed
TreeViewas well as for the standard file explorer. I am working on an extension that adds a second TreeView to the explorer view container, and when an item is selected in the second TreeView, I need to be able to un-select an item in the explorer. As far as I can tell, there's no way to do that.alythobani commentedon Feb 14, 2025
Ditto on this! I tried a few hacky workarounds (e.g. calling
TreeView.reveal(undefined as unknown as T, {select: true, focus: false})) and almost none of them worked. Temporarily clearing/refreshing the tree view (or adding/selecting/removing a dummyTreeItem) did work—but the resulting UI flash / jumpiness wasn't ideal so I'd rather just let the item stay selected. So yeah, this feature would be very nice.rikzun commentedon Jun 12, 2025
found how to do it
you just need to edit TreeItem id
however it for some reason breaks command execution in updated TreeItem, but you still can call
vscode.commands.executeCommandintreeView.onDidChangeSelectionsectioneduardotenholder commentedon Jun 17, 2025
I would like to use this feature in my extension, too.
Just ship it already, please.
alatyshau commentedon Dec 17, 2025
Thanks to rikzun (@rikzun)'s brilliant hint in this thread! We implemented it and can confirm it works perfectly.
The Problem
VS Code TreeView API has no
clearSelection()method. You can select items withreveal(), but can't deselect them.The Solution: Selection Epoch Hack
VS Code tracks selection by
TreeItem.id. If you change all IDs, VS Code "forgets" the selection.Why It Works
TreeItem.iditem1@0→item1@1)Note: Implement
getParent()if you usereveal(), otherwise it throws an error.Tested on VS Code 1.96.0 with 3 synchronized TreeView panels. Works great!
whitequark commentedon Dec 17, 2025
That is unbelievably cursed.
slfotg commentedon Feb 28, 2026
The hack works-ish but I'd really like a solution that doesn't involve firing the
onDidChangeTreeDataeventslfotg commentedon Feb 28, 2026
Triggering the event forces a re-render which is not ideal.
adaex commentedon Aug 20, 2026
I've opened #331847 with a minimal proposal for this, in case it helps move the discussion along.
It adds
TreeView.clearSelection()behind atreeViewClearSelectionAPI proposal, forwarding to the internalITreeView.setSelection([])that$revealalready uses. It leaves the focused element alone, and does not open the view — clearing a selection isn't a request for the user's attention.Two notes on scope:
treeViewRevealproposal on purpose.reveal(undefined)there means "show an empty tree view" (Allow to reveal an empty TreeView programmatically #90005) and is still being debated; adding a second meaning to it seemed likely to make that harder rather than easier.onDidChangeTreeDatarefresh for a purely visual change, and it makesTreeItem.idunstable, so anything else keyed off the id has to be kept on a separate identity. That is a fair amount of machinery to maintain for "stop showing this row as selected".Happy to change the name or shape, or close it if you'd rather solve this differently.