-
Notifications
You must be signed in to change notification settings - Fork 30.4k
/
Copy pathnotebookProvider.ts
92 lines (79 loc) · 3.48 KB
/
notebookProvider.ts
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
89
90
91
92
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as glob from 'vs/base/common/glob';
import { URI } from 'vs/base/common/uri';
import { basename } from 'vs/base/common/path';
import { INotebookExclusiveDocumentFilter, isDocumentExcludePattern, NotebookEditorPriority } from 'vs/workbench/contrib/notebook/common/notebookCommon';
export interface NotebookSelector {
readonly filenamePattern?: string | glob.IRelativePattern | INotebookExclusiveDocumentFilter;
readonly excludeFileNamePattern?: string;
}
export interface NotebookEditorDescriptor {
readonly id: string;
readonly displayName: string;
readonly selectors: readonly NotebookSelector[];
readonly priority: NotebookEditorPriority;
readonly providerExtensionId?: string;
readonly providerDescription?: string;
readonly providerDisplayName: string;
readonly providerExtensionLocation: URI;
readonly dynamicContribution: boolean;
readonly exclusive: boolean;
}
export class NotebookProviderInfo implements NotebookEditorDescriptor {
readonly id: string;
readonly displayName: string;
readonly selectors: readonly NotebookSelector[];
readonly priority: NotebookEditorPriority;
// it's optional as the memento might not have it
readonly providerExtensionId?: string;
readonly providerDescription?: string;
readonly providerDisplayName: string;
readonly providerExtensionLocation: URI;
readonly dynamicContribution: boolean;
readonly exclusive: boolean;
constructor(descriptor: NotebookEditorDescriptor) {
this.id = descriptor.id;
this.displayName = descriptor.displayName;
this.selectors = descriptor.selectors;
this.priority = descriptor.priority;
this.providerExtensionId = descriptor.providerExtensionId;
this.providerDescription = descriptor.providerDescription;
this.providerDisplayName = descriptor.providerDisplayName;
this.providerExtensionLocation = descriptor.providerExtensionLocation;
this.dynamicContribution = descriptor.dynamicContribution;
this.exclusive = descriptor.exclusive;
}
matches(resource: URI): boolean {
return this.selectors?.some(selector => NotebookProviderInfo.selectorMatches(selector, resource));
}
static selectorMatches(selector: NotebookSelector, resource: URI): boolean {
if (!selector.filenamePattern) {
return false;
}
if (typeof selector.filenamePattern === 'string') {
if (glob.match(selector.filenamePattern.toLowerCase(), basename(resource.fsPath).toLowerCase())) {
if (selector.excludeFileNamePattern) {
if (glob.match(selector.excludeFileNamePattern.toLowerCase(), basename(resource.fsPath).toLowerCase())) {
// should exclude
return false;
}
}
return true;
}
}
let filenamePattern = isDocumentExcludePattern(selector.filenamePattern) ? selector.filenamePattern.include : (selector.filenamePattern as string | glob.IRelativePattern);
let excludeFilenamePattern = isDocumentExcludePattern(selector.filenamePattern) ? selector.filenamePattern.exclude : undefined;
if (glob.match(filenamePattern, basename(resource.fsPath).toLowerCase())) {
if (excludeFilenamePattern) {
if (glob.match(excludeFilenamePattern, basename(resource.fsPath).toLowerCase())) {
return false;
}
}
return true;
}
return false;
}
}