Skip to content

Commit 6fb6056

Browse files
authored
1 parent 0a9940b commit 6fb6056

File tree

1 file changed

+26
-19
lines changed
  • src/vs/workbench/contrib/mergeEditor/browser/view

1 file changed

+26
-19
lines changed

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

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,22 @@ import { InputCodeEditorView } from 'vs/workbench/contrib/mergeEditor/browser/vi
1414
import { ResultCodeEditorView } from 'vs/workbench/contrib/mergeEditor/browser/view/editors/resultCodeEditorView';
1515

1616
export class MergeEditorViewModel {
17+
private counter = 0;
1718
private readonly lastFocusedEditor = derivedObservableWithWritableCache<
18-
CodeEditorView | undefined
19+
{ view: CodeEditorView | undefined; counter: number }
1920
>('lastFocusedEditor', (reader, lastValue) => {
2021
const editors = [
2122
this.inputCodeEditorView1,
2223
this.inputCodeEditorView2,
2324
this.resultCodeEditorView,
2425
];
25-
return editors.find((e) => e.isFocused.read(reader)) || lastValue;
26+
const view = editors.find((e) => e.isFocused.read(reader));
27+
return view ? { view, counter: this.counter++ } : lastValue || { view: undefined, counter: this.counter++ };
2628
});
2729

2830
private readonly manuallySetActiveModifiedBaseRange = observableValue<
29-
ModifiedBaseRange | undefined
30-
>('manuallySetActiveModifiedBaseRange', undefined);
31+
{ range: ModifiedBaseRange | undefined; counter: number }
32+
>('manuallySetActiveModifiedBaseRange', { range: undefined, counter: 0 });
3133

3234
private getRange(editor: CodeEditorView, modifiedBaseRange: ModifiedBaseRange, reader: IReader | undefined): LineRange {
3335
if (editor === this.resultCodeEditorView) {
@@ -42,17 +44,22 @@ export class MergeEditorViewModel {
4244
'activeModifiedBaseRange',
4345
(reader) => {
4446
const focusedEditor = this.lastFocusedEditor.read(reader);
45-
if (!focusedEditor) {
46-
return this.manuallySetActiveModifiedBaseRange.read(reader);
47+
const manualRange = this.manuallySetActiveModifiedBaseRange.read(reader);
48+
if (manualRange.counter > focusedEditor.counter) {
49+
return manualRange.range;
4750
}
48-
const cursorLineNumber = focusedEditor.cursorLineNumber.read(reader);
51+
52+
if (!focusedEditor.view) {
53+
return;
54+
}
55+
const cursorLineNumber = focusedEditor.view.cursorLineNumber.read(reader);
4956
if (!cursorLineNumber) {
5057
return undefined;
5158
}
5259

5360
const modifiedBaseRanges = this.model.modifiedBaseRanges.read(reader);
5461
return modifiedBaseRanges.find((r) => {
55-
const range = this.getRange(focusedEditor, r, reader);
62+
const range = this.getRange(focusedEditor.view!, r, reader);
5663
return range.isEmpty
5764
? range.startLineNumber === cursorLineNumber
5865
: range.contains(cursorLineNumber);
@@ -72,28 +79,28 @@ export class MergeEditorViewModel {
7279
state: ModifiedBaseRangeState,
7380
tx: ITransaction
7481
): void {
75-
this.manuallySetActiveModifiedBaseRange.set(baseRange, tx);
76-
this.lastFocusedEditor.clearCache(tx);
82+
this.manuallySetActiveModifiedBaseRange.set({ range: baseRange, counter: this.counter++ }, tx);
7783
this.model.setState(baseRange, state, true, tx);
7884
}
7985

8086
private goToConflict(getModifiedBaseRange: (editor: CodeEditorView, curLineNumber: number) => ModifiedBaseRange | undefined): void {
81-
const lastFocusedEditor = this.lastFocusedEditor.get();
82-
if (!lastFocusedEditor) {
83-
return;
87+
let editor = this.lastFocusedEditor.get().view;
88+
if (!editor) {
89+
editor = this.resultCodeEditorView;
8490
}
85-
const curLineNumber = lastFocusedEditor.editor.getPosition()?.lineNumber;
91+
const curLineNumber = editor.editor.getPosition()?.lineNumber;
8692
if (curLineNumber === undefined) {
8793
return;
8894
}
89-
const modifiedBaseRange = getModifiedBaseRange(lastFocusedEditor, curLineNumber);
95+
const modifiedBaseRange = getModifiedBaseRange(editor, curLineNumber);
9096
if (modifiedBaseRange) {
91-
const range = this.getRange(lastFocusedEditor, modifiedBaseRange, undefined);
92-
lastFocusedEditor.editor.setPosition({
97+
const range = this.getRange(editor, modifiedBaseRange, undefined);
98+
editor.editor.focus();
99+
editor.editor.setPosition({
93100
lineNumber: range.startLineNumber,
94-
column: lastFocusedEditor.editor.getModel()!.getLineFirstNonWhitespaceColumn(range.startLineNumber),
101+
column: editor.editor.getModel()!.getLineFirstNonWhitespaceColumn(range.startLineNumber),
95102
});
96-
lastFocusedEditor.editor.revealLinesNearTop(range.startLineNumber, range.endLineNumberExclusive, ScrollType.Smooth);
103+
editor.editor.revealLinesNearTop(range.startLineNumber, range.endLineNumberExclusive, ScrollType.Smooth);
97104
}
98105
}
99106

0 commit comments

Comments
 (0)