Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/notebook/common/notebookProvider.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 { URI } from '../../../../base/common/uri.js';
8
import { basename } from '../../../../base/common/path.js';
9
import { INotebookExclusiveDocumentFilter, isDocumentExcludePattern, TransientOptions } from './notebookCommon.js';
10
import { RegisteredEditorPriority } from '../../../services/editor/common/editorResolverService.js';
11
import { ExtensionIdentifier } from '../../../../platform/extensions/common/extensions.js';
12
13
type NotebookSelector = string | glob.IRelativePattern | INotebookExclusiveDocumentFilter;
14
15
export interface NotebookEditorDescriptor {
16
readonly extension?: ExtensionIdentifier;
17
readonly id: string;
18
readonly displayName: string;
19
readonly selectors: readonly { filenamePattern?: string; excludeFileNamePattern?: string }[];
20
readonly priority: RegisteredEditorPriority;
21
readonly providerDisplayName: string;
22
}
23
24
interface INotebookEditorDescriptorDto {
25
readonly _selectors: readonly NotebookSelector[];
26
}
27
28
export class NotebookProviderInfo {
29
30
readonly extension?: ExtensionIdentifier;
31
readonly id: string;
32
readonly displayName: string;
33
readonly priority: RegisteredEditorPriority;
34
readonly providerDisplayName: string;
35
36
public _selectors: NotebookSelector[];
37
get selectors() {
38
return this._selectors;
39
}
40
private _options: TransientOptions;
41
get options() {
42
return this._options;
43
}
44
45
constructor(descriptor: NotebookEditorDescriptor) {
46
this.extension = descriptor.extension;
47
this.id = descriptor.id;
48
this.displayName = descriptor.displayName;
49
this._selectors = descriptor.selectors?.map(selector => ({
50
include: selector.filenamePattern,
51
exclude: selector.excludeFileNamePattern || ''
52
}))
53
|| (descriptor as unknown as INotebookEditorDescriptorDto)._selectors
54
|| [];
55
this.priority = descriptor.priority;
56
this.providerDisplayName = descriptor.providerDisplayName;
57
this._options = {
58
transientCellMetadata: {},
59
transientDocumentMetadata: {},
60
transientOutputs: false,
61
cellContentMetadata: {}
62
};
63
}
64
65
update(args: { selectors?: NotebookSelector[]; options?: TransientOptions }) {
66
if (args.selectors) {
67
this._selectors = args.selectors;
68
}
69
70
if (args.options) {
71
this._options = args.options;
72
}
73
}
74
75
matches(resource: URI): boolean {
76
return this.selectors?.some(selector => NotebookProviderInfo.selectorMatches(selector, resource));
77
}
78
79
static selectorMatches(selector: NotebookSelector, resource: URI): boolean {
80
if (typeof selector === 'string') {
81
// filenamePattern
82
if (glob.match(selector.toLowerCase(), basename(resource.fsPath).toLowerCase())) {
83
return true;
84
}
85
}
86
87
if (glob.isRelativePattern(selector)) {
88
if (glob.match(selector, basename(resource.fsPath).toLowerCase())) {
89
return true;
90
}
91
}
92
93
if (!isDocumentExcludePattern(selector)) {
94
return false;
95
}
96
97
const filenamePattern = selector.include;
98
const excludeFilenamePattern = selector.exclude;
99
100
if (glob.match(filenamePattern, basename(resource.fsPath).toLowerCase())) {
101
if (excludeFilenamePattern) {
102
if (glob.match(excludeFilenamePattern, basename(resource.fsPath).toLowerCase())) {
103
return false;
104
}
105
}
106
return true;
107
}
108
109
return false;
110
}
111
112
static possibleFileEnding(selectors: NotebookSelector[]): string | undefined {
113
for (const selector of selectors) {
114
const ending = NotebookProviderInfo._possibleFileEnding(selector);
115
if (ending) {
116
return ending;
117
}
118
}
119
return undefined;
120
}
121
122
private static _possibleFileEnding(selector: NotebookSelector): string | undefined {
123
124
const pattern = /^.*(\.[a-zA-Z0-9_-]+)$/;
125
126
let candidate: string | undefined;
127
128
if (typeof selector === 'string') {
129
candidate = selector;
130
} else if (glob.isRelativePattern(selector)) {
131
candidate = selector.pattern;
132
} else if (selector.include) {
133
return NotebookProviderInfo._possibleFileEnding(selector.include);
134
}
135
136
if (candidate) {
137
const match = pattern.exec(candidate);
138
if (match) {
139
return match[1];
140
}
141
}
142
143
return undefined;
144
}
145
}
146
147