Path: blob/main/src/vs/workbench/contrib/notebook/common/notebookProvider.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import * as glob from '../../../../base/common/glob.js';6import { URI } from '../../../../base/common/uri.js';7import { basename } from '../../../../base/common/path.js';8import { INotebookExclusiveDocumentFilter, isDocumentExcludePattern, TransientOptions } from './notebookCommon.js';9import { RegisteredEditorPriority } from '../../../services/editor/common/editorResolverService.js';10import { ExtensionIdentifier } from '../../../../platform/extensions/common/extensions.js';1112type NotebookSelector = string | glob.IRelativePattern | INotebookExclusiveDocumentFilter;1314export interface NotebookEditorDescriptor {15readonly extension?: ExtensionIdentifier;16readonly id: string;17readonly displayName: string;18readonly selectors: readonly { filenamePattern?: string; excludeFileNamePattern?: string }[];19readonly priority: RegisteredEditorPriority;20readonly providerDisplayName: string;21}2223interface INotebookEditorDescriptorDto {24readonly _selectors: readonly NotebookSelector[];25}2627export class NotebookProviderInfo {2829readonly extension?: ExtensionIdentifier;30readonly id: string;31readonly displayName: string;32readonly priority: RegisteredEditorPriority;33readonly providerDisplayName: string;3435public _selectors: NotebookSelector[];36get selectors() {37return this._selectors;38}39private _options: TransientOptions;40get options() {41return this._options;42}4344constructor(descriptor: NotebookEditorDescriptor) {45this.extension = descriptor.extension;46this.id = descriptor.id;47this.displayName = descriptor.displayName;48this._selectors = descriptor.selectors?.map(selector => ({49include: selector.filenamePattern,50exclude: selector.excludeFileNamePattern || ''51}))52|| (descriptor as unknown as INotebookEditorDescriptorDto)._selectors53|| [];54this.priority = descriptor.priority;55this.providerDisplayName = descriptor.providerDisplayName;56this._options = {57transientCellMetadata: {},58transientDocumentMetadata: {},59transientOutputs: false,60cellContentMetadata: {}61};62}6364update(args: { selectors?: NotebookSelector[]; options?: TransientOptions }) {65if (args.selectors) {66this._selectors = args.selectors;67}6869if (args.options) {70this._options = args.options;71}72}7374matches(resource: URI): boolean {75return this.selectors?.some(selector => NotebookProviderInfo.selectorMatches(selector, resource));76}7778static selectorMatches(selector: NotebookSelector, resource: URI): boolean {79if (typeof selector === 'string') {80// filenamePattern81if (glob.match(selector.toLowerCase(), basename(resource.fsPath).toLowerCase())) {82return true;83}84}8586if (glob.isRelativePattern(selector)) {87if (glob.match(selector, basename(resource.fsPath).toLowerCase())) {88return true;89}90}9192if (!isDocumentExcludePattern(selector)) {93return false;94}9596const filenamePattern = selector.include;97const excludeFilenamePattern = selector.exclude;9899if (glob.match(filenamePattern, basename(resource.fsPath).toLowerCase())) {100if (excludeFilenamePattern) {101if (glob.match(excludeFilenamePattern, basename(resource.fsPath).toLowerCase())) {102return false;103}104}105return true;106}107108return false;109}110111static possibleFileEnding(selectors: NotebookSelector[]): string | undefined {112for (const selector of selectors) {113const ending = NotebookProviderInfo._possibleFileEnding(selector);114if (ending) {115return ending;116}117}118return undefined;119}120121private static _possibleFileEnding(selector: NotebookSelector): string | undefined {122123const pattern = /^.*(\.[a-zA-Z0-9_-]+)$/;124125let candidate: string | undefined;126127if (typeof selector === 'string') {128candidate = selector;129} else if (glob.isRelativePattern(selector)) {130candidate = selector.pattern;131} else if (selector.include) {132return NotebookProviderInfo._possibleFileEnding(selector.include);133}134135if (candidate) {136const match = pattern.exec(candidate);137if (match) {138return match[1];139}140}141142return undefined;143}144}145146147