Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/interactive/browser/interactiveHistoryService.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { HistoryNavigator2 } from '../../../../base/common/history.js';
7
import { Disposable } from '../../../../base/common/lifecycle.js';
8
import { ResourceMap } from '../../../../base/common/map.js';
9
import { URI } from '../../../../base/common/uri.js';
10
import { createDecorator } from '../../../../platform/instantiation/common/instantiation.js';
11
12
export const IInteractiveHistoryService = createDecorator<IInteractiveHistoryService>('IInteractiveHistoryService');
13
14
export interface IInteractiveHistoryService {
15
readonly _serviceBrand: undefined;
16
17
matchesCurrent(uri: URI, value: string): boolean;
18
addToHistory(uri: URI, value: string): void;
19
getPreviousValue(uri: URI): string | null;
20
getNextValue(uri: URI): string | null;
21
replaceLast(uri: URI, value: string): void;
22
clearHistory(uri: URI): void;
23
has(uri: URI): boolean;
24
}
25
26
export class InteractiveHistoryService extends Disposable implements IInteractiveHistoryService {
27
declare readonly _serviceBrand: undefined;
28
_history: ResourceMap<HistoryNavigator2<string>>;
29
30
constructor() {
31
super();
32
33
this._history = new ResourceMap<HistoryNavigator2<string>>();
34
}
35
36
matchesCurrent(uri: URI, value: string): boolean {
37
const history = this._history.get(uri);
38
if (!history) {
39
return false;
40
}
41
42
return history.current() === value;
43
}
44
45
addToHistory(uri: URI, value: string): void {
46
const history = this._history.get(uri);
47
if (!history) {
48
this._history.set(uri, new HistoryNavigator2<string>([value], 50));
49
return;
50
}
51
52
history.resetCursor();
53
history.add(value);
54
}
55
56
getPreviousValue(uri: URI): string | null {
57
const history = this._history.get(uri);
58
return history?.previous() ?? null;
59
}
60
61
getNextValue(uri: URI): string | null {
62
const history = this._history.get(uri);
63
64
return history?.next() ?? null;
65
}
66
67
replaceLast(uri: URI, value: string) {
68
const history = this._history.get(uri);
69
if (!history) {
70
this._history.set(uri, new HistoryNavigator2<string>([value], 50));
71
return;
72
} else {
73
history.replaceLast(value);
74
history.resetCursor();
75
}
76
}
77
78
clearHistory(uri: URI) {
79
this._history.delete(uri);
80
}
81
82
has(uri: URI) {
83
return this._history.has(uri) ? true : false;
84
}
85
86
}
87
88