Skip to content

Commit 610472b

Browse files
committed
1 parent 0d71f35 commit 610472b

File tree

1 file changed

+43
-12
lines changed

1 file changed

+43
-12
lines changed

src/vs/editor/browser/widget/diffEditorWidget2/movedBlocksLines.ts

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Action } from 'vs/base/common/actions';
99
import { booleanComparator, compareBy, findMaxIdxBy, numberComparator, tieBreakComparators } from 'vs/base/common/arrays';
1010
import { Codicon } from 'vs/base/common/codicons';
1111
import { Disposable, toDisposable } from 'vs/base/common/lifecycle';
12-
import { IObservable, autorun, autorunWithStore, constObservable, derived, derivedWithStore, keepAlive, observableFromEvent, observableSignalFromEvent, observableValue } from 'vs/base/common/observable';
12+
import { IObservable, autorun, autorunHandleChanges, autorunWithStore, constObservable, derived, derivedWithStore, keepAlive, observableFromEvent, observableSignalFromEvent, observableValue } from 'vs/base/common/observable';
1313
import { ThemeIcon } from 'vs/base/common/themables';
1414
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
1515
import { DiffEditorEditors } from 'vs/editor/browser/widget/diffEditorWidget2/diffEditorEditors';
@@ -82,19 +82,50 @@ export class MovedBlocksLinesPart extends Disposable {
8282
}
8383
}));
8484

85-
this._register(this._editors.original.onDidChangeCursorPosition(e => {
86-
const m = this._diffModel.get();
87-
if (!m) { return; }
88-
const movedText = m.diff.get()!.movedTexts.find(m => m.lineRangeMapping.original.contains(e.position.lineNumber));
89-
if (movedText !== m.movedTextToCompare.get()) {
90-
m.movedTextToCompare.set(undefined, undefined);
85+
const originalCursorPosition = observableFromEvent(this._editors.original.onDidChangeCursorPosition, () => this._editors.original.getPosition());
86+
const modifiedCursorPosition = observableFromEvent(this._editors.modified.onDidChangeCursorPosition, () => this._editors.modified.getPosition());
87+
const originalHasFocus = observableSignalFromEvent(
88+
'original.onDidFocusEditorWidget',
89+
e => this._editors.original.onDidFocusEditorWidget(() => setTimeout(() => e(undefined), 0))
90+
);
91+
const modifiedHasFocus = observableSignalFromEvent(
92+
'modified.onDidFocusEditorWidget',
93+
e => this._editors.modified.onDidFocusEditorWidget(() => setTimeout(() => e(undefined), 0))
94+
);
95+
96+
let lastChangedEditor: 'original' | 'modified' = 'modified';
97+
98+
this._register(autorunHandleChanges({
99+
createEmptyChangeSummary: () => undefined,
100+
handleChange: (ctx, summary) => {
101+
if (ctx.didChange(originalHasFocus)) { lastChangedEditor = 'original'; }
102+
if (ctx.didChange(modifiedHasFocus)) { lastChangedEditor = 'modified'; }
103+
return true;
91104
}
92-
m.setActiveMovedText(movedText);
93-
}));
94-
this._register(this._editors.modified.onDidChangeCursorPosition(e => {
95-
const m = this._diffModel.get();
105+
}, reader => {
106+
originalHasFocus.read(reader);
107+
modifiedHasFocus.read(reader);
108+
109+
const m = this._diffModel.read(reader);
96110
if (!m) { return; }
97-
const movedText = m.diff.get()!.movedTexts.find(m => m.lineRangeMapping.modified.contains(e.position.lineNumber));
111+
const diff = m.diff.read(reader);
112+
113+
let movedText: MovedText | undefined = undefined;
114+
115+
if (diff && lastChangedEditor === 'original') {
116+
const originalPos = originalCursorPosition.read(reader);
117+
if (originalPos) {
118+
movedText = diff.movedTexts.find(m => m.lineRangeMapping.original.contains(originalPos.lineNumber));
119+
}
120+
}
121+
122+
if (diff && lastChangedEditor === 'modified') {
123+
const modifiedPos = modifiedCursorPosition.read(reader);
124+
if (modifiedPos) {
125+
movedText = diff.movedTexts.find(m => m.lineRangeMapping.modified.contains(modifiedPos.lineNumber));
126+
}
127+
}
128+
98129
if (movedText !== m.movedTextToCompare.get()) {
99130
m.movedTextToCompare.set(undefined, undefined);
100131
}

0 commit comments

Comments
 (0)