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

Changes #192109

Merged
merged 3 commits into from
Sep 4, 2023
Merged

Changes #192109

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
6 changes: 6 additions & 0 deletions src/vs/base/common/arrays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@ export function* groupAdjacentBy<T>(items: Iterable<T>, shouldBeGrouped: (item1:
}
}

export function forEachAdjacent<T>(arr: T[], f: (item1: T | undefined, item2: T | undefined) => void): void {
for (let i = 0; i <= arr.length; i++) {
f(i === 0 ? undefined : arr[i - 1], i === arr.length ? undefined : arr[i]);
}
}

interface IMutableSplice<T> extends ISplice<T> {
readonly toInsert: T[];
deleteCount: number;
Expand Down
8 changes: 4 additions & 4 deletions src/vs/base/common/arraysFind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class MonotonousArray<T> {
public static assertInvariants = false;

private _findLastMonotonousLastIdx = 0;
private _lastPredicate: ((item: T) => boolean) | undefined;
private _prevFindLastPredicate: ((item: T) => boolean) | undefined;

constructor(private readonly _items: T[]) {
}
Expand All @@ -92,14 +92,14 @@ export class MonotonousArray<T> {
*/
findLastMonotonous(predicate: (item: T) => boolean): T | undefined {
if (MonotonousArray.assertInvariants) {
if (this._lastPredicate) {
if (this._prevFindLastPredicate) {
for (const item of this._items) {
if (this._lastPredicate(item) && !predicate(item)) {
if (this._prevFindLastPredicate(item) && !predicate(item)) {
throw new Error('MonotonousArray: current predicate must be weaker than (or equal to) the previous predicate.');
}
}
}
this._lastPredicate = predicate;
this._prevFindLastPredicate = predicate;
}

const idx = findLastIdxMonotonous(this._items, predicate, this._findLastMonotonousLastIdx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { addDisposableListener, addStandardDisposableListener, reset } from 'vs/
import { ActionBar } from 'vs/base/browser/ui/actionbar/actionbar';
import { DomScrollableElement } from 'vs/base/browser/ui/scrollbar/scrollableElement';
import { Action } from 'vs/base/common/actions';
import { forEachAdjacent, groupAdjacentBy } from 'vs/base/common/arrays';
import { Codicon } from 'vs/base/common/codicons';
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
import { Disposable, DisposableStore, toDisposable } from 'vs/base/common/lifecycle';
Expand Down Expand Up @@ -224,7 +225,7 @@ const viewElementGroupLineMargin = 3;
function computeViewElementGroups(diffs: DetailedLineRangeMapping[], originalLineCount: number, modifiedLineCount: number): ViewElementGroup[] {
const result: ViewElementGroup[] = [];

for (const g of group(diffs, (a, b) => (b.modified.startLineNumber - a.modified.endLineNumberExclusive < 2 * viewElementGroupLineMargin))) {
for (const g of groupAdjacentBy(diffs, (a, b) => (b.modified.startLineNumber - a.modified.endLineNumberExclusive < 2 * viewElementGroupLineMargin))) {
const viewElements: ViewElement[] = [];
viewElements.push(new HeaderViewElement());

Expand All @@ -237,7 +238,7 @@ function computeViewElementGroups(diffs: DetailedLineRangeMapping[], originalLin
Math.min(g[g.length - 1].modified.endLineNumberExclusive + viewElementGroupLineMargin, modifiedLineCount + 1)
);

forEachAdjacentItems(g, (a, b) => {
forEachAdjacent(g, (a, b) => {
const origRange = new LineRange(a ? a.original.endLineNumberExclusive : origFullRange.startLineNumber, b ? b.original.startLineNumber : origFullRange.endLineNumberExclusive);
const modifiedRange = new LineRange(a ? a.modified.endLineNumberExclusive : modifiedFullRange.startLineNumber, b ? b.modified.startLineNumber : modifiedFullRange.endLineNumberExclusive);

Expand Down Expand Up @@ -659,31 +660,3 @@ class View extends Disposable {
return r.html;
}
}

function forEachAdjacentItems<T>(items: T[], callback: (item1: T | undefined, item2: T | undefined) => void) {
let last: T | undefined;
for (const item of items) {
callback(last, item);
last = item;
}
callback(last, undefined);
}

function* group<T>(items: Iterable<T>, shouldBeGrouped: (item1: T, item2: T) => boolean): Iterable<T[]> {
let currentGroup: T[] | undefined;
let last: T | undefined;
for (const item of items) {
if (last !== undefined && shouldBeGrouped(last, item)) {
currentGroup!.push(item);
} else {
if (currentGroup) {
yield currentGroup;
}
currentGroup = [item];
}
last = item;
}
if (currentGroup) {
yield currentGroup;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Disposable, toDisposable } from 'vs/base/common/lifecycle';
import { IObservable, IReader, ISettableObservable, ITransaction, autorunWithStore, derived, observableSignal, observableSignalFromEvent, observableValue, transaction, waitForState } from 'vs/base/common/observable';
import { readHotReloadableExport } from 'vs/editor/browser/widget/diffEditorWidget2/utils';
import { ISerializedLineRange, LineRange } from 'vs/editor/common/core/lineRange';
import { AdvancedLinesDiffComputer } from 'vs/editor/common/diff/advancedLinesDiffComputer';
import { DefaultLinesDiffComputer } from 'vs/editor/common/diff/defaultLinesDiffComputer/defaultLinesDiffComputer';
import { IDocumentDiff, IDocumentDiffProvider } from 'vs/editor/common/diff/documentDiffProvider';
import { MovedText } from 'vs/editor/common/diff/linesDiffComputer';
import { DetailedLineRangeMapping } from 'vs/editor/common/diff/rangeMapping';
Expand Down Expand Up @@ -175,7 +175,7 @@ export class DiffEditorViewModel extends Disposable implements IDiffEditorViewMo
debouncer.cancel();
contentChangedSignal.read(reader);
documentDiffProviderOptionChanged.read(reader);
readHotReloadableExport(AdvancedLinesDiffComputer, reader);
readHotReloadableExport(DefaultLinesDiffComputer, reader);

this._isDiffUpToDate.set(false, undefined);

Expand Down
5 changes: 5 additions & 0 deletions src/vs/editor/common/core/lineRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ export class LineRangeSet {
}
}

contains(lineNumber: number): boolean {
const rangeThatStartsBeforeEnd = findLastMonotonous(this._normalizedRanges, r => r.startLineNumber <= lineNumber);
return !!rangeThatStartsBeforeEnd && rangeThatStartsBeforeEnd.endLineNumberExclusive > lineNumber;
}

intersects(range: LineRange): boolean {
const rangeThatStartsBeforeEnd = findLastMonotonous(this._normalizedRanges, r => r.startLineNumber < range.endLineNumberExclusive);
return !!rangeThatStartsBeforeEnd && rangeThatStartsBeforeEnd.endLineNumberExclusive > range.startLineNumber;
Expand Down
6 changes: 6 additions & 0 deletions src/vs/editor/common/core/offsetRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ export class OffsetRange {
}
return result;
}

public forEach(f: (offset: number) => void): void {
for (let i = this.start; i < this.endExclusive; i++) {
f(i);
}
}
}

export class OffsetRangeSet {
Expand Down