microsoft / microsoft/vscode

Add expand/collapse events for folders in the built-in Explorer

Open
#327,242 3 comments 1 reaction 1 assignee Claimed by @lramos15 View on GitHub
feature-request tree-views
Dominant language
TypeScript
Stars
193k
Forks
42.4k
PR merge metrics
PR metrics pending

Description

I'd like to request an API that lets extensions detect when a folder in the built-in VS Code Explorer is expanded or collapsed.

I know that `TreeView` already has `onDidExpandElement` and `onDidCollapseElement`, but, if I understand correctly, those events only work for TreeViews provided by an extension. There doesn't seem to be an equivalent for the standard File Explorer.

## My use case

I'd like to build an extension that automatically collapses the contents of a folder when that folder itself is collapsed.

For example, imagine I have this:
```
project/
└── src/
├── components/
│ ├── buttons/
│ └── inputs/
└── utils/
```

If I expand folder `src`, then open several nested folders, and later collapse the `src`, I'd like all of the nested folders recursively under `src` to be collapsed as well.

That way, when I expand `src` again, I see a clean, collapsed `src` folder instead of the previous deeply expanded tree.

This is especially useful in larger projects where the Explorer can quickly become cluttered with many expanded folders.

## Proposed API

I'm not particularly attached to the naming, but something along the lines of:
```js
vscode.window.onDidExpandExplorerItem
vscode.window.onDidCollapseExplorerItem
```
would allow extensions to react to changes in the built-in Explorer.

The event could provide the URI of the affected item, for example:
```js
vscode.window.onDidCollapseExplorerItem(event => {
console.log(event.uri);
});
```

There may be a better way to expose this that fits the existing API design; the main thing I'm looking for is the ability to observe expand/collapse events from the built-in Explorer.

I've seen this requested a number of times in discussions and feature requests around VS Code, and similar behavior is available in other editors/IDEs. It seems like a relatively small feature that could also enable extensions to provide more useful Explorer navigation and organization features.

I'd be interested in contributing an implementation if this is something the VS Code team would consider adding.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.