Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/notebook/common/notebookOutputRenderer.ts
3296 views
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 * as glob from '../../../../base/common/glob.js';
7
import { Iterable } from '../../../../base/common/iterator.js';
8
import { joinPath } from '../../../../base/common/resources.js';
9
import { URI } from '../../../../base/common/uri.js';
10
import { ExtensionIdentifier, IExtensionDescription } from '../../../../platform/extensions/common/extensions.js';
11
import { INotebookRendererInfo, ContributedNotebookRendererEntrypoint, NotebookRendererMatch, RendererMessagingSpec, NotebookRendererEntrypoint, INotebookStaticPreloadInfo as INotebookStaticPreloadInfo } from './notebookCommon.js';
12
13
class DependencyList {
14
private readonly value: ReadonlySet<string>;
15
public readonly defined: boolean;
16
17
constructor(value: Iterable<string>) {
18
this.value = new Set(value);
19
this.defined = this.value.size > 0;
20
}
21
22
/** Gets whether any of the 'available' dependencies match the ones in this list */
23
public matches(available: ReadonlyArray<string>) {
24
// For now this is simple, but this may expand to support globs later
25
// @see https://github.com/microsoft/vscode/issues/119899
26
return available.some(v => this.value.has(v));
27
}
28
}
29
30
export class NotebookOutputRendererInfo implements INotebookRendererInfo {
31
32
readonly id: string;
33
readonly entrypoint: NotebookRendererEntrypoint;
34
readonly displayName: string;
35
readonly extensionLocation: URI;
36
readonly extensionId: ExtensionIdentifier;
37
readonly hardDependencies: DependencyList;
38
readonly optionalDependencies: DependencyList;
39
readonly messaging: RendererMessagingSpec;
40
41
readonly mimeTypes: readonly string[];
42
private readonly mimeTypeGlobs: glob.ParsedPattern[];
43
44
readonly isBuiltin: boolean;
45
46
constructor(descriptor: {
47
readonly id: string;
48
readonly displayName: string;
49
readonly entrypoint: ContributedNotebookRendererEntrypoint;
50
readonly mimeTypes: readonly string[];
51
readonly extension: IExtensionDescription;
52
readonly dependencies: readonly string[] | undefined;
53
readonly optionalDependencies: readonly string[] | undefined;
54
readonly requiresMessaging: RendererMessagingSpec | undefined;
55
}) {
56
this.id = descriptor.id;
57
this.extensionId = descriptor.extension.identifier;
58
this.extensionLocation = descriptor.extension.extensionLocation;
59
this.isBuiltin = descriptor.extension.isBuiltin;
60
61
if (typeof descriptor.entrypoint === 'string') {
62
this.entrypoint = {
63
extends: undefined,
64
path: joinPath(this.extensionLocation, descriptor.entrypoint)
65
};
66
} else {
67
this.entrypoint = {
68
extends: descriptor.entrypoint.extends,
69
path: joinPath(this.extensionLocation, descriptor.entrypoint.path)
70
};
71
}
72
73
this.displayName = descriptor.displayName;
74
this.mimeTypes = descriptor.mimeTypes;
75
this.mimeTypeGlobs = this.mimeTypes.map(pattern => glob.parse(pattern));
76
this.hardDependencies = new DependencyList(descriptor.dependencies ?? Iterable.empty());
77
this.optionalDependencies = new DependencyList(descriptor.optionalDependencies ?? Iterable.empty());
78
this.messaging = descriptor.requiresMessaging ?? RendererMessagingSpec.Never;
79
}
80
81
public matchesWithoutKernel(mimeType: string) {
82
if (!this.matchesMimeTypeOnly(mimeType)) {
83
return NotebookRendererMatch.Never;
84
}
85
86
if (this.hardDependencies.defined) {
87
return NotebookRendererMatch.WithHardKernelDependency;
88
}
89
90
if (this.optionalDependencies.defined) {
91
return NotebookRendererMatch.WithOptionalKernelDependency;
92
}
93
94
return NotebookRendererMatch.Pure;
95
}
96
97
public matches(mimeType: string, kernelProvides: ReadonlyArray<string>) {
98
if (!this.matchesMimeTypeOnly(mimeType)) {
99
return NotebookRendererMatch.Never;
100
}
101
102
if (this.hardDependencies.defined) {
103
return this.hardDependencies.matches(kernelProvides)
104
? NotebookRendererMatch.WithHardKernelDependency
105
: NotebookRendererMatch.Never;
106
}
107
108
return this.optionalDependencies.matches(kernelProvides)
109
? NotebookRendererMatch.WithOptionalKernelDependency
110
: NotebookRendererMatch.Pure;
111
}
112
113
private matchesMimeTypeOnly(mimeType: string) {
114
if (this.entrypoint.extends) { // We're extending another renderer
115
return false;
116
}
117
118
return this.mimeTypeGlobs.some(pattern => pattern(mimeType)) || this.mimeTypes.some(pattern => pattern === mimeType);
119
}
120
}
121
122
export class NotebookStaticPreloadInfo implements INotebookStaticPreloadInfo {
123
124
readonly type: string;
125
readonly entrypoint: URI;
126
readonly extensionLocation: URI;
127
readonly localResourceRoots: readonly URI[];
128
129
constructor(descriptor: {
130
readonly type: string;
131
readonly entrypoint: string;
132
readonly localResourceRoots: readonly string[];
133
readonly extension: IExtensionDescription;
134
}) {
135
this.type = descriptor.type;
136
137
this.entrypoint = joinPath(descriptor.extension.extensionLocation, descriptor.entrypoint);
138
this.extensionLocation = descriptor.extension.extensionLocation;
139
this.localResourceRoots = descriptor.localResourceRoots.map(root => joinPath(descriptor.extension.extensionLocation, root));
140
}
141
}
142
143