textDiffEditorModel.ts 2.6 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

J
Johannes Rieken 已提交
6 7 8 9
import { IDiffEditorModel } from 'vs/editor/common/editorCommon';
import { EditorModel } from 'vs/workbench/common/editor';
import { BaseTextEditorModel } from 'vs/workbench/common/editor/textEditorModel';
import { DiffEditorModel } from 'vs/workbench/common/editor/diffEditorModel';
E
Erich Gamma 已提交
10 11 12 13 14 15

/**
 * The base text editor model for the diff editor. It is made up of two text editor models, the original version
 * and the modified version.
 */
export class TextDiffEditorModel extends DiffEditorModel {
16 17 18 19

	protected readonly _originalModel: BaseTextEditorModel;
	protected readonly _modifiedModel: BaseTextEditorModel;

20
	private _textDiffEditorModel: IDiffEditorModel | null;
E
Erich Gamma 已提交
21 22 23 24 25 26 27

	constructor(originalModel: BaseTextEditorModel, modifiedModel: BaseTextEditorModel) {
		super(originalModel, modifiedModel);

		this.updateTextDiffEditorModel();
	}

J
Joao Moreno 已提交
28
	get originalModel(): BaseTextEditorModel {
29
		return this._originalModel;
J
Joao Moreno 已提交
30 31 32
	}

	get modifiedModel(): BaseTextEditorModel {
33
		return this._modifiedModel;
J
Joao Moreno 已提交
34 35
	}

36 37
	async load(): Promise<EditorModel> {
		await super.load();
E
Erich Gamma 已提交
38

39 40 41
		this.updateTextDiffEditorModel();

		return this;
E
Erich Gamma 已提交
42 43 44 45 46 47 48 49
	}

	private updateTextDiffEditorModel(): void {
		if (this.originalModel.isResolved() && this.modifiedModel.isResolved()) {

			// Create new
			if (!this._textDiffEditorModel) {
				this._textDiffEditorModel = {
J
Joao Moreno 已提交
50 51
					original: this.originalModel.textEditorModel,
					modified: this.modifiedModel.textEditorModel
E
Erich Gamma 已提交
52 53 54 55 56
				};
			}

			// Update existing
			else {
J
Joao Moreno 已提交
57 58
				this._textDiffEditorModel.original = this.originalModel.textEditorModel;
				this._textDiffEditorModel.modified = this.modifiedModel.textEditorModel;
E
Erich Gamma 已提交
59 60 61 62
			}
		}
	}

63
	get textDiffEditorModel(): IDiffEditorModel | null {
E
Erich Gamma 已提交
64 65 66
		return this._textDiffEditorModel;
	}

B
Benjamin Pasero 已提交
67
	isResolved(): boolean {
E
Erich Gamma 已提交
68 69 70
		return !!this._textDiffEditorModel;
	}

B
Benjamin Pasero 已提交
71
	isReadonly(): boolean {
72 73 74
		return this.modifiedModel.isReadonly();
	}

B
Benjamin Pasero 已提交
75
	dispose(): void {
E
Erich Gamma 已提交
76 77 78 79 80 81 82 83 84

		// Free the diff editor model but do not propagate the dispose() call to the two models
		// inside. We never created the two models (original and modified) so we can not dispose
		// them without sideeffects. Rather rely on the models getting disposed when their related
		// inputs get disposed from the diffEditorInput.
		this._textDiffEditorModel = null;

		super.dispose();
	}
85
}