|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { CancellationToken } from '../../../base/common/cancellation.js'; |
| 7 | +import { Emitter, Event } from '../../../base/common/event.js'; |
| 8 | +import { Disposable, DisposableMap, IDisposable, toDisposable } from '../../../base/common/lifecycle.js'; |
| 9 | +import { InstantiationType, registerSingleton } from '../../../platform/instantiation/common/extensions.js'; |
| 10 | +import { extHostNamedCustomer, IExtHostContext } from '../../services/extensions/common/extHostCustomers.js'; |
| 11 | +import { ExtHostContext, ExtHostCustomEditorOutlineShape, MainContext, MainThreadCustomEditorOutlineShape } from '../common/extHost.protocol.js'; |
| 12 | +import { ICustomEditorOutlineItemDto, ICustomEditorOutlineProviderService } from '../../contrib/customEditor/common/customEditorOutlineService.js'; |
| 13 | + |
| 14 | +class CustomEditorOutlineProviderEntry { |
| 15 | + private readonly _onDidChangeOutline = new Emitter<void>(); |
| 16 | + readonly onDidChangeOutline = this._onDidChangeOutline.event; |
| 17 | + |
| 18 | + private readonly _onDidChangeActiveItem = new Emitter<string | undefined>(); |
| 19 | + readonly onDidChangeActiveItem = this._onDidChangeActiveItem.event; |
| 20 | + |
| 21 | + private _activeItemId: string | undefined; |
| 22 | + |
| 23 | + get activeItemId(): string | undefined { return this._activeItemId; } |
| 24 | + |
| 25 | + fireDidChangeOutline(): void { |
| 26 | + this._onDidChangeOutline.fire(); |
| 27 | + } |
| 28 | + |
| 29 | + fireDidChangeActiveItem(itemId: string | undefined): void { |
| 30 | + this._activeItemId = itemId; |
| 31 | + this._onDidChangeActiveItem.fire(itemId); |
| 32 | + } |
| 33 | + |
| 34 | + dispose(): void { |
| 35 | + this._onDidChangeOutline.dispose(); |
| 36 | + this._onDidChangeActiveItem.dispose(); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +class CustomEditorOutlineProviderService extends Disposable implements ICustomEditorOutlineProviderService { |
| 41 | + declare readonly _serviceBrand: undefined; |
| 42 | + |
| 43 | + private readonly _entries = this._register(new DisposableMap<string, CustomEditorOutlineProviderEntry>()); |
| 44 | + |
| 45 | + private readonly _onDidChange = this._register(new Emitter<void>()); |
| 46 | + readonly onDidChange: Event<void> = this._onDidChange.event; |
| 47 | + |
| 48 | + private _provideOutline?: (viewType: string, token: CancellationToken) => Promise<ICustomEditorOutlineItemDto[] | undefined>; |
| 49 | + private _revealItem?: (viewType: string, itemId: string) => void; |
| 50 | + |
| 51 | + setDelegate(delegate: { |
| 52 | + provideOutline: (viewType: string, token: CancellationToken) => Promise<ICustomEditorOutlineItemDto[] | undefined>; |
| 53 | + revealItem: (viewType: string, itemId: string) => void; |
| 54 | + }): void { |
| 55 | + this._provideOutline = delegate.provideOutline; |
| 56 | + this._revealItem = delegate.revealItem; |
| 57 | + } |
| 58 | + |
| 59 | + hasProvider(viewType: string): boolean { |
| 60 | + return this._entries.has(viewType); |
| 61 | + } |
| 62 | + |
| 63 | + getProviderViewTypes(): string[] { |
| 64 | + return [...this._entries.keys()]; |
| 65 | + } |
| 66 | + |
| 67 | + async provideOutline(viewType: string, token: CancellationToken): Promise<ICustomEditorOutlineItemDto[] | undefined> { |
| 68 | + if (this._provideOutline) { |
| 69 | + return this._provideOutline(viewType, token); |
| 70 | + } |
| 71 | + return undefined; |
| 72 | + } |
| 73 | + |
| 74 | + revealItem(viewType: string, itemId: string): void { |
| 75 | + if (this._revealItem) { |
| 76 | + this._revealItem(viewType, itemId); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + getActiveItemId(viewType: string): string | undefined { |
| 81 | + return this._entries.get(viewType)?.activeItemId; |
| 82 | + } |
| 83 | + |
| 84 | + onDidChangeOutline(viewType: string): Event<void> { |
| 85 | + const entry = this._entries.get(viewType); |
| 86 | + return entry ? entry.onDidChangeOutline : Event.None; |
| 87 | + } |
| 88 | + |
| 89 | + onDidChangeActiveItem(viewType: string): Event<string | undefined> { |
| 90 | + const entry = this._entries.get(viewType); |
| 91 | + return entry ? entry.onDidChangeActiveItem : Event.None; |
| 92 | + } |
| 93 | + |
| 94 | + registerProvider(viewType: string): IDisposable { |
| 95 | + const entry = new CustomEditorOutlineProviderEntry(); |
| 96 | + this._entries.set(viewType, entry); |
| 97 | + this._onDidChange.fire(); |
| 98 | + return toDisposable(() => { |
| 99 | + this._entries.deleteAndDispose(viewType); |
| 100 | + this._onDidChange.fire(); |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + unregisterProvider(viewType: string): void { |
| 105 | + this._entries.deleteAndDispose(viewType); |
| 106 | + this._onDidChange.fire(); |
| 107 | + } |
| 108 | + |
| 109 | + fireDidChangeOutline(viewType: string): void { |
| 110 | + this._entries.get(viewType)?.fireDidChangeOutline(); |
| 111 | + } |
| 112 | + |
| 113 | + fireDidChangeActiveItem(viewType: string, itemId: string | undefined): void { |
| 114 | + this._entries.get(viewType)?.fireDidChangeActiveItem(itemId); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +registerSingleton(ICustomEditorOutlineProviderService, CustomEditorOutlineProviderService, InstantiationType.Delayed); |
| 119 | + |
| 120 | +@extHostNamedCustomer(MainContext.MainThreadCustomEditorOutline) |
| 121 | +export class MainThreadCustomEditorOutline extends Disposable implements MainThreadCustomEditorOutlineShape { |
| 122 | + |
| 123 | + private readonly _proxy: ExtHostCustomEditorOutlineShape; |
| 124 | + private readonly _registrations = this._register(new DisposableMap<string>()); |
| 125 | + |
| 126 | + constructor( |
| 127 | + context: IExtHostContext, |
| 128 | + @ICustomEditorOutlineProviderService private readonly _service: ICustomEditorOutlineProviderService, |
| 129 | + ) { |
| 130 | + super(); |
| 131 | + this._proxy = context.getProxy(ExtHostContext.ExtHostCustomEditorOutline); |
| 132 | + |
| 133 | + // Wire the service delegate to call through to the ext host |
| 134 | + if (this._service instanceof CustomEditorOutlineProviderService) { |
| 135 | + this._service.setDelegate({ |
| 136 | + provideOutline: (viewType, token) => this._proxy.$provideOutline(viewType, token), |
| 137 | + revealItem: (viewType, itemId) => this._proxy.$revealItem(viewType, itemId), |
| 138 | + }); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + $registerCustomEditorOutlineProvider(viewType: string): void { |
| 143 | + const registration = this._service.registerProvider(viewType); |
| 144 | + this._registrations.set(viewType, registration); |
| 145 | + } |
| 146 | + |
| 147 | + $unregisterCustomEditorOutlineProvider(viewType: string): void { |
| 148 | + this._registrations.deleteAndDispose(viewType); |
| 149 | + this._service.unregisterProvider(viewType); |
| 150 | + } |
| 151 | + |
| 152 | + $onDidChangeOutline(viewType: string): void { |
| 153 | + this._service.fireDidChangeOutline(viewType); |
| 154 | + } |
| 155 | + |
| 156 | + $onDidChangeActiveItem(viewType: string, itemId: string | undefined): void { |
| 157 | + this._service.fireDidChangeActiveItem(viewType, itemId); |
| 158 | + } |
| 159 | +} |
0 commit comments