Repository navigation
Add proposed TreeView.clearSelection API - #331847
Open
Aex (adaex) wants to merge 1 commit into
Open
Aex (adaex) wants to merge 1 commit into
Aex (adaex) wants to merge 1 commit into
Conversation
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
Contributor
There was a problem hiding this comment.
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.
Mohammad javad Dianat (dianatofficial)
left a comment
There was a problem hiding this comment.
Verified the diff. Changes align with project standards.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds a
TreeView.clearSelection()method behind a newtreeViewClearSelectionAPI proposal.Refs #48754
Why
TreeView.reveal(element, { select: true })selects an element, but there is no way to undo it:TreeView.selectionis 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.idfor 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:onDidChangeTreeDatarefresh for what is purely a UI change, so the view visibly re-renders;TreeItem.idbecomes unstable, so anything else keyed off it needs to be kept on a separate identity;Given #48754 has been open for a while and the id-rewriting trick keeps being rediscovered, a small explicit API seemed worth proposing.
What
ITreeView.setSelection([]), which is already used by$reveal.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.treeViewRevealproposal: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-testsaddition 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.