-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathactivatePythonCppDebug.ts
More file actions
259 lines (227 loc) · 8.19 KB
/
activatePythonCppDebug.ts
File metadata and controls
259 lines (227 loc) · 8.19 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
'use strict';
import * as vscode from 'vscode';
import { WorkspaceFolder, DebugConfiguration, ProviderResult, CancellationToken } from 'vscode';
import { PythonCppDebugSession } from './pythonCppDebug';
import * as os from 'os';
export function activatePythonCppDebug(context: vscode.ExtensionContext, factory?: vscode.DebugAdapterDescriptorFactory) {
context.subscriptions.push(
vscode.commands.registerCommand('extension.pythonCpp-debug.runEditorContents', (resource: vscode.Uri) => {
let targetResource = resource;
if (!targetResource && vscode.window.activeTextEditor) {
targetResource = vscode.window.activeTextEditor.document.uri;
}
if (targetResource) {
vscode.debug.startDebugging(undefined, {
type: 'pythoncpp',
name: 'PythonCpp Debug',
request: 'launch',
pythonConfig: 'default',
cppConfig: os.platform().startsWith("win") ? "default (win) Attach" : os.platform() === "darwin" ? "default (lldb) Attach" : "default (gdb) Attach"
},
{ noDebug: true }
);
}
}),
vscode.commands.registerCommand('extension.pythonCpp-debug.debugEditorContents', (resource: vscode.Uri) => {
let targetResource = resource;
if (!targetResource && vscode.window.activeTextEditor) {
targetResource = vscode.window.activeTextEditor.document.uri;
}
if (targetResource) {
vscode.debug.startDebugging(undefined, {
type: 'pythoncpp',
name: 'PythonCpp Debug',
request: 'launch',
pythonConfig: 'default',
cppConfig: os.platform().startsWith("win") ? "default (win) Attach" : os.platform() === "darwin" ? "default (lldb) Attach" : "default (gdb) Attach"
});
}
})
);
// register a configuration provider for 'pythoncpp' debug type
const provider = new PythonCppConfigurationProvider();
context.subscriptions.push(vscode.debug.registerDebugConfigurationProvider('pythoncpp', provider));
if (!factory) {
factory = new InlineDebugAdapterFactory();
}
context.subscriptions.push(vscode.debug.registerDebugAdapterDescriptorFactory('pythoncpp', factory));
if ('dispose' in factory) {
context.subscriptions.push(factory);
}
}
class PythonCppConfigurationProvider implements vscode.DebugConfigurationProvider {
/**
* Check Debug Configuration before DebugSession is launched
*/
resolveDebugConfiguration(
folder: WorkspaceFolder | undefined,
config: DebugConfiguration,
token?: CancellationToken
): ProviderResult<DebugConfiguration | undefined> {
// if launch.json is missing or empty
if (!config.type && !config.request && !config.name) {
let msg = "Please make sure you have a launch.json file with a configuration of type 'pythoncpp' to use this debugger";
return vscode.window.showErrorMessage(msg).then(_ => {
return undefined; // abort launch
});
}
if (!folder) {
let msg = "Working folder not found, open a folder and try again";
return vscode.window.showErrorMessage(msg).then(_ => {
return undefined;
});
}
if (
!config.entirePythonConfig &&
((config.pythonConfig && (config.pythonConfig === 'custom' || config.pythonConfig === 'manual')) || !config.pythonConfig) &&
!config.pythonLaunchName
) {
let msg =
"Make sure to either set 'pythonLaunchName' to the name of " +
"your python configuration or set 'pythonConfig: default'";
return vscode.window.showErrorMessage(msg).then(_ => {
return undefined; // abort launch
});
}
if (
!config.entireCppConfig &&
((config.cppConfig && (config.cppConfig === 'custom' || config.cppConfig === 'manual')) || !config.cppConfig) &&
!config.cppAttachName
) {
let msg =
"Make sure to either set 'cppAttachName' to the name of " +
"your C++ configuration or set 'cppConfig' to the default configuration you wish to use";
return vscode.window.showErrorMessage(msg).then(_ => {
return undefined; // abort launch
});
}
return config;
}
async provideDebugConfigurations(
folder?: vscode.WorkspaceFolder,
token?: vscode.CancellationToken
): Promise<vscode.DebugConfiguration[]> {
interface MenuItem extends vscode.QuickPickItem {
configuration: vscode.DebugConfiguration;
type: string;
}
const lldbConfig: vscode.DebugConfiguration = {
"name": "(lldb) Attach",
"type": "cppdbg",
"request": "attach",
"program": await getPythonPath(null),
"processId": "",
// eslint-disable-next-line @typescript-eslint/naming-convention
"MIMode": "lldb",
"miDebuggerPath": "/path/to/lldb or remove this attribute for the path to be found automatically",
"setupCommands": [
{
"description": "Enable pretty-printing for lldb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
};
const gdbConfig: vscode.DebugConfiguration = {
"name": "(gdb) Attach",
"type": "cppdbg",
"request": "attach",
"program": await getPythonPath(null),
"processId": "",
// eslint-disable-next-line @typescript-eslint/naming-convention
"MIMode": "gdb",
"miDebuggerPath": "/path/to/gdb or remove this attribute for the path to be found automatically",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
};
const winConfig: vscode.DebugConfiguration = {
"name": "(Windows) Attach",
"type": "cppvsdbg",
"request": "attach",
"processId": ""
};
const items: MenuItem[] = [
{ label: "Python C++ Debugger", configuration: winConfig, description: "Default", type: "Default" },
{ label: "Python C++ Debugger", configuration: winConfig, description: "Custom: Windows", type: "(Windows)" },
{ label: "Python C++ Debugger", configuration: gdbConfig, description: "Custom: GDB", type: "(gdb)" },
{ label: "Python C++ Debugger", configuration: lldbConfig, description: "Custom: LLDB", type: "(lldb)" }
];
const selection: MenuItem | undefined = await vscode.window.showQuickPick(items, { placeHolder: "Select a configuration" });
if (!selection || selection.type === "Default") {
const defaultConfig: vscode.DebugConfiguration = {
"name": "Python C++ Debugger",
"type": "pythoncpp",
"request": "launch",
"pythonConfig": "default",
cppConfig: os.platform().startsWith("win") ? "default (win) Attach" : os.platform() === "darwin" ? "default (lldb) Attach" : "default (gdb) Attach"
};
return [defaultConfig];
}
const pythonConfig: vscode.DebugConfiguration = {
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
};
const pythonCppConfig: vscode.DebugConfiguration = {
"name": "Python C++ Debugger",
"type": "pythoncpp",
"request": "launch",
"pythonLaunchName": "Python: Current File",
"cppAttachName": selection.type + " Attach"
};
return [pythonCppConfig, selection.configuration, pythonConfig];
}
}
export async function getPythonPath(document: vscode.TextDocument | null): Promise<string> {
try {
let pyExt = vscode.extensions.getExtension('ms-python.python');
if (!pyExt) {
return 'python';
}
if (pyExt.packageJSON?.featureFlags?.usingNewInterpreterStorage) {
if (!pyExt.isActive) {
await pyExt.activate();
}
const pythonPath = pyExt.exports.settings.getExecutionDetails ?
pyExt.exports.settings.getExecutionDetails(
document?.uri
).execCommand :
pyExt.exports.settings.getExecutionCommand(document?.uri);
return pythonPath ? pythonPath.join(' ') : 'python';
} else {
let path;
if (document) {
path = vscode.workspace.getConfiguration(
'python',
document.uri
).get<string>('pythonPath');
}
else {
path = vscode.workspace.getConfiguration(
'python'
).get<string>('pythonPath');
}
if (!path) {
return 'python';
}
}
} catch (ignored) {
return 'python';
}
return 'python';
}
class InlineDebugAdapterFactory implements vscode.DebugAdapterDescriptorFactory {
createDebugAdapterDescriptor(_session: vscode.DebugSession): ProviderResult<vscode.DebugAdapterDescriptor> {
return new vscode.DebugAdapterInlineImplementation(new PythonCppDebugSession());
}
}