-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
extension.ts
221 lines (198 loc) · 7.36 KB
/
extension.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
let NEXT_TERM_ID = 1;
console.log("Terminals: " + vscode.window.terminals.length);
// vscode.window.onDidOpenTerminal
vscode.window.onDidOpenTerminal(_terminal => {
console.log("Terminal opened. Total count: " + vscode.window.terminals.length);
});
vscode.window.onDidOpenTerminal((terminal: vscode.Terminal) => {
vscode.window.showInformationMessage(`onDidOpenTerminal, name: ${terminal.name}`);
});
// vscode.window.onDidChangeActiveTerminal
vscode.window.onDidChangeActiveTerminal(e => {
console.log(`Active terminal changed, name=${e ? e.name : 'undefined'}`);
});
// vscode.window.createTerminal
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.createTerminal', () => {
vscode.window.createTerminal(`Ext Terminal #${NEXT_TERM_ID++}`);
vscode.window.showInformationMessage('Hello World 2!');
}));
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.createTerminalHideFromUser', () => {
vscode.window.createTerminal({
name: `Ext Terminal #${NEXT_TERM_ID++}`,
hideFromUser: true
});
}));
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.createAndSend', () => {
const terminal = vscode.window.createTerminal(`Ext Terminal #${NEXT_TERM_ID++}`);
terminal.sendText("echo 'Sent text immediately after creating'");
}));
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.createZshLoginShell', () => {
vscode.window.createTerminal(`Ext Terminal #${NEXT_TERM_ID++}`, '/bin/zsh', ['-l']);
}));
// Terminal.hide
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.hide', () => {
if (ensureTerminalExists()) {
selectTerminal().then(terminal => {
if (terminal) {
terminal.hide();
}
});
}
}));
// Terminal.show
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.show', () => {
if (ensureTerminalExists()) {
selectTerminal().then(terminal => {
if (terminal) {
terminal.show();
}
});
}
}));
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.showPreserveFocus', () => {
if (ensureTerminalExists()) {
selectTerminal().then(terminal => {
if (terminal) {
terminal.show(true);
}
});
}
}));
// Terminal.sendText
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.sendText', () => {
if (ensureTerminalExists()) {
selectTerminal().then(terminal => {
if (terminal) {
terminal.sendText("echo 'Hello world!'");
}
});
}
}));
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.sendTextNoNewLine', () => {
if (ensureTerminalExists()) {
selectTerminal().then(terminal => {
if (terminal) {
terminal.sendText("echo 'Hello world!'", false);
}
});
}
}));
// Terminal.dispose
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.dispose', () => {
if (ensureTerminalExists()) {
selectTerminal().then(terminal => {
if (terminal) {
terminal.dispose();
}
});
}
}));
// Terminal.processId
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.processId', () => {
selectTerminal().then(terminal => {
if (!terminal) {
return;
}
terminal.processId.then((processId) => {
if (processId) {
vscode.window.showInformationMessage(`Terminal.processId: ${processId}`);
} else {
vscode.window.showInformationMessage('Terminal does not have a process ID');
}
});
});
}));
// vscode.window.onDidCloseTerminal
vscode.window.onDidCloseTerminal((terminal) => {
vscode.window.showInformationMessage(`onDidCloseTerminal, name: ${terminal.name}`);
});
// vscode.window.terminals
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.terminals', () => {
selectTerminal();
}));
// ExtensionContext.environmentVariableCollection
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.updateEnvironment', () => {
const collection = context.environmentVariableCollection;
collection.replace('FOO', 'BAR');
collection.append('PATH', '/test/path');
}));
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.clearEnvironment', () => {
context.environmentVariableCollection.clear();
}));
// vvv Proposed APIs below vvv
// vscode.window.onDidWriteTerminalData
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.onDidWriteTerminalData', () => {
vscode.window.onDidWriteTerminalData(e => {
vscode.window.showInformationMessage(`onDidWriteTerminalData listener attached, check the devtools console to see events`);
console.log('onDidWriteData', e);
});
}));
// vscode.window.onDidChangeTerminalDimensions
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.onDidChangeTerminalDimensions', () => {
vscode.window.showInformationMessage(`Listening to onDidChangeTerminalDimensions, check the devtools console to see events`);
vscode.window.onDidChangeTerminalDimensions(event => {
console.log(`onDidChangeTerminalDimensions: terminal:${event.terminal.name}, columns=${event.dimensions.columns}, rows=${event.dimensions.rows}`);
});
}));
// vscode.window.registerTerminalLinkProvider
context.subscriptions.push(vscode.commands.registerCommand('terminalTest.registerTerminalLinkProvider', () => {
type CustomTerminalLink = vscode.TerminalLink & {
data: string;
};
vscode.window.registerTerminalLinkProvider(new class implements vscode.TerminalLinkProvider<CustomTerminalLink> {
provideTerminalLinks(context: vscode.TerminalLinkContext, _token: vscode.CancellationToken) {
// Detect the first instance of the word "link" if it exists and linkify it
const startIndex = (context.line as string).indexOf('link');
if (startIndex === -1) {
return [];
}
return [
{
startIndex,
length: 'link'.length,
tooltip: 'Show a notification',
// You can return data in this object to access inside handleTerminalLink
data: 'Example data'
}
];
}
handleTerminalLink(link: CustomTerminalLink) {
vscode.window.showInformationMessage(`Link activated (data = ${link.data})`);
}
});
}));
context.subscriptions.push(vscode.window.registerTerminalProfileProvider('terminalTest.terminal-profile', {
provideTerminalProfile(_token: vscode.CancellationToken): vscode.ProviderResult<vscode.TerminalProfile> {
return {
options: {
name: 'Terminal API',
shellPath: process.title || 'C:/Windows/System32/cmd.exe'
}
};
}
}));
}
function selectTerminal(): Thenable<vscode.Terminal | undefined> {
interface TerminalQuickPickItem extends vscode.QuickPickItem {
terminal: vscode.Terminal;
}
const terminals = vscode.window.terminals;
const items: TerminalQuickPickItem[] = terminals.map(t => {
return {
label: `name: ${t.name}`,
terminal: t
};
});
return vscode.window.showQuickPick(items).then(item => {
return item ? item.terminal : undefined;
});
}
function ensureTerminalExists(): boolean {
if (vscode.window.terminals.length === 0) {
vscode.window.showErrorMessage('No active terminals');
return false;
}
return true;
}