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
5263 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' || glob.isRelativePattern(selector)) {
81
if (glob.match(selector, basename(resource.fsPath), { ignoreCase: true })) {
82
return true;
83
}
84
}
85
86
if (!isDocumentExcludePattern(selector)) {
87
return false;
88
}
89
90
const filenamePattern = selector.include;
91
const excludeFilenamePattern = selector.exclude;
92
93
if (glob.match(filenamePattern, basename(resource.fsPath), { ignoreCase: true })) {
94
if (excludeFilenamePattern) {
95
if (glob.match(excludeFilenamePattern, basename(resource.fsPath), { ignoreCase: true })) {
96
return false;
97
}
98
}
99
return true;
100
}
101
102
return false;
103
}
104
105
static possibleFileEnding(selectors: NotebookSelector[]): string | undefined {
106
for (const selector of selectors) {
107
const ending = NotebookProviderInfo._possibleFileEnding(selector);
108
if (ending) {
109
return ending;
110
}
111
}
112
return undefined;
113
}
114
115
private static _possibleFileEnding(selector: NotebookSelector): string | undefined {
116
117
const pattern = /^.*(\.[a-zA-Z0-9_-]+)$/;
118
119
let candidate: string | undefined;
120
121
if (typeof selector === 'string') {
122
candidate = selector;
123
} else if (glob.isRelativePattern(selector)) {
124
candidate = selector.pattern;
125
} else if (selector.include) {
126
return NotebookProviderInfo._possibleFileEnding(selector.include);
127
}
128
129
if (candidate) {
130
const match = pattern.exec(candidate);
131
if (match) {
132
return match[1];
133
}
134
}
135
136
return undefined;
137
}
138
}
139
140