src/vs/base/common/history.ts
298 LOC · 284 covered · 14 uncovered · 82 ranges · 49 concepts · 32 introducers · 27 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
history.ts ×31
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { SetWithKey } from './collections.js';
import { Event } from './event.js';
import { IDisposable } from './lifecycle.js';
import { ArrayNavigator, INavigator } from './navigator.js';
export interface IHistory<T> {
delete(t: T): boolean;
add(t: T): this;
has(t: T): boolean;
clear(): void;
forEach(callbackfn: (value: T, value2: T, set: Set<T>) => void, thisArg?: unknown): void;
replace?(t: T[]): void;
readonly onDidChange?: Event<string[]>;
}
export class HistoryNavigator<T> implements INavigator<T> {
private _limit: number;
private _navigator!: ArrayNavigator<T>;
private _disposable: IDisposable | undefined;
constructor(
limit: number = 10,
) {
this._limit = limit;
this._onChange();
if (this._history.onDidChange) {
this._disposable = this._history.onDidChange(() => this._onChange());
}
public getHistory(): T[] {
return this._elements;
}
public add(t: T) {
this._history.add(t);
this._onChange();
}
public next(): T | null {
// This will navigate past the end of the last element, and in that case the input should be cleared
history.ts ×1
return this._navigator.next();
}
public previous(): T | null {
}
public current(): T | null {
}
public first(): T | null {
}
public last(): T | null {
}
public isFirst(): boolean {
}
public isLast(): boolean {
}
public isNowhere(): boolean {
}
public has(t: T): boolean {
return this._history.has(t);
}
public clear(): void {
this._onChange();
}
private _onChange() {
const elements = this._elements;
this._navigator = new ArrayNavigator(elements, 0, elements.length, elements.length);
}
private _reduceToLimit() {
if (data.length > this._limit) {
if (this._history.replace) {
this._history.replace(replaceValue);
this._history = new Set(replaceValue);
}
}
private _currentPosition(): number {
if (!currentElement) {
}
return this._elements.indexOf(currentElement);
private get _elements(): T[] {
this._history.forEach(e => elements.push(e));
return elements;
}
public dispose(): void {
if (this._disposable) {
this._disposable.dispose();
this._disposable = undefined;
}
}
interface HistoryNode<T> {
value: T;
previous: HistoryNode<T> | undefined;
next: HistoryNode<T> | undefined;
}
/**
* The right way to use HistoryNavigator2 is for the last item in the list to be the user's uncommitted current text. eg empty string, or whatever has been typed. Then
* the user can navigate away from the last item through the list, and back to it. When updating the last item, call replaceLast.
*/
export class HistoryNavigator2<T> {
private valueSet: Set<T>;
private head: HistoryNode<T>;
private tail: HistoryNode<T>;
private cursor: HistoryNode<T>;
private _size: number;
get size(): number { return this._size; }
constructor(history: readonly T[], private capacity: number = 10, private identityFn: (t: T) => unknown = t => t) {
}
this._size = 1;
this.head = this.tail = this.cursor = {
value: history[0],
previous: undefined,
next: undefined
};
this.valueSet = new SetWithKey<T>([history[0]], identityFn);
for (let i = 1; i < history.length; i++) {
this.add(history[i]);
}
add(value: T): void {
value,
previous: this.tail,
next: undefined
};
this.tail.next = node;
this.tail = node;
this.cursor = this.tail;
this._size++;
if (this.valueSet.has(value)) {
this.valueSet.add(value);
}
while (this._size > this.capacity) {
this.head = this.head.next!;
this.head.previous = undefined;
this._size--;
}
/**
* @returns old last value
*/
replaceLast(value: T): T {
return value;
}
const oldValue = this.tail.value;
this.valueSet.delete(oldValue);
this.tail.value = value;
if (this.valueSet.has(value)) {
}
return oldValue;
}
prepend(value: T): void {
return;
}
const node: HistoryNode<T> = {
value,
previous: undefined,
next: this.head
};
this.head.previous = node;
this.head = node;
this._size++;
this.valueSet.add(value);
}
isAtEnd(): boolean {
}
current(): T {
}
previous(): T {
this.cursor = this.cursor.previous;
}
return this.cursor.value;
}
next(): T {
this.cursor = this.cursor.next;
}
return this.cursor.value;
}
has(t: T): boolean {
}
resetCursor(): T {
return this.cursor.value;
}
*[Symbol.iterator](): Iterator<T> {
while (node) {
yield node.value;
node = node.next;
}
}
private _deleteFromList(value: T): void {
const valueKey = this.identityFn(value);
while (temp !== this.tail) {
if (this.identityFn(temp.value) === valueKey) {
if (temp === this.head) {
this.head.previous = undefined;
temp.previous!.next = temp.next;
temp.next!.previous = temp.previous;
}
this._size--;
}
temp = temp.next!;
}
}