notebooks - use resolver for untitled factory

This commit is contained in:
Benjamin Pasero 2021-08-06 09:19:24 +02:00
parent 93a3fdfc3a
commit 9a3c8e7434
No known key found for this signature in database
GPG key ID: E6380CC4C8219E65
2 changed files with 31 additions and 24 deletions

View file

@ -32,6 +32,7 @@ import { NotebookCellTextModel } from 'vs/workbench/contrib/notebook/common/mode
import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookTextModel';
import { ACCESSIBLE_NOTEBOOK_DISPLAY_ORDER, BUILTIN_RENDERER_ID, CellUri, DisplayOrderKey, INotebookExclusiveDocumentFilter, INotebookContributionData, INotebookRendererInfo, INotebookTextModel, IOrderedMimeType, IOutputDto, mimeTypeIsAlwaysSecure, mimeTypeSupportedByCore, NotebookData, NotebookEditorPriority, NotebookRendererMatch, NotebookTextDiffEditorPreview, RENDERER_NOT_AVAILABLE, sortMimeTypes, TransientOptions } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { NotebookEditorInput } from 'vs/workbench/contrib/notebook/common/notebookEditorInput';
import { INotebookEditorModelResolverService } from 'vs/workbench/contrib/notebook/common/notebookEditorModelResolverService';
import { updateEditorTopPadding } from 'vs/workbench/contrib/notebook/common/notebookOptions';
import { NotebookOutputRendererInfo } from 'vs/workbench/contrib/notebook/common/notebookOutputRenderer';
import { NotebookEditorDescriptor, NotebookProviderInfo } from 'vs/workbench/contrib/notebook/common/notebookProvider';
@ -48,8 +49,6 @@ export class NotebookProviderInfoStore extends Disposable {
private readonly _memento: Memento;
private _handled: boolean = false;
private _untitledCounter = 1;
private readonly _contributedEditors = new Map<string, NotebookProviderInfo>();
private readonly _contributedEditorDisposables = new DisposableStore();
@ -61,6 +60,7 @@ export class NotebookProviderInfoStore extends Disposable {
@IAccessibilityService private readonly _accessibilityService: IAccessibilityService,
@IInstantiationService private readonly _instantiationService: IInstantiationService,
@IFileService private readonly _fileService: IFileService,
@INotebookEditorModelResolverService private readonly _notebookEditorModelResolverService: INotebookEditorModelResolverService
) {
super();
this._memento = new Memento(NotebookProviderInfoStore.CUSTOM_EDITORS_STORAGE_ID, storageService);
@ -169,14 +169,16 @@ export class NotebookProviderInfoStore extends Disposable {
const notebookOptions: INotebookEditorOptions = { ...options, cellOptions };
return { editor: NotebookEditorInput.create(this._instantiationService, notebookUri, notebookProviderInfo.id), options: notebookOptions };
};
const notebookUntitledEditorFactory: UntitledEditorInputFactoryFunction = ({ resource, options }) => {
if (!resource) {
resource = URI.from({
scheme: Schemas.untitled,
path: `Untitled-${this._untitledCounter++}`
});
}
return { editor: NotebookEditorInput.create(this._instantiationService, resource.with({ scheme: Schemas.untitled }), notebookProviderInfo.id), options };
const notebookUntitledEditorFactory: UntitledEditorInputFactoryFunction = async ({ resource, options }) => {
const ref = await this._notebookEditorModelResolverService.resolve({ untitledResource: resource }, notebookProviderInfo.id);
// untitled notebooks are disposed when they get saved. we should not hold a reference
// to such a disposed notebook and therefore dispose the reference as well
ref.object.notebook.onWillDispose(() => {
ref!.dispose();
});
return { editor: NotebookEditorInput.create(this._instantiationService, ref.object.resource, notebookProviderInfo.id), options };
};
const notebookDiffEditorInputFactory: DiffEditorInputFactoryFunction = ({ modified, original }) => {
return { editor: NotebookDiffEditorInput.create(this._instantiationService, modified.resource!, undefined, undefined, original.resource!, notebookProviderInfo.id) };
@ -325,7 +327,14 @@ export class NotebookService extends Disposable implements INotebookService {
declare readonly _serviceBrand: undefined;
private readonly _notebookProviders = new Map<string, ComplexNotebookProviderInfo | SimpleNotebookProviderInfo>();
private readonly _notebookProviderInfoStore: NotebookProviderInfoStore;
private _notebookProviderInfoStore: NotebookProviderInfoStore | undefined = undefined;
private get notebookProviderInfoStore(): NotebookProviderInfoStore {
if (!this._notebookProviderInfoStore) {
this._notebookProviderInfoStore = this._register(this._instantiationService.createInstance(NotebookProviderInfoStore));
}
return this._notebookProviderInfoStore;
}
private readonly _notebookRenderersInfoStore = this._instantiationService.createInstance(NotebookOutputRendererInfoStore);
private readonly _models = new ResourceMap<ModelData>();
@ -361,10 +370,6 @@ export class NotebookService extends Disposable implements INotebookService {
) {
super();
this._notebookProviderInfoStore = _instantiationService.createInstance(NotebookProviderInfoStore);
this._register(this._notebookProviderInfoStore);
notebookRendererExtensionPoint.setHandler((renderers) => {
this._notebookRenderersInfoStore.clear();
@ -453,7 +458,7 @@ export class NotebookService extends Disposable implements INotebookService {
getEditorTypes(): IEditorType[] {
return [...this._notebookProviderInfoStore].map(info => ({
return [...this.notebookProviderInfoStore].map(info => ({
id: info.id,
displayName: info.displayName,
providerDisplayName: info.providerDisplayName
@ -485,7 +490,7 @@ export class NotebookService extends Disposable implements INotebookService {
info.update({ selectors: data.filenamePattern });
const reg = this._notebookProviderInfoStore.add(info);
const reg = this.notebookProviderInfoStore.add(info);
this._onDidChangeEditorTypes.fire();
return toDisposable(() => {
@ -506,17 +511,17 @@ export class NotebookService extends Disposable implements INotebookService {
}
registerNotebookController(viewType: string, extensionData: NotebookExtensionDescription, controller: INotebookContentProvider): IDisposable {
this._notebookProviderInfoStore.get(viewType)?.update({ options: controller.options });
this.notebookProviderInfoStore.get(viewType)?.update({ options: controller.options });
return this._registerProviderData(viewType, new ComplexNotebookProviderInfo(viewType, controller, extensionData));
}
registerNotebookSerializer(viewType: string, extensionData: NotebookExtensionDescription, serializer: INotebookSerializer): IDisposable {
this._notebookProviderInfoStore.get(viewType)?.update({ options: serializer.options });
this.notebookProviderInfoStore.get(viewType)?.update({ options: serializer.options });
return this._registerProviderData(viewType, new SimpleNotebookProviderInfo(viewType, serializer, extensionData));
}
async withNotebookDataProvider(resource: URI, viewType?: string): Promise<ComplexNotebookProviderInfo | SimpleNotebookProviderInfo> {
const providers = this._notebookProviderInfoStore.getContributedNotebook(resource);
const providers = this.notebookProviderInfoStore.getContributedNotebook(resource);
// If we have a viewtype specified we want that data provider, as the resource won't always map correctly
const selected = viewType ? providers.find(p => p.id === viewType) : providers[0];
if (!selected) {
@ -640,14 +645,14 @@ export class NotebookService extends Disposable implements INotebookService {
getContributedNotebookTypes(resource?: URI): readonly NotebookProviderInfo[] {
if (resource) {
return this._notebookProviderInfoStore.getContributedNotebook(resource);
return this.notebookProviderInfoStore.getContributedNotebook(resource);
}
return [...this._notebookProviderInfoStore];
return [...this.notebookProviderInfoStore];
}
getContributedNotebookType(viewType: string): NotebookProviderInfo | undefined {
return this._notebookProviderInfoStore.get(viewType);
return this.notebookProviderInfoStore.get(viewType);
}
getNotebookProviderResourceRoots(): URI[] {

View file

@ -12,6 +12,7 @@ import { TestConfigurationService } from 'vs/platform/configuration/test/common/
import { IFileService } from 'vs/platform/files/common/files';
import { IStorageService } from 'vs/platform/storage/common/storage';
import { NotebookProviderInfoStore } from 'vs/workbench/contrib/notebook/browser/notebookServiceImpl';
import { INotebookEditorModelResolverService } from 'vs/workbench/contrib/notebook/common/notebookEditorModelResolverService';
import { NotebookProviderInfo } from 'vs/workbench/contrib/notebook/common/notebookProvider';
import { EditorResolverService } from 'vs/workbench/services/editor/browser/editorResolverService';
import { RegisteredEditorPriority } from 'vs/workbench/services/editor/common/editorResolverService';
@ -37,7 +38,8 @@ suite('NotebookProviderInfoStore', function () {
instantiationService,
new class extends mock<IFileService>() {
override canHandleResource() { return true; }
}
},
new class extends mock<INotebookEditorModelResolverService>() { }
);
const fooInfo = new NotebookProviderInfo({