1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { localize } from 'vs/nls';
import { AbstractSideBySideEditorInputSerializer, SideBySideEditorInput } from 'vs/workbench/common/editor/sideBySideEditorInput';
import { EditorInput } from 'vs/workbench/common/editor/editorInput';
import { EditorModel } from 'vs/workbench/common/editor/editorModel';
import { TEXT_DIFF_EDITOR_ID, BINARY_DIFF_EDITOR_ID, Verbosity, IEditorDescriptor, IEditorPane, GroupIdentifier, IResourceDiffEditorInput, IUntypedEditorInput, isResourceDiffEditorInput, IDiffEditorInput, IResourceSideBySideEditorInput, EditorInputCapabilities } from 'vs/workbench/common/editor';
import { BaseTextEditorModel } from 'vs/workbench/common/editor/textEditorModel';
import { DiffEditorModel } from 'vs/workbench/common/editor/diffEditorModel';
import { TextDiffEditorModel } from 'vs/workbench/common/editor/textDiffEditorModel';
import { withNullAsUndefined } from 'vs/base/common/types';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { shorten } from 'vs/base/common/labels';
interface IDiffEditorInputLabels {
name: string;
shortDescription: string | undefined;
mediumDescription: string | undefined;
longDescription: string | undefined;
forceDescription: boolean;
shortTitle: string;
mediumTitle: string;
longTitle: string;
}
/**
* The base editor input for the diff editor. It is made up of two editor inputs, the original version
* and the modified version.
*/
export class DiffEditorInput extends SideBySideEditorInput implements IDiffEditorInput {
static override readonly ID: string = 'workbench.editors.diffEditorInput';
override get typeId(): string {
return DiffEditorInput.ID;
}
override get editorId(): string | undefined {
return this.modified.editorId === this.original.editorId ? this.modified.editorId : undefined;
}
override get capabilities(): EditorInputCapabilities {
let capabilities = super.capabilities;
// Force description capability depends on labels
if (this.labels.forceDescription) {
capabilities |= EditorInputCapabilities.ForceDescription;
}
return capabilities;
}
private cachedModel: DiffEditorModel | undefined = undefined;
private readonly labels = this.computeLabels();
constructor(
preferredName: string | undefined,
preferredDescription: string | undefined,
readonly original: EditorInput,
readonly modified: EditorInput,
private readonly forceOpenAsBinary: boolean | undefined,
@IEditorService editorService: IEditorService
) {
super(preferredName, preferredDescription, original, modified, editorService);
}
private computeLabels(): IDiffEditorInputLabels {
// Name
let name: string;
let forceDescription = false;
if (this.preferredName) {
name = this.preferredName;
} else {
const originalName = this.original.getName();
const modifiedName = this.modified.getName();
name = localize('sideBySideLabels', "{0} ↔ {1}", originalName, modifiedName);
// Enforce description when the names are identical
forceDescription = originalName === modifiedName;
}
// Description
let shortDescription: string | undefined;
let mediumDescription: string | undefined;
let longDescription: string | undefined;
if (this.preferredDescription) {
shortDescription = this.preferredDescription;
mediumDescription = this.preferredDescription;
longDescription = this.preferredDescription;
} else {
shortDescription = this.computeLabel(this.original.getDescription(Verbosity.SHORT), this.modified.getDescription(Verbosity.SHORT));
longDescription = this.computeLabel(this.original.getDescription(Verbosity.LONG), this.modified.getDescription(Verbosity.LONG));
// Medium Description: try to be verbose by computing
// a label that resembles the difference between the two
const originalMediumDescription = this.original.getDescription(Verbosity.MEDIUM);
const modifiedMediumDescription = this.modified.getDescription(Verbosity.MEDIUM);
if (
(typeof originalMediumDescription === 'string' && typeof modifiedMediumDescription === 'string') && // we can only `shorten` when both sides are strings...
(originalMediumDescription || modifiedMediumDescription) // ...however never when both sides are empty strings
) {
const [shortenedOriginalMediumDescription, shortenedModifiedMediumDescription] = shorten([originalMediumDescription, modifiedMediumDescription]);
mediumDescription = this.computeLabel(shortenedOriginalMediumDescription, shortenedModifiedMediumDescription);
}
}
// Title
const shortTitle = this.computeLabel(this.original.getTitle(Verbosity.SHORT) ?? this.original.getName(), this.modified.getTitle(Verbosity.SHORT) ?? this.modified.getName(), ' ↔ ');
const mediumTitle = this.computeLabel(this.original.getTitle(Verbosity.MEDIUM) ?? this.original.getName(), this.modified.getTitle(Verbosity.MEDIUM) ?? this.modified.getName(), ' ↔ ');
const longTitle = this.computeLabel(this.original.getTitle(Verbosity.LONG) ?? this.original.getName(), this.modified.getTitle(Verbosity.LONG) ?? this.modified.getName(), ' ↔ ');
return { name, shortDescription, mediumDescription, longDescription, forceDescription, shortTitle, mediumTitle, longTitle };
}
private computeLabel(originalLabel: string, modifiedLabel: string, separator?: string): string;
private computeLabel(originalLabel: string | undefined, modifiedLabel: string | undefined, separator?: string): string | undefined;
private computeLabel(originalLabel: string | undefined, modifiedLabel: string | undefined, separator = ' - '): string | undefined {
if (!originalLabel || !modifiedLabel) {
return undefined;
}
if (originalLabel === modifiedLabel) {
return modifiedLabel;
}
return `${originalLabel}${separator}${modifiedLabel}`;
}
override getName(): string {
return this.labels.name;
}
override getDescription(verbosity = Verbosity.MEDIUM): string | undefined {
switch (verbosity) {
case Verbosity.SHORT:
return this.labels.shortDescription;
case Verbosity.LONG:
return this.labels.longDescription;
case Verbosity.MEDIUM:
default:
return this.labels.mediumDescription;
}
}
override getTitle(verbosity?: Verbosity): string {
switch (verbosity) {
case Verbosity.SHORT:
return this.labels.shortTitle;
case Verbosity.LONG:
return this.labels.longTitle;
default:
case Verbosity.MEDIUM:
return this.labels.mediumTitle;
}
}
override async resolve(): Promise<EditorModel> {
// Create Model - we never reuse our cached model if refresh is true because we cannot
// decide for the inputs within if the cached model can be reused or not. There may be
// inputs that need to be loaded again and thus we always recreate the model and dispose
// the previous one - if any.
const resolvedModel = await this.createModel();
if (this.cachedModel) {
this.cachedModel.dispose();
}
this.cachedModel = resolvedModel;
return this.cachedModel;
}
override prefersEditorPane<T extends IEditorDescriptor<IEditorPane>>(editorPanes: T[]): T | undefined {
if (this.forceOpenAsBinary) {
return editorPanes.find(editorPane => editorPane.typeId === BINARY_DIFF_EDITOR_ID);
}
return editorPanes.find(editorPane => editorPane.typeId === TEXT_DIFF_EDITOR_ID);
}
private async createModel(): Promise<DiffEditorModel> {
// Join resolve call over two inputs and build diff editor model
const [originalEditorModel, modifiedEditorModel] = await Promise.all([
this.original.resolve(),
this.modified.resolve()
]);
// If both are text models, return textdiffeditor model
if (modifiedEditorModel instanceof BaseTextEditorModel && originalEditorModel instanceof BaseTextEditorModel) {
return new TextDiffEditorModel(originalEditorModel, modifiedEditorModel);
}
// Otherwise return normal diff model
return new DiffEditorModel(withNullAsUndefined(originalEditorModel), withNullAsUndefined(modifiedEditorModel));
}
override toUntyped(options?: { preserveViewState: GroupIdentifier }): (IResourceDiffEditorInput & IResourceSideBySideEditorInput) | undefined {
const untyped = super.toUntyped(options);
if (untyped) {
return {
...untyped,
modified: untyped.primary,
original: untyped.secondary
};
}
return undefined;
}
override matches(otherInput: EditorInput | IUntypedEditorInput): boolean {
if (this === otherInput) {
return true;
}
if (otherInput instanceof DiffEditorInput) {
return this.modified.matches(otherInput.modified) && this.original.matches(otherInput.original) && otherInput.forceOpenAsBinary === this.forceOpenAsBinary;
}
if (isResourceDiffEditorInput(otherInput)) {
return this.modified.matches(otherInput.modified) && this.original.matches(otherInput.original);
}
return false;
}
override dispose(): void {
// Free the diff editor model but do not propagate the dispose() call to the two inputs
// We never created the two inputs (original and modified) so we can not dispose
// them without sideeffects.
if (this.cachedModel) {
this.cachedModel.dispose();
this.cachedModel = undefined;
}
super.dispose();
}
}
export class DiffEditorInputSerializer extends AbstractSideBySideEditorInputSerializer {
protected createEditorInput(instantiationService: IInstantiationService, name: string | undefined, description: string | undefined, secondaryInput: EditorInput, primaryInput: EditorInput): EditorInput {
return instantiationService.createInstance(DiffEditorInput, name, description, secondaryInput, primaryInput, undefined);
}
}
|