Path: blob/main/src/vs/workbench/contrib/notebook/common/notebookDiffEditorInput.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 { IResourceDiffEditorInput, IResourceSideBySideEditorInput, isResourceDiffEditorInput, IUntypedEditorInput } from '../../../common/editor.js';6import { EditorInput } from '../../../common/editor/editorInput.js';7import { EditorModel } from '../../../common/editor/editorModel.js';8import { URI } from '../../../../base/common/uri.js';9import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';10import { INotebookDiffEditorModel, IResolvedNotebookEditorModel } from './notebookCommon.js';11import { DiffEditorInput } from '../../../common/editor/diffEditorInput.js';12import { NotebookEditorInput } from './notebookEditorInput.js';13import { IEditorService } from '../../../services/editor/common/editorService.js';1415class NotebookDiffEditorModel extends EditorModel implements INotebookDiffEditorModel {16constructor(17readonly original: IResolvedNotebookEditorModel,18readonly modified: IResolvedNotebookEditorModel,19) {20super();21}22}2324export class NotebookDiffEditorInput extends DiffEditorInput {25static create(instantiationService: IInstantiationService, resource: URI, name: string | undefined, description: string | undefined, originalResource: URI, viewType: string) {26const original = NotebookEditorInput.getOrCreate(instantiationService, originalResource, undefined, viewType);27const modified = NotebookEditorInput.getOrCreate(instantiationService, resource, undefined, viewType);28return instantiationService.createInstance(NotebookDiffEditorInput, name, description, original, modified, viewType);29}3031static override readonly ID: string = 'workbench.input.diffNotebookInput';3233private _modifiedTextModel: IResolvedNotebookEditorModel | null = null;34private _originalTextModel: IResolvedNotebookEditorModel | null = null;3536override get resource() {37return this.modified.resource;38}3940override get editorId() {41return this.viewType;42}4344private _cachedModel: NotebookDiffEditorModel | undefined = undefined;4546constructor(47name: string | undefined,48description: string | undefined,49override readonly original: NotebookEditorInput,50override readonly modified: NotebookEditorInput,51public readonly viewType: string,52@IEditorService editorService: IEditorService53) {54super(55name,56description,57original,58modified,59undefined,60editorService61);62}6364override get typeId(): string {65return NotebookDiffEditorInput.ID;66}6768override async resolve(): Promise<NotebookDiffEditorModel> {69const [originalEditorModel, modifiedEditorModel] = await Promise.all([70this.original.resolve(),71this.modified.resolve(),72]);7374this._cachedModel?.dispose();7576// TODO@rebornix check how we restore the editor in text diff editor77if (!modifiedEditorModel) {78throw new Error(`Fail to resolve modified editor model for resource ${this.modified.resource} with notebookType ${this.viewType}`);79}8081if (!originalEditorModel) {82throw new Error(`Fail to resolve original editor model for resource ${this.original.resource} with notebookType ${this.viewType}`);83}8485this._originalTextModel = originalEditorModel;86this._modifiedTextModel = modifiedEditorModel;87this._cachedModel = new NotebookDiffEditorModel(this._originalTextModel, this._modifiedTextModel);88return this._cachedModel;89}9091override toUntyped(): IResourceDiffEditorInput & IResourceSideBySideEditorInput {92const original = { resource: this.original.resource };93const modified = { resource: this.resource };94return {95original,96modified,97primary: modified,98secondary: original,99options: {100override: this.viewType101}102};103}104105override matches(otherInput: EditorInput | IUntypedEditorInput): boolean {106if (this === otherInput) {107return true;108}109110if (otherInput instanceof NotebookDiffEditorInput) {111return this.modified.matches(otherInput.modified)112&& this.original.matches(otherInput.original)113&& this.viewType === otherInput.viewType;114}115116if (isResourceDiffEditorInput(otherInput)) {117return this.modified.matches(otherInput.modified)118&& this.original.matches(otherInput.original)119&& this.editorId !== undefined120&& (this.editorId === otherInput.options?.override || otherInput.options?.override === undefined);121}122123return false;124}125126override dispose() {127super.dispose();128this._cachedModel?.dispose();129this._cachedModel = undefined;130this.original.dispose();131this.modified.dispose();132this._originalTextModel = null;133this._modifiedTextModel = null;134}135}136137138