Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improves moved code detection feature #191085

Merged
merged 1 commit into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ export class DiffEditorDecorations extends Disposable {
return null;
}

const currentMove = this._diffModel.read(reader)!.syncedMovedTexts.read(reader);
const movedTextToCompare = this._diffModel.read(reader)!.movedTextToCompare.read(reader);
const renderIndicators = this._options.renderIndicators.read(reader);
const showEmptyDecorations = this._options.showEmptyDecorations.read(reader);

const originalDecorations: IModelDeltaDecoration[] = [];
const modifiedDecorations: IModelDeltaDecoration[] = [];
if (!currentMove) {
if (!movedTextToCompare) {
for (const m of diff.mappings) {
if (!m.lineRangeMapping.originalRange.isEmpty) {
originalDecorations.push({ range: m.lineRangeMapping.originalRange.toInclusiveRange()!, options: renderIndicators ? diffLineDeleteDecorationBackgroundWithIndicator : diffLineDeleteDecorationBackground });
Expand Down Expand Up @@ -68,14 +68,14 @@ export class DiffEditorDecorations extends Disposable {
}
}

if (!m.lineRangeMapping.modifiedRange.isEmpty && this._options.shouldRenderRevertArrows.read(reader) && !currentMove) {
if (!m.lineRangeMapping.modifiedRange.isEmpty && this._options.shouldRenderRevertArrows.read(reader) && !movedTextToCompare) {
modifiedDecorations.push({ range: Range.fromPositions(new Position(m.lineRangeMapping.modifiedRange.startLineNumber, 1)), options: arrowRevertChange });
}
}
}

if (currentMove) {
for (const m of currentMove.changes) {
if (movedTextToCompare) {
for (const m of movedTextToCompare.changes) {
const fullRangeOriginal = m.originalRange.toInclusiveRange();
if (fullRangeOriginal) {
originalDecorations.push({ range: fullRangeOriginal, options: renderIndicators ? diffLineDeleteDecorationBackgroundWithIndicator : diffLineDeleteDecorationBackground });
Expand All @@ -91,20 +91,21 @@ export class DiffEditorDecorations extends Disposable {
}
}
}
const activeMovedText = this._diffModel.read(reader)!.activeMovedText.read(reader);

for (const m of diff.movedTexts) {
originalDecorations.push({
range: m.lineRangeMapping.original.toInclusiveRange()!, options: {
description: 'moved',
blockClassName: 'movedOriginal' + (m === currentMove ? ' currentMove' : ''),
blockClassName: 'movedOriginal' + (m === activeMovedText ? ' currentMove' : ''),
blockPadding: [MovedBlocksLinesPart.movedCodeBlockPadding, 0, MovedBlocksLinesPart.movedCodeBlockPadding, MovedBlocksLinesPart.movedCodeBlockPadding],
}
});

modifiedDecorations.push({
range: m.lineRangeMapping.modified.toInclusiveRange()!, options: {
description: 'moved',
blockClassName: 'movedModified' + (m === currentMove ? ' currentMove' : ''),
blockClassName: 'movedModified' + (m === activeMovedText ? ' currentMove' : ''),
blockPadding: [4, 0, 4, 4],
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,21 @@ export class DiffEditorViewModel extends Disposable implements IDiffEditorViewMo
}
);

public readonly syncedMovedTexts = observableValue<MovedText | undefined>('syncedMovedText', undefined);
public readonly movedTextToCompare = observableValue<MovedText | undefined>('movedTextToCompare', undefined);

private readonly _activeMovedText = observableValue<MovedText | undefined>('activeMovedText', undefined);
private readonly _hoveredMovedText = observableValue<MovedText | undefined>('hoveredMovedText', undefined);


public readonly activeMovedText = derived(r => this.movedTextToCompare.read(r) ?? this._hoveredMovedText.read(r) ?? this._activeMovedText.read(r));

public setActiveMovedText(movedText: MovedText | undefined): void {
this._activeMovedText.set(movedText, undefined);
}

public setHoveredMovedText(movedText: MovedText | undefined): void {
this._hoveredMovedText.set(movedText, undefined);
}

constructor(
public readonly model: IDiffEditorModel,
Expand Down Expand Up @@ -114,8 +128,8 @@ export class DiffEditorViewModel extends Disposable implements IDiffEditorViewMo
transaction(tx => {
this._diff.set(DiffState.fromDiffResult(this._lastDiff!), tx);
updateUnchangedRegions(result, tx);
const currentSyncedMovedText = this.syncedMovedTexts.get();
this.syncedMovedTexts.set(currentSyncedMovedText ? this._lastDiff!.moves.find(m => m.lineRangeMapping.modified.intersect(currentSyncedMovedText.lineRangeMapping.modified)) : undefined, tx);
const currentSyncedMovedText = this.movedTextToCompare.get();
this.movedTextToCompare.set(currentSyncedMovedText ? this._lastDiff!.moves.find(m => m.lineRangeMapping.modified.intersect(currentSyncedMovedText.lineRangeMapping.modified)) : undefined, tx);
});
}
}
Expand All @@ -132,8 +146,8 @@ export class DiffEditorViewModel extends Disposable implements IDiffEditorViewMo
transaction(tx => {
this._diff.set(DiffState.fromDiffResult(this._lastDiff!), tx);
updateUnchangedRegions(result, tx);
const currentSyncedMovedText = this.syncedMovedTexts.get();
this.syncedMovedTexts.set(currentSyncedMovedText ? this._lastDiff!.moves.find(m => m.lineRangeMapping.modified.intersect(currentSyncedMovedText.lineRangeMapping.modified)) : undefined, tx);
const currentSyncedMovedText = this.movedTextToCompare.get();
this.movedTextToCompare.set(currentSyncedMovedText ? this._lastDiff!.moves.find(m => m.lineRangeMapping.modified.intersect(currentSyncedMovedText.lineRangeMapping.modified)) : undefined, tx);
});
}
}
Expand Down Expand Up @@ -180,8 +194,8 @@ export class DiffEditorViewModel extends Disposable implements IDiffEditorViewMo
const state = DiffState.fromDiffResult(result);
this._diff.set(state, tx);
this._isDiffUpToDate.set(true, tx);
const currentSyncedMovedText = this.syncedMovedTexts.get();
this.syncedMovedTexts.set(currentSyncedMovedText ? this._lastDiff.moves.find(m => m.lineRangeMapping.modified.intersect(currentSyncedMovedText.lineRangeMapping.modified)) : undefined, tx);
const currentSyncedMovedText = this.movedTextToCompare.get();
this.movedTextToCompare.set(currentSyncedMovedText ? this._lastDiff.moves.find(m => m.lineRangeMapping.modified.intersect(currentSyncedMovedText.lineRangeMapping.modified)) : undefined, tx);
});
}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import { Codicon } from 'vs/base/common/codicons';
import { KeyCode } from 'vs/base/common/keyCodes';
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { EditorAction2, ServicesAccessor } from 'vs/editor/browser/editorExtensions';
import { findFocusedDiffEditor } from 'vs/editor/browser/widget/diffEditor.contribution';
Expand Down Expand Up @@ -128,3 +129,29 @@ export class SwitchSide extends EditorAction2 {
}

registerAction2(SwitchSide);

export class ExitCompareMove extends EditorAction2 {
constructor() {
super({
id: 'diffEditor.exitCompareMove',
title: { value: localize('exitCompareMove', "Exit Compare Move"), original: 'Exit Compare Move' },
icon: Codicon.close,
precondition: EditorContextKeys.comparingMovedCode,
f1: false,
category: diffEditorCategory,
keybinding: {
weight: 10000,
primary: KeyCode.Escape,
}
});
}

runEditorCommand(accessor: ServicesAccessor, editor: ICodeEditor, ...args: unknown[]): void {
const diffEditor = findFocusedDiffEditor(accessor);
if (diffEditor instanceof DiffEditorWidget2) {
diffEditor.exitCompareMove();
}
}
}

registerAction2(ExitCompareMove);
25 changes: 12 additions & 13 deletions src/vs/editor/browser/widget/diffEditorWidget2/diffEditorWidget2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ export class DiffEditorWidget2 extends DelegatingEditor implements IDiffEditor {
isEmbeddedDiffEditorKey.set(this._options.isInEmbeddedEditor.read(reader));
}));

