Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/notebook/common/notebookDiffEditorInput.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { IResourceDiffEditorInput, IResourceSideBySideEditorInput, isResourceDiffEditorInput, IUntypedEditorInput } from '../../../common/editor.js';
7
import { EditorInput } from '../../../common/editor/editorInput.js';
8
import { EditorModel } from '../../../common/editor/editorModel.js';
9
import { URI } from '../../../../base/common/uri.js';
10
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
11
import { INotebookDiffEditorModel, IResolvedNotebookEditorModel } from './notebookCommon.js';
12
import { DiffEditorInput } from '../../../common/editor/diffEditorInput.js';
13
import { NotebookEditorInput } from './notebookEditorInput.js';
14
import { IEditorService } from '../../../services/editor/common/editorService.js';
15
16
class NotebookDiffEditorModel extends EditorModel implements INotebookDiffEditorModel {
17
constructor(
18
readonly original: IResolvedNotebookEditorModel,
19
readonly modified: IResolvedNotebookEditorModel,
20
) {
21
super();
22
}
23
}
24
25
export class NotebookDiffEditorInput extends DiffEditorInput {
26
static create(instantiationService: IInstantiationService, resource: URI, name: string | undefined, description: string | undefined, originalResource: URI, viewType: string) {
27
const original = NotebookEditorInput.getOrCreate(instantiationService, originalResource, undefined, viewType);
28
const modified = NotebookEditorInput.getOrCreate(instantiationService, resource, undefined, viewType);
29
return instantiationService.createInstance(NotebookDiffEditorInput, name, description, original, modified, viewType);
30
}
31
32
static override readonly ID: string = 'workbench.input.diffNotebookInput';
33
34
private _modifiedTextModel: IResolvedNotebookEditorModel | null = null;
35
private _originalTextModel: IResolvedNotebookEditorModel | null = null;
36
37
override get resource() {
38
return this.modified.resource;
39
}
40
41
override get editorId() {
42
return this.viewType;
43
}
44
45
private _cachedModel: NotebookDiffEditorModel | undefined = undefined;
46
47
constructor(
48
name: string | undefined,
49
description: string | undefined,
50
override readonly original: NotebookEditorInput,
51
override readonly modified: NotebookEditorInput,
52
public readonly viewType: string,
53
@IEditorService editorService: IEditorService
54
) {
55
super(
56
name,
57
description,
58
original,
59
modified,
60
undefined,
61
editorService
62
);
63
}
64
65
override get typeId(): string {
66
return NotebookDiffEditorInput.ID;
67
}
68
69
override async resolve(): Promise<NotebookDiffEditorModel> {
70
const [originalEditorModel, modifiedEditorModel] = await Promise.all([
71
this.original.resolve(),
72
this.modified.resolve(),
73
]);
74
75
this._cachedModel?.dispose();
76
77
// TODO@rebornix check how we restore the editor in text diff editor
78
if (!modifiedEditorModel) {
79
throw new Error(`Fail to resolve modified editor model for resource ${this.modified.resource} with notebookType ${this.viewType}`);
80
}
81
82
if (!originalEditorModel) {
83
throw new Error(`Fail to resolve original editor model for resource ${this.original.resource} with notebookType ${this.viewType}`);
84
}
85
86
this._originalTextModel = originalEditorModel;
87
this._modifiedTextModel = modifiedEditorModel;
88
this._cachedModel = new NotebookDiffEditorModel(this._originalTextModel, this._modifiedTextModel);
89
return this._cachedModel;
90
}
91
92
override toUntyped(): IResourceDiffEditorInput & IResourceSideBySideEditorInput {
93
const original = { resource: this.original.resource };
94
const modified = { resource: this.resource };
95
return {
96
original,
97
modified,
98
primary: modified,
99
secondary: original,
100
options: {
101
override: this.viewType
102
}
103
};
104
}
105
106
override matches(otherInput: EditorInput | IUntypedEditorInput): boolean {
107
if (this === otherInput) {
108
return true;
109
}
110
111
if (otherInput instanceof NotebookDiffEditorInput) {
112
return this.modified.matches(otherInput.modified)
113
&& this.original.matches(otherInput.original)
114
&& this.viewType === otherInput.viewType;
115
}
116
117
if (isResourceDiffEditorInput(otherInput)) {
118
return this.modified.matches(otherInput.modified)
119
&& this.original.matches(otherInput.original)
120
&& this.editorId !== undefined
121
&& (this.editorId === otherInput.options?.override || otherInput.options?.override === undefined);
122
}
123
124
return false;
125
}
126
127
override dispose() {
128
super.dispose();
129
this._cachedModel?.dispose();
130
this._cachedModel = undefined;
131
this.original.dispose();
132
this.modified.dispose();
133
this._originalTextModel = null;
134
this._modifiedTextModel = null;
135
}
136
}
137
138