forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
terminalFindWidget.ts
158 lines (140 loc) · 6.56 KB
/
terminalFindWidget.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { SimpleFindWidget } from 'vs/workbench/contrib/codeEditor/browser/find/simpleFindWidget';
import { IContextViewService } from 'vs/platform/contextview/browser/contextView';
import { IContextKeyService, IContextKey } from 'vs/platform/contextkey/common/contextkey';
import { FindReplaceState } from 'vs/editor/contrib/find/browser/findState';
import { ITerminalInstance, ITerminalService, IXtermTerminal } from 'vs/workbench/contrib/terminal/browser/terminal';
import { TerminalContextKeys } from 'vs/workbench/contrib/terminal/common/terminalContextKey';
import { IColorTheme, IThemeService } from 'vs/platform/theme/common/themeService';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { Event } from 'vs/base/common/event';
import { ISearchOptions } from 'xterm-addon-search';
export class TerminalFindWidget extends SimpleFindWidget {
private _findInputFocused: IContextKey<boolean>;
private _findWidgetFocused: IContextKey<boolean>;
private _findWidgetVisible: IContextKey<boolean>;
constructor(
findState: FindReplaceState,
private _instance: ITerminalInstance,
@IContextViewService _contextViewService: IContextViewService,
@IKeybindingService keybindingService: IKeybindingService,
@IContextKeyService private readonly _contextKeyService: IContextKeyService,
@IThemeService private readonly _themeService: IThemeService,
@IConfigurationService private readonly _configurationService: IConfigurationService,
@ITerminalService private readonly _terminalService: ITerminalService
) {
super(findState, { showOptionButtons: true, showResultCount: true, type: 'Terminal' }, _contextViewService, _contextKeyService, keybindingService);
this._register(findState.onFindReplaceStateChange(() => {
this.show();
}));
this._findInputFocused = TerminalContextKeys.findInputFocus.bindTo(this._contextKeyService);
this._findWidgetFocused = TerminalContextKeys.findFocus.bindTo(this._contextKeyService);
this._findWidgetVisible = TerminalContextKeys.findVisible.bindTo(this._contextKeyService);
this.updateTheme(this._themeService.getColorTheme());
this._register(this._themeService.onDidColorThemeChange((theme?: IColorTheme) => {
this.updateTheme(theme ?? this._themeService.getColorTheme());
if (this.isVisible()) {
this.find(true, true);
}
}));
this._register(this._configurationService.onDidChangeConfiguration((e) => {
if (e.affectsConfiguration('workbench.colorCustomizations') && this.isVisible()) {
this.find(true, true);
}
}));
}
find(previous: boolean, update?: boolean) {
const xterm = this._instance.xterm;
if (!xterm) {
return;
}
if (previous) {
this._findPreviousWithEvent(xterm, this.inputValue, { regex: this._getRegexValue(), wholeWord: this._getWholeWordValue(), caseSensitive: this._getCaseSensitiveValue(), incremental: update });
} else {
this._findNextWithEvent(xterm, this.inputValue, { regex: this._getRegexValue(), wholeWord: this._getWholeWordValue(), caseSensitive: this._getCaseSensitiveValue() });
}
}
override reveal(): void {
const initialInput = this._instance.hasSelection() && this._instance.selection!.indexOf('\n') === -1 ? this._instance.selection : undefined;
const xterm = this._instance.xterm;
if (xterm && this.inputValue && this.inputValue !== '') {
// trigger highlight all matches
this._findPreviousWithEvent(xterm, this.inputValue, { incremental: true, regex: this._getRegexValue(), wholeWord: this._getWholeWordValue(), caseSensitive: this._getCaseSensitiveValue() }).then(foundMatch => {
this.updateButtons(foundMatch);
this._register(Event.once(xterm.onDidChangeSelection)(() => xterm.clearActiveSearchDecoration()));
});
}
this.updateButtons(false);
super.reveal(initialInput);
this._findWidgetVisible.set(true);
}
override show() {
const initialInput = this._instance.hasSelection() && this._instance.selection!.indexOf('\n') === -1 ? this._instance.selection : undefined;
super.show(initialInput);
this._findWidgetVisible.set(true);
}
override hide() {
super.hide();
this._findWidgetVisible.reset();
this._instance.focus();
this._instance.xterm?.clearSearchDecorations();
}
protected async _getResultCount(): Promise<{ resultIndex: number; resultCount: number } | undefined> {
return this._instance.xterm?.findResult;
}
protected _onInputChanged() {
// Ignore input changes for now
const xterm = this._instance.xterm;
if (xterm) {
this._findPreviousWithEvent(xterm, this.inputValue, { regex: this._getRegexValue(), wholeWord: this._getWholeWordValue(), caseSensitive: this._getCaseSensitiveValue(), incremental: true }).then(foundMatch => {
this.updateButtons(foundMatch);
});
}
return false;
}
protected _onFocusTrackerFocus() {
this._findWidgetFocused.set(true);
}
protected _onFocusTrackerBlur() {
if (this._terminalService.activeInstance !== this._instance) {
return;
}
this._instance.xterm?.clearActiveSearchDecoration();
this._findWidgetFocused.reset();
}
protected _onFindInputFocusTrackerFocus() {
this._findInputFocused.set(true);
}
protected _onFindInputFocusTrackerBlur() {
if (this._terminalService.activeInstance !== this._instance) {
return;
}
this._findInputFocused.reset();
}
findFirst() {
const instance = this._instance;
if (instance.hasSelection()) {
instance.clearSelection();
}
const xterm = instance.xterm;
if (xterm) {
this._findPreviousWithEvent(xterm, this.inputValue, { regex: this._getRegexValue(), wholeWord: this._getWholeWordValue(), caseSensitive: this._getCaseSensitiveValue() });
}
}
private async _findNextWithEvent(xterm: IXtermTerminal, term: string, options: ISearchOptions): Promise<boolean> {
return xterm.findNext(term, options).then(foundMatch => {
this._register(Event.once(xterm.onDidChangeSelection)(() => xterm.clearActiveSearchDecoration()));
return foundMatch;
});
}
private async _findPreviousWithEvent(xterm: IXtermTerminal, term: string, options: ISearchOptions): Promise<boolean> {
return xterm.findPrevious(term, options).then(foundMatch => {
this._register(Event.once(xterm.onDidChangeSelection)(() => xterm.clearActiveSearchDecoration()));
return foundMatch;
});
}
}