-
Notifications
You must be signed in to change notification settings - Fork 28.2k
/
terminal.suggest.contribution.ts
176 lines (157 loc) · 9.19 KB
/
terminal.suggest.contribution.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import type { Terminal as RawXtermTerminal } from '@xterm/xterm';
import * as dom from 'vs/base/browser/dom';
import { Event } from 'vs/base/common/event';
import { KeyCode } from 'vs/base/common/keyCodes';
import { DisposableStore, MutableDisposable, toDisposable } from 'vs/base/common/lifecycle';
import { localize2 } from 'vs/nls';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { ContextKeyExpr, IContextKey, IContextKeyService, IReadableSet } from 'vs/platform/contextkey/common/contextkey';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
import { TerminalSettingId } from 'vs/platform/terminal/common/terminal';
import { ITerminalContribution, ITerminalInstance, IXtermTerminal } from 'vs/workbench/contrib/terminal/browser/terminal';
import { registerActiveInstanceAction } from 'vs/workbench/contrib/terminal/browser/terminalActions';
import { registerTerminalContribution } from 'vs/workbench/contrib/terminal/browser/terminalExtensions';
import { TerminalWidgetManager } from 'vs/workbench/contrib/terminal/browser/widgets/widgetManager';
import { ITerminalConfiguration, ITerminalProcessManager, TERMINAL_CONFIG_SECTION } from 'vs/workbench/contrib/terminal/common/terminal';
import { TerminalContextKeys } from 'vs/workbench/contrib/terminal/common/terminalContextKey';
import { SuggestAddon } from 'vs/workbench/contrib/terminalContrib/suggest/browser/terminalSuggestAddon';
import { TerminalSuggestCommandId } from 'vs/workbench/contrib/terminalContrib/suggest/common/terminal.suggest';
// #region Terminal Contributions
class TerminalSuggestContribution extends DisposableStore implements ITerminalContribution {
static readonly ID = 'terminal.suggest';
static get(instance: ITerminalInstance): TerminalSuggestContribution | null {
return instance.getContribution<TerminalSuggestContribution>(TerminalSuggestContribution.ID);
}
private readonly _addon: MutableDisposable<SuggestAddon> = new MutableDisposable();
private _terminalSuggestWidgetContextKeys: IReadableSet<string> = new Set(TerminalContextKeys.suggestWidgetVisible.key);
private _terminalSuggestWidgetVisibleContextKey: IContextKey<boolean>;
get addon(): SuggestAddon | undefined { return this._addon.value; }
constructor(
private readonly _instance: ITerminalInstance,
processManager: ITerminalProcessManager,
widgetManager: TerminalWidgetManager,
@IContextKeyService private readonly _contextKeyService: IContextKeyService,
@IConfigurationService private readonly _configurationService: IConfigurationService,
@IInstantiationService private readonly _instantiationService: IInstantiationService
) {
super();
this.add(toDisposable(() => this._addon?.dispose()));
this._terminalSuggestWidgetVisibleContextKey = TerminalContextKeys.suggestWidgetVisible.bindTo(this._contextKeyService);
}
xtermOpen(xterm: IXtermTerminal & { raw: RawXtermTerminal }): void {
this.add(Event.runAndSubscribe(this._instance.onDidChangeShellType, async () => {
this._loadSuggestAddon(xterm.raw);
}));
this.add(this._contextKeyService.onDidChangeContext(e => {
if (e.affectsSome(this._terminalSuggestWidgetContextKeys)) {
this._loadSuggestAddon(xterm.raw);
}
}));
this.add(this._configurationService.onDidChangeConfiguration(e => {
if (e.affectsConfiguration(TerminalSettingId.SendKeybindingsToShell)) {
this._loadSuggestAddon(xterm.raw);
}
}));
}
private _loadSuggestAddon(xterm: RawXtermTerminal): void {
const sendingKeybindingsToShell = this._configurationService.getValue<ITerminalConfiguration>(TERMINAL_CONFIG_SECTION).sendKeybindingsToShell;
if (sendingKeybindingsToShell || this._instance.shellType !== 'pwsh') {
this._addon.clear();
return;
}
if (this._terminalSuggestWidgetVisibleContextKey) {
this._addon.value = this._instantiationService.createInstance(SuggestAddon, this._instance.capabilities, this._terminalSuggestWidgetVisibleContextKey);
xterm.loadAddon(this._addon.value);
this._addon.value.setPanel(dom.findParentWithClass(xterm.element!, 'panel')!);
this._addon.value.setScreen(xterm.element!.querySelector('.xterm-screen')!);
this.add(this._instance.onDidBlur(() => this._addon.value?.hideSuggestWidget()));
this.add(this._addon.value.onAcceptedCompletion(async text => {
this._instance.focus();
this._instance.sendText(text, false);
}));
this.add(this._instance.onDidSendText(() => this._addon.value?.hideSuggestWidget()));
}
}
}
registerTerminalContribution(TerminalSuggestContribution.ID, TerminalSuggestContribution);
// #endregion
// #region Actions
registerActiveInstanceAction({
id: TerminalSuggestCommandId.SelectPrevSuggestion,
title: localize2('workbench.action.terminal.selectPrevSuggestion', 'Select the Previous Suggestion'),
f1: false,
precondition: ContextKeyExpr.and(ContextKeyExpr.or(TerminalContextKeys.processSupported, TerminalContextKeys.terminalHasBeenCreated), TerminalContextKeys.focus, TerminalContextKeys.isOpen, TerminalContextKeys.suggestWidgetVisible),
keybinding: {
// Up is bound to other workbench keybindings that this needs to beat
primary: KeyCode.UpArrow,
weight: KeybindingWeight.WorkbenchContrib + 1
},
run: (activeInstance) => TerminalSuggestContribution.get(activeInstance)?.addon?.selectPreviousSuggestion()
});
registerActiveInstanceAction({
id: TerminalSuggestCommandId.SelectPrevPageSuggestion,
title: localize2('workbench.action.terminal.selectPrevPageSuggestion', 'Select the Previous Page Suggestion'),
f1: false,
precondition: ContextKeyExpr.and(ContextKeyExpr.or(TerminalContextKeys.processSupported, TerminalContextKeys.terminalHasBeenCreated), TerminalContextKeys.focus, TerminalContextKeys.isOpen, TerminalContextKeys.suggestWidgetVisible),
keybinding: {
// Up is bound to other workbench keybindings that this needs to beat
primary: KeyCode.PageUp,
weight: KeybindingWeight.WorkbenchContrib + 1
},
run: (activeInstance) => TerminalSuggestContribution.get(activeInstance)?.addon?.selectPreviousPageSuggestion()
});
registerActiveInstanceAction({
id: TerminalSuggestCommandId.SelectNextSuggestion,
title: localize2('workbench.action.terminal.selectNextSuggestion', 'Select the Next Suggestion'),
f1: false,
precondition: ContextKeyExpr.and(ContextKeyExpr.or(TerminalContextKeys.processSupported, TerminalContextKeys.terminalHasBeenCreated), TerminalContextKeys.focus, TerminalContextKeys.isOpen, TerminalContextKeys.suggestWidgetVisible),
keybinding: {
// Down is bound to other workbench keybindings that this needs to beat
primary: KeyCode.DownArrow,
weight: KeybindingWeight.WorkbenchContrib + 1
},
run: (activeInstance) => TerminalSuggestContribution.get(activeInstance)?.addon?.selectNextSuggestion()
});
registerActiveInstanceAction({
id: TerminalSuggestCommandId.SelectNextPageSuggestion,
title: localize2('workbench.action.terminal.selectNextPageSuggestion', 'Select the Next Page Suggestion'),
f1: false,
precondition: ContextKeyExpr.and(ContextKeyExpr.or(TerminalContextKeys.processSupported, TerminalContextKeys.terminalHasBeenCreated), TerminalContextKeys.focus, TerminalContextKeys.isOpen, TerminalContextKeys.suggestWidgetVisible),
keybinding: {
// Down is bound to other workbench keybindings that this needs to beat
primary: KeyCode.PageDown,
weight: KeybindingWeight.WorkbenchContrib + 1
},
run: (activeInstance) => TerminalSuggestContribution.get(activeInstance)?.addon?.selectNextPageSuggestion()
});
registerActiveInstanceAction({
id: TerminalSuggestCommandId.AcceptSelectedSuggestion,
title: localize2('workbench.action.terminal.acceptSelectedSuggestion', 'Accept Selected Suggestion'),
f1: false,
precondition: ContextKeyExpr.and(ContextKeyExpr.or(TerminalContextKeys.processSupported, TerminalContextKeys.terminalHasBeenCreated), TerminalContextKeys.focus, TerminalContextKeys.isOpen, TerminalContextKeys.suggestWidgetVisible),
keybinding: {
primary: KeyCode.Enter,
secondary: [KeyCode.Tab],
// Enter is bound to other workbench keybindings that this needs to beat
weight: KeybindingWeight.WorkbenchContrib + 1
},
run: (activeInstance) => TerminalSuggestContribution.get(activeInstance)?.addon?.acceptSelectedSuggestion()
});
registerActiveInstanceAction({
id: TerminalSuggestCommandId.HideSuggestWidget,
title: localize2('workbench.action.terminal.hideSuggestWidget', 'Hide Suggest Widget'),
f1: false,
precondition: ContextKeyExpr.and(ContextKeyExpr.or(TerminalContextKeys.processSupported, TerminalContextKeys.terminalHasBeenCreated), TerminalContextKeys.focus, TerminalContextKeys.isOpen, TerminalContextKeys.suggestWidgetVisible),
keybinding: {
primary: KeyCode.Escape,
// Escape is bound to other workbench keybindings that this needs to beat
weight: KeybindingWeight.WorkbenchContrib + 1
},
run: (activeInstance) => TerminalSuggestContribution.get(activeInstance)?.addon?.hideSuggestWidget()
});
// #endregion