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

Refactors TokenizationStateStore #178683

Merged
merged 1 commit into from Mar 30, 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
24 changes: 21 additions & 3 deletions src/vs/editor/common/core/offsetRange.ts
Expand Up @@ -9,6 +9,24 @@ import { BugIndicatingError } from 'vs/base/common/errors';
* A range of offsets (0-based).
*/
export class OffsetRange {
public static addRange(range: OffsetRange, sortedRanges: OffsetRange[]): void {
let i = 0;
while (i < sortedRanges.length && sortedRanges[i].endExclusive < range.start) {
i++;
}
let j = i;
while (j < sortedRanges.length && sortedRanges[j].start <= range.endExclusive) {
j++;
}
if (i === j) {
sortedRanges.splice(i, 0, range);
} else {
const start = Math.min(range.start, sortedRanges[i].start);
const end = Math.max(range.endExclusive, sortedRanges[j - 1].endExclusive);
sortedRanges.splice(i, j - i, new OffsetRange(start, end));
}
}

public static tryCreate(start: number, endExclusive: number): OffsetRange | undefined {
if (start > endExclusive) {
return undefined;
Expand Down Expand Up @@ -64,9 +82,9 @@ export class OffsetRange {
* The resulting range is empty if the ranges do not intersect, but touch.
* If the ranges don't even touch, the result is undefined.
*/
public intersect(seq1Range: OffsetRange): OffsetRange | undefined {
const start = Math.max(this.start, seq1Range.start);
const end = Math.min(this.endExclusive, seq1Range.endExclusive);
public intersect(other: OffsetRange): OffsetRange | undefined {
const start = Math.max(this.start, other.start);
const end = Math.min(this.endExclusive, other.endExclusive);
if (start <= end) {
return new OffsetRange(start, end);
}
Expand Down
77 changes: 77 additions & 0 deletions src/vs/editor/common/model/fixedArray.ts
@@ -0,0 +1,77 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { arrayInsert } from 'vs/base/common/arrays';

/**
* An array that avoids being sparse by always
* filling up unused indices with a default value.
*/
export class FixedArray<T> {
private _store: T[] = [];

constructor(
private readonly _default: T
) { }

public get(index: number): T {
if (index < this._store.length) {
return this._store[index];
}
return this._default;
}

public set(index: number, value: T): void {
while (index >= this._store.length) {
this._store[this._store.length] = this._default;
}
this._store[index] = value;
}

public replace(index: number, oldLength: number, newLength: number): void {
if (index >= this._store.length) {
return;
}

if (oldLength === 0) {
this.insert(index, newLength);
return;
} else if (newLength === 0) {
this.delete(index, oldLength);
return;
}

const before = this._store.slice(0, index);
const after = this._store.slice(index + oldLength);
const insertArr = arrayFill(newLength, this._default);
this._store = before.concat(insertArr, after);
}

public delete(deleteIndex: number, deleteCount: number): void {
if (deleteCount === 0 || deleteIndex >= this._store.length) {
return;
}
this._store.splice(deleteIndex, deleteCount);
}

public insert(insertIndex: number, insertCount: number): void {
if (insertCount === 0 || insertIndex >= this._store.length) {
return;
}
const arr: T[] = [];
for (let i = 0; i < insertCount; i++) {
arr[i] = this._default;
}
this._store = arrayInsert(this._store, insertIndex, arr);
}
}

function arrayFill<T>(length: number, value: T): T[] {
const arr: T[] = [];
for (let i = 0; i < length; i++) {
arr[i] = value;
}
return arr;
}