const comparingMovedCodeKey = EditorContextKeys.comparingMovedCode.bindTo(this._contextKeyService);
this._register(autorun(reader => {
/** @description update comparingMovedCodeKey */
comparingMovedCodeKey.set(!!this._diffModel.read(reader)?.movedTextToCompare.read(reader));
}));

const diffEditorRenderSideBySideInlineBreakpointReachedContextKeyValue = EditorContextKeys.diffEditorRenderSideBySideInlineBreakpointReached.bindTo(this._contextKeyService);
this._register(autorun(reader => {
/** @description update accessibleDiffViewerVisible context key */
Expand Down Expand Up @@ -223,19 +229,6 @@ export class DiffEditorWidget2 extends DelegatingEditor implements IDiffEditor {
),
}));

this._register(this._editors.original.onDidChangeCursorPosition(e => {
const m = this._diffModel.get();
if (!m) { return; }
const movedText = m.diff.get()!.movedTexts.find(m => m.lineRangeMapping.original.contains(e.position.lineNumber));
m.syncedMovedTexts.set(movedText, undefined);
}));
this._register(this._editors.modified.onDidChangeCursorPosition(e => {
const m = this._diffModel.get();
if (!m) { return; }
const movedText = m.diff.get()!.movedTexts.find(m => m.lineRangeMapping.modified.contains(e.position.lineNumber));
m.syncedMovedTexts.set(movedText, undefined);
}));

