-
Notifications
You must be signed in to change notification settings - Fork 39.1k
Support DocumentSymbolProvider in CustomTextEditor #304909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jogibear9988
wants to merge
6
commits into
microsoft:main
Choose a base branch
from
jogibear9988:customEditorOutline
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,521
−1
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
014f7e7
A full proposed extension API was added so that extensions providing …
jogibear9988 9445d44
fix copilot issues
jogibear9988 c380f6d
work on github issues
jogibear9988 bc58b9d
fix selection
jogibear9988 4a1583c
fix comment https://github.com/microsoft/vscode/pull/304909#discussio…
jogibear9988 fe178bf
fixes after copilot review
jogibear9988 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
208 changes: 208 additions & 0 deletions
208
src/vs/workbench/api/browser/mainThreadCustomEditorOutline.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,208 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import { CancellationToken } from '../../../base/common/cancellation.js'; | ||
| import { Emitter, Event } from '../../../base/common/event.js'; | ||
| import { Disposable, DisposableMap, IDisposable, toDisposable } from '../../../base/common/lifecycle.js'; | ||
| import { URI, UriComponents } from '../../../base/common/uri.js'; | ||
| import { InstantiationType, registerSingleton } from '../../../platform/instantiation/common/extensions.js'; | ||
| import { extHostNamedCustomer, IExtHostContext } from '../../services/extensions/common/extHostCustomers.js'; | ||
| import { ExtHostContext, ExtHostCustomEditorOutlineShape, MainContext, MainThreadCustomEditorOutlineShape } from '../common/extHost.protocol.js'; | ||
| import { ICustomEditorOutlineItemDto, ICustomEditorOutlineProviderService } from '../../contrib/customEditor/common/customEditorOutlineService.js'; | ||
|
|
||
| class ResourceEntry { | ||
| private readonly _onDidChangeOutline = new Emitter<void>(); | ||
| readonly onDidChangeOutline = this._onDidChangeOutline.event; | ||
|
|
||
| private readonly _onDidChangeActiveItem = new Emitter<string | undefined>(); | ||
| readonly onDidChangeActiveItem = this._onDidChangeActiveItem.event; | ||
|
|
||
| private _activeItemId: string | undefined; | ||
|
|
||
| get activeItemId(): string | undefined { return this._activeItemId; } | ||
|
|
||
| fireDidChangeOutline(): void { | ||
| this._onDidChangeOutline.fire(); | ||
| } | ||
|
|
||
| fireDidChangeActiveItem(itemId: string | undefined): void { | ||
| this._activeItemId = itemId; | ||
| this._onDidChangeActiveItem.fire(itemId); | ||
| } | ||
|
|
||
| dispose(): void { | ||
| this._onDidChangeOutline.dispose(); | ||
| this._onDidChangeActiveItem.dispose(); | ||
| } | ||
| } | ||
|
|
||
| class CustomEditorOutlineProviderEntry { | ||
|
|
||
| private readonly _resourceEntries = new Map<string, ResourceEntry>(); | ||
|
|
||
| getOrCreateResourceEntry(resource: URI): ResourceEntry { | ||
| const key = resource.toString(); | ||
| let entry = this._resourceEntries.get(key); | ||
| if (!entry) { | ||
| entry = new ResourceEntry(); | ||
| this._resourceEntries.set(key, entry); | ||
| } | ||
| return entry; | ||
| } | ||
|
|
||
| getResourceEntry(resource: URI): ResourceEntry | undefined { | ||
| return this._resourceEntries.get(resource.toString()); | ||
| } | ||
|
|
||
| fireDidChangeOutline(resource: URI): void { | ||
| this._resourceEntries.get(resource.toString())?.fireDidChangeOutline(); | ||
| } | ||
|
|
||
| fireDidChangeActiveItem(resource: URI, itemId: string | undefined): void { | ||
| this.getOrCreateResourceEntry(resource).fireDidChangeActiveItem(itemId); | ||
| } | ||
|
|
||
| removeResourceEntry(resource: URI): void { | ||
| const key = resource.toString(); | ||
| const entry = this._resourceEntries.get(key); | ||
| if (entry) { | ||
| entry.dispose(); | ||
| this._resourceEntries.delete(key); | ||
| } | ||
| } | ||
|
|
||
| dispose(): void { | ||
| for (const entry of this._resourceEntries.values()) { | ||
| entry.dispose(); | ||
| } | ||
| this._resourceEntries.clear(); | ||
| } | ||
| } | ||
|
|
||
| class CustomEditorOutlineProviderService extends Disposable implements ICustomEditorOutlineProviderService { | ||
| declare readonly _serviceBrand: undefined; | ||
|
|
||
| private readonly _entries = this._register(new DisposableMap<string, CustomEditorOutlineProviderEntry>()); | ||
|
|
||
| private readonly _onDidChange = this._register(new Emitter<void>()); | ||
| readonly onDidChange: Event<void> = this._onDidChange.event; | ||
|
|
||
| private _provideOutline?: (viewType: string, resource: URI, token: CancellationToken) => Promise<ICustomEditorOutlineItemDto[] | undefined>; | ||
| private _revealItem?: (viewType: string, resource: URI, itemId: string) => void; | ||
|
|
||
| setDelegate(delegate: { | ||
| provideOutline: (viewType: string, resource: URI, token: CancellationToken) => Promise<ICustomEditorOutlineItemDto[] | undefined>; | ||
| revealItem: (viewType: string, resource: URI, itemId: string) => void; | ||
| }): void { | ||
| this._provideOutline = delegate.provideOutline; | ||
| this._revealItem = delegate.revealItem; | ||
| } | ||
|
|
||
| hasProvider(viewType: string): boolean { | ||
| return this._entries.has(viewType); | ||
| } | ||
|
|
||
| getProviderViewTypes(): string[] { | ||
| return [...this._entries.keys()]; | ||
| } | ||
|
|
||
| async provideOutline(viewType: string, resource: URI, token: CancellationToken): Promise<ICustomEditorOutlineItemDto[] | undefined> { | ||
| if (this._provideOutline) { | ||
| return this._provideOutline(viewType, resource, token); | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| revealItem(viewType: string, resource: URI, itemId: string): void { | ||
| if (this._revealItem) { | ||
| this._revealItem(viewType, resource, itemId); | ||
| } | ||
| } | ||
|
|
||
| getActiveItemId(viewType: string, resource: URI): string | undefined { | ||
| return this._entries.get(viewType)?.getResourceEntry(resource)?.activeItemId; | ||
| } | ||
|
|
||
| onDidChangeOutline(viewType: string, resource: URI): Event<void> { | ||
| const entry = this._entries.get(viewType); | ||
| return entry ? entry.getOrCreateResourceEntry(resource).onDidChangeOutline : Event.None; | ||
| } | ||
|
|
||
| onDidChangeActiveItem(viewType: string, resource: URI): Event<string | undefined> { | ||
| const entry = this._entries.get(viewType); | ||
| return entry ? entry.getOrCreateResourceEntry(resource).onDidChangeActiveItem : Event.None; | ||
| } | ||
|
|
||
| registerProvider(viewType: string): IDisposable { | ||
| const entry = new CustomEditorOutlineProviderEntry(); | ||
| this._entries.set(viewType, entry); | ||
| this._onDidChange.fire(); | ||
| return toDisposable(() => { | ||
| this._entries.deleteAndDispose(viewType); | ||
| this._onDidChange.fire(); | ||
| }); | ||
| } | ||
|
|
||
| unregisterProvider(viewType: string): void { | ||
| this._entries.deleteAndDispose(viewType); | ||
| this._onDidChange.fire(); | ||
| } | ||
|
|
||
| releaseResource(viewType: string, resource: URI): void { | ||
| this._entries.get(viewType)?.removeResourceEntry(resource); | ||
| } | ||
|
|
||
| fireDidChangeOutline(viewType: string, resource: URI): void { | ||
| this._entries.get(viewType)?.fireDidChangeOutline(resource); | ||
| } | ||
|
|
||
| fireDidChangeActiveItem(viewType: string, resource: URI, itemId: string | undefined): void { | ||
| this._entries.get(viewType)?.fireDidChangeActiveItem(resource, itemId); | ||
| } | ||
| } | ||
|
|
||
| registerSingleton(ICustomEditorOutlineProviderService, CustomEditorOutlineProviderService, InstantiationType.Delayed); | ||
|
|
||
| @extHostNamedCustomer(MainContext.MainThreadCustomEditorOutline) | ||
| export class MainThreadCustomEditorOutline extends Disposable implements MainThreadCustomEditorOutlineShape { | ||
|
|
||
| private readonly _proxy: ExtHostCustomEditorOutlineShape; | ||
| private readonly _registrations = this._register(new DisposableMap<string>()); | ||
|
|
||
| constructor( | ||
| context: IExtHostContext, | ||
| @ICustomEditorOutlineProviderService private readonly _service: ICustomEditorOutlineProviderService, | ||
| ) { | ||
| super(); | ||
| this._proxy = context.getProxy(ExtHostContext.ExtHostCustomEditorOutline); | ||
|
|
||
| // Wire the service delegate to call through to the ext host | ||
| if (this._service instanceof CustomEditorOutlineProviderService) { | ||
| this._service.setDelegate({ | ||
| provideOutline: (viewType, resource, token) => this._proxy.$provideOutline(viewType, resource, token), | ||
| revealItem: (viewType, resource, itemId) => this._proxy.$revealItem(viewType, resource, itemId), | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| $registerCustomEditorOutlineProvider(viewType: string): void { | ||
| const registration = this._service.registerProvider(viewType); | ||
| this._registrations.set(viewType, registration); | ||
| } | ||
|
|
||
| $unregisterCustomEditorOutlineProvider(viewType: string): void { | ||
| // deleteAndDispose disposes the registration returned by registerProvider(), | ||
| // whose dispose handler already removes the entry and fires onDidChange. | ||
| this._registrations.deleteAndDispose(viewType); | ||
| } | ||
|
|
||
| $onDidChangeOutline(viewType: string, resource: UriComponents): void { | ||
| this._service.fireDidChangeOutline(viewType, URI.revive(resource)); | ||
| } | ||
|
|
||
| $onDidChangeActiveItem(viewType: string, resource: UriComponents, itemId: string | undefined): void { | ||
| this._service.fireDidChangeActiveItem(viewType, URI.revive(resource), itemId); | ||
| } | ||
| } | ||
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import type * as vscode from 'vscode'; | ||
| import { CancellationToken } from '../../../base/common/cancellation.js'; | ||
| import { DisposableStore, toDisposable } from '../../../base/common/lifecycle.js'; | ||
| import { ThemeIcon } from '../../../base/common/themables.js'; | ||
| import { URI, UriComponents } from '../../../base/common/uri.js'; | ||
| import { ExtHostCustomEditorOutlineShape, ICustomEditorOutlineItemDto, MainContext, MainThreadCustomEditorOutlineShape } from './extHost.protocol.js'; | ||
| import { IExtensionDescription } from '../../../platform/extensions/common/extensions.js'; | ||
| import { checkProposedApiEnabled } from '../../services/extensions/common/extensions.js'; | ||
| import { IRPCProtocol } from '../../services/extensions/common/proxyIdentifier.js'; | ||
|
|
||
| export class ExtHostCustomEditorOutline implements ExtHostCustomEditorOutlineShape { | ||
|
|
||
| private readonly _proxy: MainThreadCustomEditorOutlineShape; | ||
| private readonly _providers = new Map<string, { provider: vscode.CustomEditorOutlineProvider; disposables: DisposableStore }>(); | ||
|
|
||
| constructor( | ||
| mainContext: IRPCProtocol, | ||
| ) { | ||
| this._proxy = mainContext.getProxy(MainContext.MainThreadCustomEditorOutline); | ||
| } | ||
|
|
||
| registerCustomEditorOutlineProvider( | ||
| extension: IExtensionDescription, | ||
| viewType: string, | ||
| provider: vscode.CustomEditorOutlineProvider, | ||
| ): vscode.Disposable { | ||
| checkProposedApiEnabled(extension, 'customEditorOutline'); | ||
|
|
||
| if (this._providers.has(viewType)) { | ||
| throw new Error(`An outline provider for custom editor view type '${viewType}' is already registered`); | ||
| } | ||
|
|
||
| const disposables = new DisposableStore(); | ||
|
|
||
| this._providers.set(viewType, { provider, disposables }); | ||
| this._proxy.$registerCustomEditorOutlineProvider(viewType); | ||
|
|
||
| disposables.add(provider.onDidChangeOutline(resource => { | ||
| this._proxy.$onDidChangeOutline(viewType, resource); | ||
| })); | ||
|
|
||
| disposables.add(provider.onDidChangeActiveItem(({ uri, itemId }) => { | ||
| this._proxy.$onDidChangeActiveItem(viewType, uri, itemId); | ||
| })); | ||
|
|
||
| return toDisposable(() => { | ||
| this._providers.delete(viewType); | ||
| disposables.dispose(); | ||
| this._proxy.$unregisterCustomEditorOutlineProvider(viewType); | ||
| }); | ||
| } | ||
|
|
||
| async $provideOutline(viewType: string, resource: UriComponents, token: CancellationToken): Promise<ICustomEditorOutlineItemDto[] | undefined> { | ||
| const entry = this._providers.get(viewType); | ||
| if (!entry) { | ||
| return undefined; | ||
| } | ||
| const items = await entry.provider.provideOutline(URI.revive(resource), token); | ||
| if (!items) { | ||
| return undefined; | ||
| } | ||
| return items.map(item => this._convertItem(item)); | ||
| } | ||
|
|
||
| $revealItem(viewType: string, resource: UriComponents, itemId: string): void { | ||
| const entry = this._providers.get(viewType); | ||
| if (entry) { | ||
| entry.provider.revealItem(URI.revive(resource), itemId); | ||
| } | ||
| } | ||
|
|
||
| private _convertItem(item: vscode.CustomEditorOutlineItem): ICustomEditorOutlineItemDto { | ||
| return { | ||
| id: item.id, | ||
| label: item.label, | ||
| detail: item.detail, | ||
| tooltip: item.tooltip, | ||
| icon: ThemeIcon.isThemeIcon(item.icon) ? item.icon : undefined, | ||
| contextValue: item.contextValue, | ||
| children: item.children?.map(child => this._convertItem(child)), | ||
| }; | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.