-
Notifications
You must be signed in to change notification settings - Fork 40.8k
Expand file tree
/
Copy pathextHostCustomEditorOutline.ts
More file actions
88 lines (74 loc) · 3.4 KB
/
Copy pathextHostCustomEditorOutline.ts
File metadata and controls
88 lines (74 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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)),
};
}
}