// Revert change when an arrow is clicked.
this._register(this._editors.modified.onMouseDown(event => {
if (!event.event.rightButton && event.target.position && event.target.element?.className.includes('arrow-revert-change')) {
Expand Down Expand Up @@ -518,6 +511,12 @@ export class DiffEditorWidget2 extends DelegatingEditor implements IDiffEditor {
}
destination.focus();
}

exitCompareMove(): void {
const model = this._diffModel.get();
if (!model) { return; }
model.movedTextToCompare.set(undefined, undefined);
}
}

function translatePosition(posInOriginal: Position, mappings: LineRangeMapping[]): Range {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class ViewZoneManager extends Disposable {

const alignmentsSyncedMovedText = derived<ILineRangeAlignment[] | null>((reader) => {
/** @description alignments */
const syncedMovedText = this._diffModel.read(reader)?.syncedMovedTexts.read(reader);
const syncedMovedText = this._diffModel.read(reader)?.movedTextToCompare.read(reader);
if (!syncedMovedText) { return null; }
state.read(reader);
const mappings = syncedMovedText.changes.map(c => new DiffMapping(c));
Expand Down Expand Up @@ -171,7 +171,7 @@ export class ViewZoneManager extends Disposable {

const modLineHeight = this._editors.modified.getOption(EditorOption.lineHeight);

const syncedMovedText = this._diffModel.read(reader)?.syncedMovedTexts.read(reader);
const syncedMovedText = this._diffModel.read(reader)?.movedTextToCompare.read(reader);

const mightContainNonBasicASCII = this._editors.original.getModel()?.mightContainNonBasicASCII() ?? false;
const mightContainRTL = this._editors.original.getModel()?.mightContainRTL() ?? false;
Expand Down Expand Up @@ -424,7 +424,7 @@ export class ViewZoneManager extends Disposable {

this._register(autorun(reader => {
/** @description update editor top offsets */
const m = this._diffModel.read(reader)?.syncedMovedTexts.read(reader);
const m = this._diffModel.read(reader)?.movedTextToCompare.read(reader);

let deltaOrigToMod = 0;
if (m) {
Expand Down