Skip to content

Commit a349647

Browse files
committed
use debug config provider for non workspace debugging
1 parent 0aa2135 commit a349647

File tree

6 files changed

+71
-57
lines changed

6 files changed

+71
-57
lines changed

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"theme": "dark"
2424
},
2525
"engines": {
26-
"vscode": "^1.9.0"
26+
"vscode": "^1.17.0"
2727
},
2828
"recommendations": [
2929
"donjayamanne.jupyter"
@@ -46,7 +46,6 @@
4646
"activationEvents": [
4747
"onDebug",
4848
"onLanguage:python",
49-
"onCommand:python.python-debug.startSession",
5049
"onCommand:python.execInTerminal",
5150
"onCommand:python.sortImports",
5251
"onCommand:python.runtests",
@@ -279,7 +278,6 @@
279278
"languages": [
280279
"python"
281280
],
282-
"startSessionCommand": "python.python-debug.startSession",
283281
"enableBreakpointsFor": {
284282
"languageIds": [
285283
"python",
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import * as path from 'path';
2+
import { PythonSettings } from '../../common/configSettings';
3+
import { CancellationToken, DebugConfiguration, DebugConfigurationProvider, ProviderResult, Uri, window, workspace, WorkspaceFolder } from 'vscode';
4+
5+
type PythonDebugConfiguration = DebugConfiguration & {
6+
stopOnEntry?: boolean,
7+
pythonPath?: string,
8+
program?: string,
9+
cwd?: string,
10+
env?: object,
11+
envFile?: string,
12+
debugOptions?: string[]
13+
};
14+
15+
export class SimpleConfigurationProvider implements DebugConfigurationProvider {
16+
private getProgram(config: PythonDebugConfiguration): string | undefined {
17+
const editor = window.activeTextEditor;
18+
if (editor && editor.document.languageId === 'python') {
19+
return editor.document.fileName;
20+
}
21+
return undefined;
22+
}
23+
private getWorkspaceFolder(config: PythonDebugConfiguration): string | undefined {
24+
const program = this.getProgram(config);
25+
if (!Array.isArray(workspace.workspaceFolders) || workspace.workspaceFolders.length === 0) {
26+
return program ? path.dirname(program) : undefined;
27+
}
28+
if (workspace.workspaceFolders.length === 1) {
29+
return workspace.workspaceFolders[0].uri.fsPath;
30+
}
31+
if (program) {
32+
const workspaceFolder = workspace.getWorkspaceFolder(Uri.file(program));
33+
if (workspaceFolder) {
34+
return workspaceFolder.uri.fsPath;
35+
}
36+
}
37+
38+
return undefined;
39+
}
40+
resolveDebugConfiguration(folder: WorkspaceFolder | undefined, debugConfiguration: DebugConfiguration, token?: CancellationToken): ProviderResult<DebugConfiguration> {
41+
if (Object.keys(debugConfiguration).length > 0) {
42+
return debugConfiguration;
43+
}
44+
45+
const config = debugConfiguration as PythonDebugConfiguration;
46+
const defaultProgram = this.getProgram(config);
47+
const workspaceFolder = this.getWorkspaceFolder(config);
48+
const envFile = workspaceFolder ? path.join(workspaceFolder, '.env') : undefined;
49+
return {
50+
name: 'Launch',
51+
type: 'python',
52+
request: 'launch',
53+
stopOnEntry: true,
54+
pythonPath: PythonSettings.getInstance().pythonPath,
55+
program: defaultProgram,
56+
cwd: workspaceFolder,
57+
envFile,
58+
env: {},
59+
debugOptions: [
60+
'WaitOnAbnormalExit',
61+
'WaitOnNormalExit',
62+
'RedirectOutput'
63+
]
64+
};
65+
}
66+
}

src/client/debugger/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './configProviders/simpleProvider';

src/client/extension.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ import { WorkspaceSymbols } from './workspaceSymbols/main';
2828
import { BlockFormatProviders } from './typeFormatters/blockFormatProvider';
2929
import * as os from 'os';
3030
import * as fs from 'fs';
31-
import { activateSingleFileDebug } from './singleFileDebug';
3231
import { getPathFromPythonCommand } from './common/utils';
3332
import { JupyterProvider } from './jupyter/provider';
3433
import { activateGoToObjectDefinitionProvider } from './providers/objectDefinitionProvider';
3534
import { InterpreterManager } from './interpreter';
35+
import { SimpleConfigurationProvider } from './debugger';
3636

3737
const PYTHON: vscode.DocumentFilter = { language: 'python' };
3838
let unitTestOutChannel: vscode.OutputChannel;
@@ -152,7 +152,7 @@ export async function activate(context: vscode.ExtensionContext) {
152152
const hepProvider = new HelpProvider();
153153
context.subscriptions.push(hepProvider);
154154

155-
context.subscriptions.push(activateSingleFileDebug());
155+
context.subscriptions.push(vscode.debug.registerDebugConfigurationProvider('python', new SimpleConfigurationProvider()));
156156
}
157157

158158
class PythonExt implements vscode.Disposable {

src/client/singleFileDebug.ts

Lines changed: 0 additions & 51 deletions
This file was deleted.

src/test/autocomplete/base.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ suite('Autocomplete', () => {
114114
const position = new vscode.Position(10, 9);
115115
const list = await vscode.commands.executeCommand<vscode.CompletionList>('vscode.executeCompletionItemProvider', textDocument.uri, position);
116116
assert.notEqual(list.items.filter(item => item.label === 'sleep').length, 0, 'sleep not found');
117-
assert.notEqual(list.items.filter(item => item.documentation.startsWith("Delay execution for a given number of seconds. The argument may be")).length, 0, 'Documentation incorrect');
117+
assert.notEqual(list.items.filter(item => item.documentation.toString().startsWith("Delay execution for a given number of seconds. The argument may be")).length, 0, 'Documentation incorrect');
118118
});
119119

120120
test('For custom class', done => {

0 commit comments

Comments
 (0)