Path: blob/main/src/vs/workbench/contrib/interactive/browser/interactiveHistoryService.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { HistoryNavigator2 } from '../../../../base/common/history.js';6import { Disposable } from '../../../../base/common/lifecycle.js';7import { ResourceMap } from '../../../../base/common/map.js';8import { URI } from '../../../../base/common/uri.js';9import { createDecorator } from '../../../../platform/instantiation/common/instantiation.js';1011export const IInteractiveHistoryService = createDecorator<IInteractiveHistoryService>('IInteractiveHistoryService');1213export interface IInteractiveHistoryService {14readonly _serviceBrand: undefined;1516matchesCurrent(uri: URI, value: string): boolean;17addToHistory(uri: URI, value: string): void;18getPreviousValue(uri: URI): string | null;19getNextValue(uri: URI): string | null;20replaceLast(uri: URI, value: string): void;21clearHistory(uri: URI): void;22has(uri: URI): boolean;23}2425export class InteractiveHistoryService extends Disposable implements IInteractiveHistoryService {26declare readonly _serviceBrand: undefined;27_history: ResourceMap<HistoryNavigator2<string>>;2829constructor() {30super();3132this._history = new ResourceMap<HistoryNavigator2<string>>();33}3435matchesCurrent(uri: URI, value: string): boolean {36const history = this._history.get(uri);37if (!history) {38return false;39}4041return history.current() === value;42}4344addToHistory(uri: URI, value: string): void {45const history = this._history.get(uri);46if (!history) {47this._history.set(uri, new HistoryNavigator2<string>([value], 50));48return;49}5051history.resetCursor();52history.add(value);53}5455getPreviousValue(uri: URI): string | null {56const history = this._history.get(uri);57return history?.previous() ?? null;58}5960getNextValue(uri: URI): string | null {61const history = this._history.get(uri);6263return history?.next() ?? null;64}6566replaceLast(uri: URI, value: string) {67const history = this._history.get(uri);68if (!history) {69this._history.set(uri, new HistoryNavigator2<string>([value], 50));70return;71} else {72history.replaceLast(value);73history.resetCursor();74}75}7677clearHistory(uri: URI) {78this._history.delete(uri);79}8081has(uri: URI) {82return this._history.has(uri) ? true : false;83}8485}868788