Path: blob/main/src/vs/workbench/contrib/notebook/common/notebookProvider.ts
5263 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' || glob.isRelativePattern(selector)) {80if (glob.match(selector, basename(resource.fsPath), { ignoreCase: true })) {81return true;82}83}8485if (!isDocumentExcludePattern(selector)) {86return false;87}8889const filenamePattern = selector.include;90const excludeFilenamePattern = selector.exclude;9192if (glob.match(filenamePattern, basename(resource.fsPath), { ignoreCase: true })) {93if (excludeFilenamePattern) {94if (glob.match(excludeFilenamePattern, basename(resource.fsPath), { ignoreCase: true })) {95return false;96}97}98return true;99}100101return false;102}103104static possibleFileEnding(selectors: NotebookSelector[]): string | undefined {105for (const selector of selectors) {106const ending = NotebookProviderInfo._possibleFileEnding(selector);107if (ending) {108return ending;109}110}111return undefined;112}113114private static _possibleFileEnding(selector: NotebookSelector): string | undefined {115116const pattern = /^.*(\.[a-zA-Z0-9_-]+)$/;117118let candidate: string | undefined;119120if (typeof selector === 'string') {121candidate = selector;122} else if (glob.isRelativePattern(selector)) {123candidate = selector.pattern;124} else if (selector.include) {125return NotebookProviderInfo._possibleFileEnding(selector.include);126}127128if (candidate) {129const match = pattern.exec(candidate);130if (match) {131return match[1];132}133}134135return undefined;136}137}138139140