-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathextension.ts
More file actions
233 lines (200 loc) · 12.6 KB
/
extension.ts
File metadata and controls
233 lines (200 loc) · 12.6 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved.
* See 'LICENSE' in the project root for license information.
* ------------------------------------------------------------------------------------------ */
import * as chokidar from 'chokidar';
import * as os from 'os';
import { Configuration } from 'ssh-config';
import * as vscode from 'vscode';
import * as nls from 'vscode-nls';
import { CppSettings } from '../LanguageServer/settings';
import { BaseNode, addSshTargetCmd, refreshCppSshTargetsViewCmd } from '../SSH/TargetsView/common';
import { SshTargetsProvider, getActiveSshTarget, initializeSshTargets, selectSshTarget } from '../SSH/TargetsView/sshTargetsProvider';
import { TargetLeafNode, setActiveSshTarget } from '../SSH/TargetsView/targetNodes';
import { sshCommandToConfig } from '../SSH/sshCommandToConfig';
import { getSshConfiguration, getSshConfigurationFiles, parseFailures, writeSshConfiguration } from '../SSH/sshHosts';
import { pathAccessible } from '../common';
import { instrument } from '../instrumentation';
import { getSshChannel } from '../logger';
import { AttachItemsProvider, AttachPicker, RemoteAttachPicker } from './attachToProcess';
import { ConfigurationAssetProviderFactory, ConfigurationSnippetProvider, DebugConfigurationProvider, IConfigurationAssetProvider } from './configurationProvider';
import { DebuggerType } from './configurations';
import { CppdbgDebugAdapterDescriptorFactory, CppvsdbgDebugAdapterDescriptorFactory } from './debugAdapterDescriptorFactory';
import { NativeAttachItemsProviderFactory } from './nativeAttach';
// The extension deactivate method is asynchronous, so we handle the disposables ourselves instead of using extensionContext.subscriptions.
const disposables: vscode.Disposable[] = [];
const localize: nls.LocalizeFunc = nls.loadMessageBundle();
let sshTargetsViewEnabled: boolean = false;
let sshTargetsViewSetting: string | undefined;
let sshConfigWatcher: chokidar.FSWatcher | undefined;
export async function initialize(context: vscode.ExtensionContext): Promise<void> {
// Activate Process Picker Commands
const attachItemsProvider: AttachItemsProvider = NativeAttachItemsProviderFactory.Get();
const attacher: AttachPicker = new AttachPicker(attachItemsProvider);
disposables.push(vscode.commands.registerCommand('extension.pickNativeProcess', () => attacher.ShowAttachEntries()));
const remoteAttacher: RemoteAttachPicker = new RemoteAttachPicker();
disposables.push(vscode.commands.registerCommand('extension.pickRemoteNativeProcess', (any) => remoteAttacher.ShowAttachEntries(any)));
// Activate ConfigurationProvider
const assetProvider: IConfigurationAssetProvider = ConfigurationAssetProviderFactory.getConfigurationProvider();
// Register DebugConfigurationProviders for "Run and Debug" in Debug Panel.
// On Windows platforms, the cppvsdbg debugger will also be registered for initial configurations.
let cppVsDebugProvider: DebugConfigurationProvider | null = null;
if (os.platform() === 'win32') {
cppVsDebugProvider = new DebugConfigurationProvider(assetProvider, DebuggerType.cppvsdbg);
disposables.push(vscode.debug.registerDebugConfigurationProvider(DebuggerType.cppvsdbg, instrument(cppVsDebugProvider), vscode.DebugConfigurationProviderTriggerKind.Dynamic));
}
const cppDebugProvider: DebugConfigurationProvider = new DebugConfigurationProvider(assetProvider, DebuggerType.cppdbg);
disposables.push(vscode.debug.registerDebugConfigurationProvider(DebuggerType.cppdbg, instrument(cppDebugProvider), vscode.DebugConfigurationProviderTriggerKind.Dynamic));
// Register DebugConfigurationProviders for "Run and Debug" play button.
const debugProvider: DebugConfigurationProvider = new DebugConfigurationProvider(assetProvider, DebuggerType.all);
// eslint-disable-next-line @typescript-eslint/no-misused-promises
disposables.push(vscode.commands.registerTextEditorCommand("C_Cpp.BuildAndDebugFile", async (textEditor: vscode.TextEditor, _edit: vscode.TextEditorEdit, ..._args: any[]) => { await debugProvider.buildAndDebug(textEditor); }));
// eslint-disable-next-line @typescript-eslint/no-misused-promises
disposables.push(vscode.commands.registerTextEditorCommand("C_Cpp.BuildAndRunFile", async (textEditor: vscode.TextEditor, _edit: vscode.TextEditorEdit, ..._args: any[]) => { await debugProvider.buildAndRun(textEditor); }));
// eslint-disable-next-line @typescript-eslint/no-misused-promises
disposables.push(vscode.commands.registerTextEditorCommand("C_Cpp.AddDebugConfiguration", async (textEditor: vscode.TextEditor, _edit: vscode.TextEditorEdit, ..._args: any[]) => {
const folder: vscode.WorkspaceFolder | undefined = vscode.workspace.getWorkspaceFolder(textEditor.document.uri);
if (!folder) {
void vscode.window.showWarningMessage(localize("add.debug.configuration.not.available.for.single.file", "Add debug configuration is not available for single file."));
}
await debugProvider.addDebugConfiguration(textEditor);
}));
assetProvider.getConfigurationSnippets();
const launchJsonDocumentSelector: vscode.DocumentSelector = [{
scheme: 'file',
language: 'jsonc',
pattern: '**/launch.json'
}];
// ConfigurationSnippetProvider needs to be initiallized after configurationProvider calls getConfigurationSnippets.
disposables.push(vscode.languages.registerCompletionItemProvider(launchJsonDocumentSelector, instrument(new ConfigurationSnippetProvider(assetProvider))));
// Register Debug Adapters
disposables.push(vscode.debug.registerDebugAdapterDescriptorFactory(DebuggerType.cppvsdbg, new CppvsdbgDebugAdapterDescriptorFactory(context)));
disposables.push(vscode.debug.registerDebugAdapterDescriptorFactory(DebuggerType.cppdbg, new CppdbgDebugAdapterDescriptorFactory(context)));
// SSH Targets View
await initializeSshTargets();
const sshTargetsProvider: SshTargetsProvider = new SshTargetsProvider();
disposables.push(vscode.window.registerTreeDataProvider('CppSshTargetsView', instrument(sshTargetsProvider)));
disposables.push(vscode.commands.registerCommand(addSshTargetCmd, () => enableSshTargetsViewAndRun(addSshTargetImpl)));
disposables.push(vscode.commands.registerCommand('C_Cpp.removeSshTarget', (node?: BaseNode) => enableSshTargetsViewAndRun(removeSshTargetImpl, node)));
disposables.push(vscode.commands.registerCommand(refreshCppSshTargetsViewCmd, (node?: BaseNode) => enableSshTargetsViewAndRun((node?: BaseNode) => sshTargetsProvider.refresh(node), node)));
disposables.push(vscode.commands.registerCommand('C_Cpp.setActiveSshTarget', async (node: TargetLeafNode) => {
await enableSshTargetsView();
await setActiveSshTarget(node.name);
await vscode.commands.executeCommand(refreshCppSshTargetsViewCmd);
}));
disposables.push(vscode.commands.registerCommand('C_Cpp.selectSshTarget', () => enableSshTargetsViewAndRun(selectSshTarget)));
disposables.push(vscode.commands.registerCommand('C_Cpp.selectActiveSshTarget', async () => {
await enableSshTargetsView();
const name: string | undefined = await selectSshTarget();
if (name) {
await setActiveSshTarget(name);
await vscode.commands.executeCommand(refreshCppSshTargetsViewCmd);
}
}));
disposables.push(vscode.commands.registerCommand('C_Cpp.activeSshTarget', () => enableSshTargetsViewAndRun(getActiveSshTarget)));
disposables.push(vscode.commands.registerCommand('C_Cpp.sshTerminal', (node: TargetLeafNode) => enableSshTargetsViewAndRun(sshTerminal, node)));
disposables.push(sshTargetsProvider);
// Decide if we should show the SSH Targets View.
sshTargetsViewSetting = new CppSettings().sshTargetsView;
// Active SSH Target initialized in initializeSshTargets()
if (sshTargetsViewSetting === 'enabled' || (sshTargetsViewSetting === 'default' && await getActiveSshTarget(false))) {
// Don't wait
void enableSshTargetsView();
}
disposables.push(vscode.workspace.onDidChangeConfiguration(async (e: vscode.ConfigurationChangeEvent) => {
if (e.affectsConfiguration('C_Cpp.sshTargetsView')) {
sshTargetsViewSetting = new CppSettings().sshTargetsView;
if (sshTargetsViewSetting === 'enabled' || (sshTargetsViewSetting === 'default' && await getActiveSshTarget(false))) {
await enableSshTargetsView();
} else if (sshTargetsViewSetting === 'disabled') {
await disableSshTargetsView();
}
}
}));
}
export function dispose(): void {
if (sshConfigWatcher) {
void sshConfigWatcher.close();
sshConfigWatcher = undefined;
}
disposables.forEach(d => d.dispose());
}
function sshTerminal(node: TargetLeafNode): void {
const terminal: vscode.Terminal = vscode.window.createTerminal(`SSH: ${node.name}`);
terminal.sendText(`ssh "${node.name}"`);
terminal.show();
}
async function enableSshTargetsViewAndRun<T>(func: (...paras: any[]) => T | Promise<T>, ...args: any[]): Promise<T> {
await enableSshTargetsView();
return func(...args);
}
async function enableSshTargetsView(): Promise<void> {
if (sshTargetsViewEnabled || sshTargetsViewSetting === 'disabled') {
return;
}
await vscode.commands.executeCommand('setContext', 'cpptools.enableSshTargetsView', true);
sshConfigWatcher = chokidar.watch(getSshConfigurationFiles(), { ignoreInitial: true })
.on('add', () => void vscode.commands.executeCommand(refreshCppSshTargetsViewCmd))
.on('change', () => void vscode.commands.executeCommand(refreshCppSshTargetsViewCmd))
.on('unlink', () => void vscode.commands.executeCommand(refreshCppSshTargetsViewCmd));
sshTargetsViewEnabled = true;
}
async function disableSshTargetsView(): Promise<void> {
await vscode.commands.executeCommand('setContext', 'cpptools.enableSshTargetsView', false);
if (sshConfigWatcher) {
void sshConfigWatcher.close();
sshConfigWatcher = undefined;
}
sshTargetsViewEnabled = false;
}
async function addSshTargetImpl(): Promise<string> {
const validConfigFiles: string[] = [];
for (const configFile of getSshConfigurationFiles()) {
if (await pathAccessible(configFile) && parseFailures.get(configFile)) {
getSshChannel().appendLine(localize('cannot.modify.config.file', 'Cannot modify SSH configuration file because of parse failure "{0}".', configFile));
} else {
validConfigFiles.push(configFile);
}
}
if (validConfigFiles.length === 0) {
throw new Error(localize('no.valid.ssh.config.file', 'No valid SSH configuration file found.'));
}
const name: string | undefined = await vscode.window.showInputBox({
title: localize('enter.ssh.target.name', 'Enter SSH Target Name'),
placeHolder: localize('ssh.target.name.place.holder', 'Example: `mySSHTarget`'),
ignoreFocusOut: true
});
if (name === undefined) {
// Cancelled
return '';
}
const command: string | undefined = await vscode.window.showInputBox({
title: localize('enter.ssh.connection.command', 'Enter SSH Connection Command'),
placeHolder: localize('ssh.connection.command.place.holder', 'Example: `ssh hello@microsoft.com -A`'),
ignoreFocusOut: true
});
if (!command) {
return '';
}
const newEntry: { [key: string]: string } = sshCommandToConfig(command, name);
const targetFile: string | undefined = await vscode.window.showQuickPick(validConfigFiles, { title: localize('select.ssh.config.file', 'Select an SSH configuration file') });
if (!targetFile) {
return '';
}
const parsedSshConfig: Configuration = await getSshConfiguration(targetFile, false);
parsedSshConfig.prepend(newEntry, true);
await writeSshConfiguration(targetFile, parsedSshConfig);
return name;
}
async function removeSshTargetImpl(node: TargetLeafNode): Promise<boolean> {
const labelYes: string = localize('yes', 'Yes');
const labelNo: string = localize('no', 'No');
const confirm: string | undefined = await vscode.window.showInformationMessage(localize('ssh.target.delete.confirmation', 'Are you sure you want to permanently delete "{0}"?', node.name), labelYes, labelNo);
if (!confirm || confirm === labelNo) {
return false;
}
const parsedSshConfig: Configuration = await getSshConfiguration(node.sshConfigHostInfo.file, false);
parsedSshConfig.remove({ Host: node.name });
await writeSshConfiguration(node.sshConfigHostInfo.file, parsedSshConfig);
return true;
}