Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/common/editor/textDiffEditorModel.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 { IDiffEditorModel } from '../../../editor/common/editorCommon.js';
7
import { BaseTextEditorModel } from './textEditorModel.js';
8
import { DiffEditorModel } from './diffEditorModel.js';
9
import { IMarkdownString } from '../../../base/common/htmlContent.js';
10
11
/**
12
* The base text editor model for the diff editor. It is made up of two text editor models, the original version
13
* and the modified version.
14
*/
15
export class TextDiffEditorModel extends DiffEditorModel {
16
17
protected override readonly _originalModel: BaseTextEditorModel | undefined;
18
override get originalModel(): BaseTextEditorModel | undefined { return this._originalModel; }
19
20
protected override readonly _modifiedModel: BaseTextEditorModel | undefined;
21
override get modifiedModel(): BaseTextEditorModel | undefined { return this._modifiedModel; }
22
23
private _textDiffEditorModel: IDiffEditorModel | undefined = undefined;
24
get textDiffEditorModel(): IDiffEditorModel | undefined { return this._textDiffEditorModel; }
25
26
constructor(originalModel: BaseTextEditorModel, modifiedModel: BaseTextEditorModel) {
27
super(originalModel, modifiedModel);
28
29
this._originalModel = originalModel;
30
this._modifiedModel = modifiedModel;
31
32
this.updateTextDiffEditorModel();
33
}
34
35
override async resolve(): Promise<void> {
36
await super.resolve();
37
38
this.updateTextDiffEditorModel();
39
}
40
41
private updateTextDiffEditorModel(): void {
42
if (this.originalModel?.isResolved() && this.modifiedModel?.isResolved()) {
43
44
// Create new
45
if (!this._textDiffEditorModel) {
46
this._textDiffEditorModel = {
47
original: this.originalModel.textEditorModel,
48
modified: this.modifiedModel.textEditorModel
49
};
50
}
51
52
// Update existing
53
else {
54
this._textDiffEditorModel.original = this.originalModel.textEditorModel;
55
this._textDiffEditorModel.modified = this.modifiedModel.textEditorModel;
56
}
57
}
58
}
59
60
override isResolved(): boolean {
61
return !!this._textDiffEditorModel;
62
}
63
64
isReadonly(): boolean | IMarkdownString {
65
return !!this.modifiedModel && this.modifiedModel.isReadonly();
66
}
67
68
override dispose(): void {
69
70
// Free the diff editor model but do not propagate the dispose() call to the two models
71
// inside. We never created the two models (original and modified) so we can not dispose
72
// them without sideeffects. Rather rely on the models getting disposed when their related
73
// inputs get disposed from the diffEditorInput.
74
this._textDiffEditorModel = undefined;
75
76
super.dispose();
77
}
78
}
79
80