Skip to content

Allow programatically un-selecting an item in a contributed tree #48754

Open

Description

@DanTup

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.

Activity

  1. added
    feature-request
    tree-views
    on Apr 26, 2018
  2. changed the title [-]Allow un-selected an item in a contributed tree[/-] [+]Allow un-selecting an item in a contributed tree[/+] on Apr 26, 2018
  3. DanTup commented on Sep 19, 2018

    @DanTup
    ContributorAuthor

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

  4. sandy081 commented on Sep 19, 2018

    @sandy081
    Member

    Danny Tuppeny (@DanTup) Sorry, there is no workaround. I will try to implement this soon.

  5. DanTup commented on Jan 29, 2019

    @DanTup
    ContributorAuthor

    Sandeep Somavarapu (@sandy081) Is this still planned?

  6. sandy081 commented on Jan 29, 2019

    @sandy081
    Member

    Danny Tuppeny (@DanTup) not for January.

  7. DanTup commented on Feb 14, 2019

    @DanTup
    ContributorAuthor

    Sandeep Somavarapu (@sandy081) Any chance for Feb? :)

  8. sandy081 commented on Feb 18, 2019

    @sandy081
    Member

    No tree features planned in Feb. Would be jumping back to them in March for sure.

  9. 38 remaining items

  10. mjrist commented on Aug 28, 2024

    @mjrist

    Need the same feature in my extension.

  11. marcel-goldschen-ohm commented on Dec 18, 2024

    @marcel-goldschen-ohm

    Yes, this would be useful.

  12. dherman commented on Jan 30, 2025

    @dherman

    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.

  13. alythobani commented on Feb 14, 2025

    @alythobani

    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.

  14. rikzun commented on Jun 12, 2025

    @rikzun

    found how to do it
    you just need to edit TreeItem id

    clearSelection(id: string) {
        const element = this.steps.find((v) => v.id == id)
        if (!element) return
        
        element.id = "-1"
        this._onDidChangeTreeData.fire()
        
        setTimeout(() => {
            element.id = id
            this._onDidChangeTreeData.fire()
        }, 50)
    }
    
    treeView.onDidChangeSelection((e) => {
        if (!e.selection.length) return
        provider.clearSelection(e.selection[0].id!)
    })

    however it for some reason breaks command execution in updated TreeItem, but you still can call vscode.commands.executeCommand in treeView.onDidChangeSelection section

  15. eduardotenholder commented on Jun 17, 2025

    @eduardotenholder

    I would like to use this feature in my extension, too.
    Just ship it already, please.

  16. alatyshau commented on Dec 17, 2025

    @alatyshau

    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.

    export class MyTreeDataProvider implements vscode.TreeDataProvider<MyItem> {
        private _onDidChangeTreeData = new vscode.EventEmitter<MyItem | undefined>();
        readonly onDidChangeTreeData = this._onDidChangeTreeData.event;
    
        private selectionEpoch = 0;  // ← the magic counter
    
        getChildren(): MyItem[] {
            return [
                new MyItem('item1', this.selectionEpoch, 'First Item'),
                new MyItem('item2', this.selectionEpoch, 'Second Item'),
            ];
        }
    
        // Clears selection by invalidating all IDs
        clearSelection(): void {
            this.selectionEpoch++;
            this._onDidChangeTreeData.fire(undefined);
        }
    }
    
    class MyItem extends vscode.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

    1. VS Code tracks selection by TreeItem.id
    2. Incrementing epoch changes all IDs (item1@0 → item1@1)
    3. 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!

  17. whitequark commented on Dec 17, 2025

    @whitequark

    That is unbelievably cursed.

  18. slfotg commented on Feb 28, 2026

    @slfotg

    The hack works-ish but I'd really like a solution that doesn't involve firing the onDidChangeTreeData event

  19. slfotg commented on Feb 28, 2026

    @slfotg

    The hack works-ish but I'd really like a solution that doesn't involve firing the onDidChangeTreeData event

    Triggering the event forces a re-render which is not ideal.

  20. adaex commented on Aug 20, 2026

    @adaex
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

apifeature-requesttree-views

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions