Skip to content

Add proposed TreeView.clearSelection API - #331847

Open
Aex (adaex) wants to merge 1 commit into
microsoft:mainfrom
adaex:api/tree-view-clear-selection
Open

Aex (adaex) wants to merge 1 commit into
microsoft:mainfrom
adaex:api/tree-view-clear-selection

Conversation

@adaex

Copy link
Copy Markdown
Contributor

This adds a TreeView.clearSelection() method behind a new treeViewClearSelection API proposal.

Refs #48754

Why

TreeView.reveal(element, { select: true }) selects an element, but there is no way to undo it: TreeView.selection is read-only, and the selection survives refreshes because the tree tracks it by tree item handle.

This matters for any view that mirrors state owned by something else (an active editor, a running process, a cursor position). When that state stops corresponding to an element in the tree, the view keeps advertising a selection that is no longer true. VS Code's own explorer does not have this problem — open a file outside the workspace folders and it simply shows nothing selected.

The workaround extensions use today is to change TreeItem.id for the affected rows so the old handles stop resolving and the tree drops the selection along with the focus trait. It works, but it has real costs:

  • it needs a full onDidChangeTreeData refresh for what is purely a UI change, so the view visibly re-renders;
  • TreeItem.id becomes unstable, so anything else keyed off it needs to be kept on a separate identity;
  • providers end up carrying a generation counter per row plus the bookkeeping to expire those counters.

Given #48754 has been open for a while and the id-rewriting trick keeps being rediscovered, a small explicit API seemed worth proposing.

What

export interface TreeView<T> extends Disposable {
	clearSelection(): void;
}
  • Forwards to the existing internal ITreeView.setSelection([]), which is already used by $reveal.
  • Leaves the focused element alone — only the selection is cleared.
  • Unlike reveal, it does not open the view. Clearing a selection is not a request for the user's attention, and a view that was never created has no selection to clear.
  • Deliberately kept separate from the treeViewReveal proposal: reveal(undefined) there means "show an empty tree view" (Allow to reveal an empty TreeView programmatically #90005), which is a different operation and still under discussion. Overloading it further did not seem helpful.

Tests

  • mainThreadTreeViews.test.ts: the empty selection reaches the tree view, the view is not opened, and an unknown view id is a no-op.
  • vscode-api-tests: exercises the API surface and the round trip to the main thread.

I could not run the integration test suite in my environment (no display), so the vscode-api-tests addition is only verified as far as compiling and the API being reachable — worth a CI run.

Happy to adjust the name, the shape, or drop the "does not open the view" behaviour if you'd rather it match reveal.

Extensions can select an item with `TreeView.reveal`, but there is no way
to undo that: `TreeView.selection` is read-only and the selection survives
refreshes because it is tracked by tree item handle. Views that mirror
external state therefore keep showing a stale selection once that state no
longer maps to any element in the tree, which is what the built-in explorer
avoids when you open a file outside the workspace.

The workaround in the wild is to change every `TreeItem.id` so the old
handles no longer resolve and the selection is dropped. That forces a full
`onDidChangeTreeData` refresh for a pure UI change and makes item ids
unstable for everything else that keys off them.

Add `TreeView.clearSelection()` behind the `treeViewClearSelection` API
proposal. It forwards to the existing internal `ITreeView.setSelection([])`,
leaves the focused element alone and, unlike `reveal`, does not open the
view: clearing a selection is not a request for the user's attention.

Refs microsoft#48754
Copilot AI balanced review requested due to automatic review settings August 20, 2026 17:42

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds the proposed TreeView.clearSelection() extension API, clearing tree selections without opening the view or changing focus.

Changes:

  • Defines and registers the proposed API.
  • Implements the extension-host/main-thread RPC flow.
  • Adds unit and API coverage for clearing selections and no-op behavior.

Reviewed changes

Copilot reviewed 7 out of 8 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/vscode-dts/vscode.proposed.treeViewClearSelection.d.ts Defines the proposed API.
src/vs/workbench/api/test/browser/mainThreadTreeViews.test.ts Tests main-thread behavior.
src/vs/workbench/api/common/extHostTreeViews.ts Exposes and forwards the API.
src/vs/workbench/api/common/extHost.protocol.ts Adds the RPC contract.
src/vs/workbench/api/browser/mainThreadTreeViews.ts Clears the internal tree selection.
src/vs/platform/extensions/common/extensionsApiProposals.ts Registers the proposal.
extensions/vscode-api-tests/src/singlefolder-tests/tree.test.ts Exercises the extension API surface.
extensions/vscode-api-tests/package.json Enables the proposal for API tests.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified the diff. Changes align with project standards.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants