Skip to content

Commit fc86ba3

Browse files
committed
microsoft#50583 Implement Input box with history
1 parent 8950954 commit fc86ba3

1 file changed

Lines changed: 50 additions & 1 deletion

File tree

src/vs/base/browser/ui/inputbox/inputBox.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { Event, Emitter } from 'vs/base/common/event';
1818
import { Widget } from 'vs/base/browser/ui/widget';
1919
import { Color } from 'vs/base/common/color';
2020
import { mixin } from 'vs/base/common/objects';
21+
import { HistoryNavigator } from 'vs/base/common/history';
2122

2223
const $ = dom.$;
2324

@@ -80,7 +81,7 @@ const defaultOpts = {
8081

8182
export class InputBox extends Widget {
8283
private contextViewProvider: IContextViewProvider;
83-
private element: HTMLElement;
84+
element: HTMLElement;
8485
private input: HTMLInputElement;
8586
private mirror: HTMLElement;
8687
private actionbar: ActionBar;
@@ -496,4 +497,52 @@ export class InputBox extends Widget {
496497

497498
super.dispose();
498499
}
500+
}
501+
502+
export interface IHistoryInputOptions extends IInputOptions {
503+
history: string[];
504+
}
505+
506+
export class HistoryInputBox extends InputBox {
507+
508+
private readonly history: HistoryNavigator<string>;
509+
510+
constructor(container: HTMLElement, contextViewProvider: IContextViewProvider, options: IHistoryInputOptions) {
511+
super(container, contextViewProvider, options);
512+
this.history = new HistoryNavigator<string>(options.history, 100);
513+
}
514+
515+
public addToHistory(value: string): void {
516+
if (value !== this.history.current()) {
517+
this.history.add(value);
518+
}
519+
}
520+
521+
public getHistory(): string[] {
522+
return this.history.getHistory();
523+
}
524+
525+
public showNextValue() {
526+
let next = this.history.next();
527+
if (next) {
528+
this.value = next;
529+
}
530+
}
531+
532+
public showPreviousValue() {
533+
let previous;
534+
if (this.value.length === 0) {
535+
previous = this.history.current();
536+
} else {
537+
this.history.addIfNotPresent(this.value);
538+
previous = this.history.previous();
539+
}
540+
if (previous) {
541+
this.value = previous;
542+
}
543+
}
544+
545+
public clearHistory(): void {
546+
this.history.clear();
547+
}
499548
}

0 commit comments

Comments
 (0)