Skip to content

Commit 49f372e

Browse files
authored
Code cleanup (microsoft#158349)
1 parent d272040 commit 49f372e

File tree

8 files changed

+104
-86
lines changed

8 files changed

+104
-86
lines changed

src/vs/workbench/contrib/mergeEditor/browser/commands/commands.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ export class OpenResultResource extends MergeEditorAction {
209209

210210
override runWithViewModel(viewModel: MergeEditorViewModel, accessor: ServicesAccessor): void {
211211
const editorService = accessor.get(IEditorService);
212-
editorService.openEditor({ resource: viewModel.model.result.uri });
212+
editorService.openEditor({ resource: viewModel.model.resultTextModel.uri });
213213
}
214214
}
215215

@@ -369,7 +369,7 @@ export class CompareInput2WithBaseCommand extends MergeEditorAction {
369369
function mergeEditorCompare(viewModel: MergeEditorViewModel, commandService: ICommandService, inputNumber: 1 | 2) {
370370
const model = viewModel.model;
371371
const base = model.base.uri;
372-
const input = inputNumber === 1 ? model.input1.uri : model.input2.uri;
372+
const input = inputNumber === 1 ? model.input1.textModel.uri : model.input2.textModel.uri;
373373
openDiffEditor(commandService, base, input);
374374
}
375375

src/vs/workbench/contrib/mergeEditor/browser/mergeEditorInput.ts

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { DEFAULT_EDITOR_ASSOCIATION, EditorInputCapabilities, IEditorIdentifier,
1717
import { EditorInput, IEditorCloseHandler } from 'vs/workbench/common/editor/editorInput';
1818
import { AbstractTextResourceEditorInput } from 'vs/workbench/common/editor/textResourceEditorInput';
1919
import { MergeDiffComputer } from 'vs/workbench/contrib/mergeEditor/browser/model/diffComputer';
20-
import { MergeEditorModel } from 'vs/workbench/contrib/mergeEditor/browser/model/mergeEditorModel';
20+
import { InputData, MergeEditorModel } from 'vs/workbench/contrib/mergeEditor/browser/model/mergeEditorModel';
2121
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
2222
import { ILanguageSupport, ITextFileEditorModel, ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
2323
import { autorun } from 'vs/base/common/observable';
@@ -98,31 +98,45 @@ export class MergeEditorInput extends AbstractTextResourceEditorInput implements
9898
}
9999

100100
override async resolve(): Promise<MergeEditorModel> {
101-
102101
if (!this._model) {
102+
const toInputData = async (data: MergeEditorInputData): Promise<InputData> => {
103+
const ref = await this._textModelService.createModelReference(data.uri);
104+
this._store.add(ref);
105+
return {
106+
textModel: ref.object.textEditorModel,
107+
title: data.title,
108+
description: data.description,
109+
detail: data.detail,
110+
};
111+
};
112+
113+
const [
114+
base,
115+
result,
116+
input1Data,
117+
input2Data,
118+
] = await Promise.all([
119+
this._textModelService.createModelReference(this.base),
120+
this._textModelService.createModelReference(this.result),
121+
toInputData(this.input1),
122+
toInputData(this.input2),
123+
]);
103124

104-
const base = await this._textModelService.createModelReference(this.base);
105-
const input1 = await this._textModelService.createModelReference(this.input1.uri);
106-
const input2 = await this._textModelService.createModelReference(this.input2.uri);
107-
const result = await this._textModelService.createModelReference(this.result);
125+
this._store.add(base);
126+
this._store.add(result);
108127

109128
this._model = this._instaService.createInstance(
110129
MergeEditorModel,
111130
base.object.textEditorModel,
112-
input1.object.textEditorModel,
113-
this.input1.title,
114-
this.input1.detail,
115-
this.input1.description,
116-
input2.object.textEditorModel,
117-
this.input2.title,
118-
this.input2.detail,
119-
this.input2.description,
131+
input1Data,
132+
input2Data,
120133
result.object.textEditorModel,
121134
this._instaService.createInstance(MergeDiffComputer, this._instaService.createInstance(WorkerBasedDocumentDiffProvider)),
122135
{
123136
resetUnknownOnInitialization: true
124137
},
125138
);
139+
this._store.add(this._model);
126140

127141
// set/unset the closeHandler whenever unhandled conflicts are detected
128142
const closeHandler = this._instaService.createInstance(MergeEditorCloseHandler, this._model);
@@ -133,11 +147,6 @@ export class MergeEditorInput extends AbstractTextResourceEditorInput implements
133147

134148
await this._model.onInitialized;
135149

136-
this._store.add(this._model);
137-
this._store.add(base);
138-
this._store.add(input1);
139-
this._store.add(input2);
140-
this._store.add(result);
141150
}
142151

143152
return this._model;

src/vs/workbench/contrib/mergeEditor/browser/model/mergeEditorModel.ts

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,10 @@ import { TextModelDiffChangeReason, TextModelDiffs, TextModelDiffState } from 'v
1717
import { leftJoin } from 'vs/workbench/contrib/mergeEditor/browser/utils';
1818
import { ModifiedBaseRange, ModifiedBaseRangeState } from './modifiedBaseRange';
1919

20-
export const enum MergeEditorModelState {
21-
initializing = 1,
22-
upToDate = 2,
23-
updating = 3,
24-
}
25-
2620
export class MergeEditorModel extends EditorModel {
27-
private readonly input1TextModelDiffs = this._register(new TextModelDiffs(this.base, this.input1, this.diffComputer));
28-
private readonly input2TextModelDiffs = this._register(new TextModelDiffs(this.base, this.input2, this.diffComputer));
29-
private readonly resultTextModelDiffs = this._register(new TextModelDiffs(this.base, this.result, this.diffComputer));
21+
private readonly input1TextModelDiffs = this._register(new TextModelDiffs(this.base, this.input1.textModel, this.diffComputer));
22+
private readonly input2TextModelDiffs = this._register(new TextModelDiffs(this.base, this.input2.textModel, this.diffComputer));
23+
private readonly resultTextModelDiffs = this._register(new TextModelDiffs(this.base, this.resultTextModel, this.diffComputer));
3024

3125
public readonly state = derived('state', reader => {
3226
const states = [
@@ -52,7 +46,7 @@ export class MergeEditorModel extends EditorModel {
5246
const input1Diffs = this.input1TextModelDiffs.diffs.read(reader);
5347
const input2Diffs = this.input2TextModelDiffs.diffs.read(reader);
5448

55-
return ModifiedBaseRange.fromDiffs(input1Diffs, input2Diffs, this.base, this.input1, this.input2);
49+
return ModifiedBaseRange.fromDiffs(input1Diffs, input2Diffs, this.base, this.input1.textModel, this.input2.textModel);
5650
});
5751

5852
public readonly input1LinesDiffs = this.input1TextModelDiffs.diffs;
@@ -88,7 +82,7 @@ export class MergeEditorModel extends EditorModel {
8882

8983
public readonly input1ResultMapping = derived('input1ResultMapping', reader => {
9084
const resultDiffs = this.resultDiffs.read(reader);
91-
const modifiedBaseRanges = DocumentMapping.betweenOutputs(this.input1LinesDiffs.read(reader), resultDiffs, this.input1.getLineCount());
85+
const modifiedBaseRanges = DocumentMapping.betweenOutputs(this.input1LinesDiffs.read(reader), resultDiffs, this.input1.textModel.getLineCount());
9286

9387
return new DocumentMapping(
9488
modifiedBaseRanges.lineRangeMappings.map((m) =>
@@ -105,7 +99,7 @@ export class MergeEditorModel extends EditorModel {
10599

106100
public readonly input2ResultMapping = derived('input2ResultMapping', reader => {
107101
const resultDiffs = this.resultDiffs.read(reader);
108-
const modifiedBaseRanges = DocumentMapping.betweenOutputs(this.input2LinesDiffs.read(reader), resultDiffs, this.input2.getLineCount());
102+
const modifiedBaseRanges = DocumentMapping.betweenOutputs(this.input2LinesDiffs.read(reader), resultDiffs, this.input2.textModel.getLineCount());
109103

110104
return new DocumentMapping(
111105
modifiedBaseRanges.lineRangeMappings.map((m) =>
@@ -135,28 +129,22 @@ export class MergeEditorModel extends EditorModel {
135129
}
136130

137131
public discardMergeChanges(): void {
138-
this.result.setValue(this.resultSnapshot);
132+
this.resultTextModel.setValue(this.resultSnapshot);
139133
}
140134

141135
constructor(
142136
readonly base: ITextModel,
143-
readonly input1: ITextModel,
144-
readonly input1Title: string | undefined,
145-
readonly input1Detail: string | undefined,
146-
readonly input1Description: string | undefined,
147-
readonly input2: ITextModel,
148-
readonly input2Title: string | undefined,
149-
readonly input2Detail: string | undefined,
150-
readonly input2Description: string | undefined,
151-
readonly result: ITextModel,
137+
readonly input1: InputData,
138+
readonly input2: InputData,
139+
readonly resultTextModel: ITextModel,
152140
private readonly diffComputer: IMergeDiffComputer,
153141
options: { resetUnknownOnInitialization: boolean },
154142
@IModelService private readonly modelService: IModelService,
155143
@ILanguageService private readonly languageService: ILanguageService,
156144
) {
157145
super();
158146

159-
this.resultSnapshot = result.createSnapshot();
147+
this.resultSnapshot = resultTextModel.createSnapshot();
160148
this._register(keepAlive(this.modifiedBaseRangeStateStores));
161149
this._register(keepAlive(this.modifiedBaseRangeHandlingStateStores));
162150
this._register(keepAlive(this.input1ResultMapping));
@@ -338,7 +326,7 @@ export class MergeEditorModel extends EditorModel {
338326
const { edit } = baseRange.getEditForBase(s);
339327
if (edit) {
340328
const resultRange = this.resultTextModelDiffs.getResultRange(baseRange.baseRange);
341-
const existingLines = resultRange.getLines(this.result);
329+
const existingLines = resultRange.getLines(this.resultTextModel);
342330

343331
if (equals(edit.newLines, existingLines, (a, b) => a === b)) {
344332
return s;
@@ -360,8 +348,21 @@ export class MergeEditorModel extends EditorModel {
360348
public setLanguageId(languageId: string): void {
361349
const language = this.languageService.createById(languageId);
362350
this.modelService.setMode(this.base, language);
363-
this.modelService.setMode(this.input1, language);
364-
this.modelService.setMode(this.input2, language);
365-
this.modelService.setMode(this.result, language);
351+
this.modelService.setMode(this.input1.textModel, language);
352+
this.modelService.setMode(this.input2.textModel, language);
353+
this.modelService.setMode(this.resultTextModel, language);
366354
}
367355
}
356+
357+
export interface InputData {
358+
readonly textModel: ITextModel;
359+
readonly title: string | undefined;
360+
readonly detail: string | undefined;
361+
readonly description: string | undefined;
362+
}
363+
364+
export const enum MergeEditorModelState {
365+
initializing = 1,
366+
upToDate = 2,
367+
updating = 3,
368+
}

src/vs/workbench/contrib/mergeEditor/browser/view/editors/codeEditorView.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import { IObservable, observableFromEvent, observableValue, transaction } from '
1212
import { IEditorContributionDescription } from 'vs/editor/browser/editorExtensions';
1313
import { CodeEditorWidget } from 'vs/editor/browser/widget/codeEditorWidget';
1414
import { IEditorOptions } from 'vs/editor/common/config/editorOptions';
15-
import { ITextModel } from 'vs/editor/common/model';
1615
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
1716
import { DEFAULT_EDITOR_MAX_DIMENSIONS, DEFAULT_EDITOR_MIN_DIMENSIONS } from 'vs/workbench/browser/parts/editor/editor';
17+
import { InputData } from 'vs/workbench/contrib/mergeEditor/browser/model/mergeEditorModel';
1818
import { setStyle } from 'vs/workbench/contrib/mergeEditor/browser/utils';
1919
import { MergeEditorViewModel } from 'vs/workbench/contrib/mergeEditor/browser/view/viewModel';
2020

@@ -96,16 +96,13 @@ export abstract class CodeEditorView extends Disposable {
9696

9797
public setModel(
9898
viewModel: MergeEditorViewModel,
99-
textModel: ITextModel,
100-
title: string,
101-
description: string | undefined,
102-
detail: string | undefined
99+
inputData: InputData
103100
): void {
104-
this.editor.setModel(textModel);
101+
this.editor.setModel(inputData.textModel);
105102

106-
reset(this.htmlElements.title, ...renderLabelWithIcons(title));
107-
reset(this.htmlElements.description, ...(description ? renderLabelWithIcons(description) : []));
108-
reset(this.htmlElements.detail, ...(detail ? renderLabelWithIcons(detail) : []));
103+
reset(this.htmlElements.title, ...renderLabelWithIcons(inputData.title || ''));
104+
reset(this.htmlElements.description, ...(inputData.description ? renderLabelWithIcons(inputData.description) : []));
105+
reset(this.htmlElements.detail, ...(inputData.detail ? renderLabelWithIcons(inputData.detail) : []));
109106

110107
transaction(tx => {
111108
/** @description CodeEditorView: Set Model */

src/vs/workbench/contrib/mergeEditor/browser/view/editors/inputCodeEditorView.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,15 @@ export class InputCodeEditorView extends CodeEditorView {
176176
baseRange.input1Diffs.length > 0
177177
? action(
178178
'mergeEditor.acceptInput1',
179-
localize('mergeEditor.accept', 'Accept {0}', model.input1Title),
179+
localize('mergeEditor.accept', 'Accept {0}', model.input1.title),
180180
state.toggle(1),
181181
state.input1
182182
)
183183
: undefined,
184184
baseRange.input2Diffs.length > 0
185185
? action(
186186
'mergeEditor.acceptInput2',
187-
localize('mergeEditor.accept', 'Accept {0}', model.input2Title),
187+
localize('mergeEditor.accept', 'Accept {0}', model.input2.title),
188188
state.toggle(2),
189189
state.input2
190190
)

src/vs/workbench/contrib/mergeEditor/browser/view/mergeEditor.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -305,12 +305,19 @@ export class MergeEditor extends AbstractTextEditor<IMergeEditorViewState> {
305305

306306
const viewModel = new MergeEditorViewModel(model, this.input1View, this.input2View, this.inputResultView);
307307

308-
this.input1View.setModel(viewModel, model.input1, model.input1Title || localize('input1', 'Input 1'), model.input1Detail, model.input1Description);
309-
this.input2View.setModel(viewModel, model.input2, model.input2Title || localize('input2', 'Input 2',), model.input2Detail, model.input2Description);
310-
this.inputResultView.setModel(viewModel, model.result, localize('result', 'Result',), this._labelService.getUriLabel(model.result.uri, { relative: true }), undefined);
308+
this.input1View.setModel(viewModel, { ...model.input1, title: model.input1.title || localize('input1', 'Input 1') });
309+
this.input1View.setModel(viewModel, { ...model.input2, title: model.input2.title || localize('input2', 'Input 2') });
310+
this.inputResultView.setModel(viewModel,
311+
{
312+
textModel: model.resultTextModel,
313+
title: localize('result', 'Result'),
314+
description: this._labelService.getUriLabel(model.resultTextModel.uri, { relative: true }),
315+
detail: undefined,
316+
},
317+
);
311318

312319
// Set/unset context keys based on input
313-
this._ctxResultUri.set(model.result.uri.toString());
320+
this._ctxResultUri.set(model.resultTextModel.uri.toString());
314321
this._ctxBaseUri.set(model.base.uri.toString());
315322
this._sessionDisposables.add(toDisposable(() => {
316323
this._ctxBaseUri.reset();
@@ -391,8 +398,8 @@ export class MergeEditor extends AbstractTextEditor<IMergeEditorViewState> {
391398

392399
private *baseInput1Input2() {
393400
yield model.base;
394-
yield model.input1;
395-
yield model.input2;
401+
yield model.input1.textModel;
402+
yield model.input2.textModel;
396403
}
397404

398405
private _checkBaseInput1Input2AllEmpty() {
@@ -501,7 +508,7 @@ export class MergeEditor extends AbstractTextEditor<IMergeEditorViewState> {
501508
}
502509

503510
protected computeEditorViewState(resource: URI): IMergeEditorViewState | undefined {
504-
if (!isEqual(this.model?.result.uri, resource)) {
511+
if (!isEqual(this.model?.resultTextModel.uri, resource)) {
505512
return undefined;
506513
}
507514
const result = this.inputResultView.editor.saveViewState();

src/vs/workbench/contrib/mergeEditor/electron-sandbox/devCommands.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ export class MergeEditorCopyContentsToJSON extends Action2 {
6666
return;
6767
}
6868
const contents: MergeEditorContents = {
69-
languageId: model.result.getLanguageId(),
69+
languageId: model.resultTextModel.getLanguageId(),
7070
base: model.base.getValue(),
71-
input1: model.input1.getValue(),
72-
input2: model.input2.getValue(),
73-
result: model.result.getValue(),
71+
input1: model.input1.textModel.getValue(),
72+
input2: model.input2.textModel.getValue(),
73+
result: model.resultTextModel.getValue(),
7474
initialResult: model.getInitialResultValue(),
7575
};
7676
const jsonStr = JSON.stringify(contents, undefined, 4);
@@ -211,17 +211,17 @@ export class MergeEditorSaveContentsToFolder extends Action2 {
211211
}
212212
const targetDir = result[0];
213213

214-
const extension = languageService.getExtensions(model.result.getLanguageId())[0] || '';
214+
const extension = languageService.getExtensions(model.resultTextModel.getLanguageId())[0] || '';
215215

216216
async function write(fileName: string, source: string) {
217217
await fileService.writeFile(URI.joinPath(targetDir, fileName + extension), VSBuffer.fromString(source), {});
218218
}
219219

220220
await Promise.all([
221221
write('base', model.base.getValue()),
222-
write('input1', model.input1.getValue()),
223-
write('input2', model.input2.getValue()),
224-
write('result', model.result.getValue()),
222+
write('input1', model.input1.textModel.getValue()),
223+
write('input2', model.input2.textModel.getValue()),
224+
write('result', model.resultTextModel.getValue()),
225225
write('initialResult', model.getInitialResultValue()),
226226
]);
227227

0 commit comments

Comments
 (0)