Skip to content

Commit 2509cd5

Browse files
authored
implement tracksEditorViewState and computeEditorViewState (microsoft#153315)
fixes microsoft#150804
1 parent ade80ca commit 2509cd5

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

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

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import 'vs/css!./media/mergeEditor';
1717
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
1818
import { CodeEditorWidget } from 'vs/editor/browser/widget/codeEditorWidget';
1919
import { IEditorOptions as ICodeEditorOptions } from 'vs/editor/common/config/editorOptions';
20-
import { ScrollType } from 'vs/editor/common/editorCommon';
20+
import { ICodeEditorViewState, ScrollType } from 'vs/editor/common/editorCommon';
2121
import { ITextResourceConfigurationService } from 'vs/editor/common/services/textResourceConfiguration';
2222
import { localize } from 'vs/nls';
2323
import { createAndFillInActionBarActions } from 'vs/platform/actions/browser/menuEntryActionViewItem';
@@ -83,7 +83,7 @@ class MergeEditorLayout {
8383
}
8484
}
8585

86-
export class MergeEditor extends AbstractTextEditor<any> {
86+
export class MergeEditor extends AbstractTextEditor<IMergeEditorViewState> {
8787

8888
static readonly ID = 'mergeEditor';
8989

@@ -282,6 +282,9 @@ export class MergeEditor extends AbstractTextEditor<any> {
282282
this.inputResultView.setModel(viewModel, model.result, localize('result', 'Result',), this._labelService.getUriLabel(model.result.uri, { relative: true }), undefined);
283283
this._ctxBaseResourceScheme.set(model.base.uri.scheme);
284284

285+
const viewState = this.loadEditorViewState(input, context);
286+
this._applyViewState(viewState);
287+
285288
this._sessionDisposables.add(thenIfNotDisposed(model.onInitialized, () => {
286289
const firstConflict = model.modifiedBaseRanges.get().find(r => r.isConflicting);
287290
if (!firstConflict) {
@@ -439,17 +442,50 @@ export class MergeEditor extends AbstractTextEditor<any> {
439442
this._ctxUsesColumnLayout.set(newValue);
440443
}
441444

442-
// --- view state (TODO@bpasero revisit with https://github.com/microsoft/vscode/issues/150804)
445+
private _applyViewState(state: IMergeEditorViewState | undefined) {
446+
if (!state) {
447+
return;
448+
}
449+
this.inputResultView.editor.restoreViewState(state);
450+
if (state.input1State) {
451+
this.input1View.editor.restoreViewState(state.input1State);
452+
}
453+
if (state.input2State) {
454+
this.input2View.editor.restoreViewState(state.input2State);
455+
}
456+
if (state.focusIndex >= 0) {
457+
[this.input1View.editor, this.input2View.editor, this.inputResultView.editor][state.focusIndex].focus();
458+
}
459+
}
443460

444-
protected computeEditorViewState(resource: URI): undefined {
445-
return undefined;
461+
protected computeEditorViewState(resource: URI): IMergeEditorViewState | undefined {
462+
if (isEqual(this.model?.result.uri, resource)) {
463+
// TODO@bpasero Why not check `input#resource` and don't ask me for "forgein" resources?
464+
return undefined;
465+
}
466+
const result = this.inputResultView.editor.saveViewState();
467+
if (!result) {
468+
return undefined;
469+
}
470+
const input1State = this.input1View.editor.saveViewState() ?? undefined;
471+
const input2State = this.input2View.editor.saveViewState() ?? undefined;
472+
const focusIndex = [this.input1View.editor, this.input2View.editor, this.inputResultView.editor].findIndex(editor => editor.hasWidgetFocus());
473+
return { ...result, input1State, input2State, focusIndex };
446474
}
447475

476+
448477
protected tracksEditorViewState(input: EditorInput): boolean {
449-
return false;
478+
return input instanceof MergeEditorInput;
450479
}
451480
}
452481

482+
type IMergeEditorViewState = ICodeEditorViewState & {
483+
readonly input1State?: ICodeEditorViewState;
484+
readonly input2State?: ICodeEditorViewState;
485+
readonly focusIndex: number;
486+
};
487+
488+
453489
function synchronizeScrolling(scrollingEditor: CodeEditorWidget, targetEditor: CodeEditorWidget, mapping: DocumentMapping | undefined, source: MappingDirection) {
454490
if (!mapping) {
455491
return;

0 commit comments

Comments
 (0)