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

Diff editor code improvements #205147

Merged
merged 1 commit into from
Feb 16, 2024
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 @@ -5,7 +5,7 @@

import { Emitter } from 'vs/base/common/event';
import { Disposable } from 'vs/base/common/lifecycle';
import { IObservable, IReader, autorunHandleChanges, observableFromEvent } from 'vs/base/common/observable';
import { IObservable, IReader, autorunHandleChanges, derivedOpts, observableFromEvent } from 'vs/base/common/observable';
import { IEditorConstructionOptions } from 'vs/editor/browser/config/editorConfiguration';
import { IDiffEditorConstructionOptions } from 'vs/editor/browser/editorBrowser';
import { CodeEditorWidget, ICodeEditorWidgetOptions } from 'vs/editor/browser/widget/codeEditorWidget';
Expand Down Expand Up @@ -36,6 +36,8 @@ export class DiffEditorEditors extends Disposable {
public readonly modifiedSelections: IObservable<Selection[]>;
public readonly modifiedCursor: IObservable<Position>;

public readonly originalCursor: IObservable<Position>;

constructor(
private readonly originalEditorElement: HTMLElement,
private readonly modifiedEditorElement: HTMLElement,
Expand All @@ -56,7 +58,9 @@ export class DiffEditorEditors extends Disposable {
this.modifiedScrollHeight = observableFromEvent(this.modified.onDidScrollChange, () => /** @description modified.getScrollHeight */ this.modified.getScrollHeight());

this.modifiedSelections = observableFromEvent(this.modified.onDidChangeCursorSelection, () => this.modified.getSelections() ?? []);
this.modifiedCursor = observableFromEvent(this.modified.onDidChangeCursorPosition, () => this.modified.getPosition() ?? new Position(1, 1));
this.modifiedCursor = derivedOpts({ owner: this, equalityComparer: Position.equals }, reader => this.modifiedSelections.read(reader)[0]?.getPosition() ?? new Position(1, 1));

this.originalCursor = observableFromEvent(this.original.onDidChangeCursorPosition, () => this.original.getPosition() ?? new Position(1, 1));

this._register(autorunHandleChanges({
createEmptyChangeSummary: () => ({} as IDiffEditorConstructionOptions),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ class CollapsedCodeOverlayWidget extends ViewZoneOverlayWidget {
}

this._register(autorun(reader => {
/** @description Update CollapsedCodeOverlayWidget canMove* css classes */
const isFullyRevealed = this._unchangedRegion.visibleLineCountTop.read(reader) + this._unchangedRegion.visibleLineCountBottom.read(reader) === this._unchangedRegion.lineCount;

this._nodes.bottom.classList.toggle('canMoveTop', !isFullyRevealed);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ export class MovedBlocksLinesFeature extends Disposable {
}
}));

const originalCursorPosition = observableFromEvent(this._editors.original.onDidChangeCursorPosition, () => this._editors.original.getPosition());
const modifiedCursorPosition = observableFromEvent(this._editors.modified.onDidChangeCursorPosition, () => this._editors.modified.getPosition());
const originalHasFocus = observableSignalFromEvent(
'original.onDidFocusEditorWidget',
e => this._editors.original.onDidFocusEditorWidget(() => setTimeout(() => e(undefined), 0))
Expand Down Expand Up @@ -115,14 +113,14 @@ export class MovedBlocksLinesFeature extends Disposable {
let movedText: MovedText | undefined = undefined;

if (diff && lastChangedEditor === 'original') {
const originalPos = originalCursorPosition.read(reader);
const originalPos = this._editors.originalCursor.read(reader);
if (originalPos) {
movedText = diff.movedTexts.find(m => m.lineRangeMapping.original.contains(originalPos.lineNumber));
}
}

if (diff && lastChangedEditor === 'modified') {
const modifiedPos = modifiedCursorPosition.read(reader);
const modifiedPos = this._editors.modifiedCursor.read(reader);
if (modifiedPos) {
movedText = diff.movedTexts.find(m => m.lineRangeMapping.modified.contains(modifiedPos.lineNumber));
}
Expand Down
2 changes: 1 addition & 1 deletion src/vs/editor/browser/widget/diffEditor/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ function addLength(position: Position, length: LengthObj): Position {

export function bindContextKey<T extends ContextKeyValue>(key: RawContextKey<T>, service: IContextKeyService, computeValue: (reader: IReader) => T): IDisposable {
const boundKey = key.bindTo(service);
return autorunOpts({ debugName: () => `Update ${key.key}` }, reader => {
return autorunOpts({ debugName: () => `Set Context Key "${key.key}"` }, reader => {
boundKey.set(computeValue(reader));
});
}
Expand Down