Skip to content

Commit 77e163c

Browse files
committed
fix #698, debug single file without launch.json
1 parent 3af4277 commit 77e163c

3 files changed

Lines changed: 75 additions & 26 deletions

File tree

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@
257257
"debuggers": [{
258258
"type": "python",
259259
"label": "Python",
260+
"languages": [
261+
"python"
262+
],
263+
"startSessionCommand": "python.python-debug.startSession",
260264
"enableBreakpointsFor": {
261265
"languageIds": [
262266
"python",

src/client/extension.ts

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { WorkspaceSymbols } from './workspaceSymbols/main';
2727
import { BlockFormatProviders } from './typeFormatters/blockFormatProvider';
2828
import * as os from 'os';
2929
import * as fs from 'fs';
30-
30+
import { activateSingleFileDebug } from './singleFileDebug';
3131

3232
const PYTHON: vscode.DocumentFilter = { language: 'python', scheme: 'file' };
3333
let unitTestOutChannel: vscode.OutputChannel;
@@ -65,7 +65,7 @@ export function activate(context: vscode.ExtensionContext) {
6565
context.subscriptions.push(vscode.commands.registerCommand(Commands.Start_REPL, () => {
6666
let term = vscode.window.createTerminal('Python', pythonSettings.pythonPath);
6767
term.show();
68-
context.subscriptions.push(term);
68+
context.subscriptions.push(term);
6969
}));
7070

7171
// Enable indentAction
@@ -117,6 +117,8 @@ export function activate(context: vscode.ExtensionContext) {
117117

118118
const hepProvider = new HelpProvider();
119119
context.subscriptions.push(hepProvider);
120+
121+
context.subscriptions.push(activateSingleFileDebug());
120122
}
121123

122124
// this method is called when your extension is deactivated
@@ -125,32 +127,32 @@ export function deactivate() {
125127

126128
class PythonExt {
127129

128-
private _isDjangoProject: ContextKey;
130+
private _isDjangoProject: ContextKey;
129131

130-
constructor() {
131-
this._isDjangoProject = new ContextKey('python.isDjangoProject');
132-
this._ensureState();
133-
}
132+
constructor() {
133+
this._isDjangoProject = new ContextKey('python.isDjangoProject');
134+
this._ensureState();
135+
}
134136

135-
private _ensureState(): void {
136-
// context: python.isDjangoProject
137-
this._isDjangoProject.set(fs.existsSync(vscode.workspace.rootPath.concat("/manage.py")));
138-
}
137+
private _ensureState(): void {
138+
// context: python.isDjangoProject
139+
this._isDjangoProject.set(fs.existsSync(vscode.workspace.rootPath.concat("/manage.py")));
140+
}
139141
}
140142

141-
class ContextKey {
142-
private _name: string;
143-
private _lastValue: boolean;
144-
145-
constructor(name:string) {
146-
this._name = name;
147-
}
148-
149-
public set(value:boolean): void {
150-
if (this._lastValue === value) {
151-
return;
152-
}
153-
this._lastValue = value;
154-
vscode.commands.executeCommand('setContext', this._name, this._lastValue);
155-
}
143+
class ContextKey {
144+
private _name: string;
145+
private _lastValue: boolean;
146+
147+
constructor(name: string) {
148+
this._name = name;
149+
}
150+
151+
public set(value: boolean): void {
152+
if (this._lastValue === value) {
153+
return;
154+
}
155+
this._lastValue = value;
156+
vscode.commands.executeCommand('setContext', this._name, this._lastValue);
157+
}
156158
}

src/client/singleFileDebug.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import * as vscode from 'vscode';
2+
import { dirname } from 'path';
3+
4+
export function activateSingleFileDebug() {
5+
return vscode.commands.registerCommand('python.python-debug.startSession', config => {
6+
7+
if (!config.request) { // if 'request' is missing interpret this as a missing launch.json
8+
config.type = 'python';
9+
config.name = 'Launch';
10+
config.request = 'launch';
11+
config.pythonPath = "python";
12+
config.debugOptions = [
13+
"WaitOnAbnormalExit",
14+
"WaitOnNormalExit",
15+
"RedirectOutput"
16+
];
17+
config.stopOnEntry = true;
18+
config.module = '';
19+
config.args = [];
20+
config.console = "integratedTerminal";
21+
config.exceptionHandling = [];
22+
config.env = null;
23+
24+
if (vscode.workspace.rootPath) {
25+
config.cwd = vscode.workspace.rootPath;
26+
}
27+
28+
if (!config.program) {
29+
const editor = vscode.window.activeTextEditor;
30+
if (editor && editor.document.languageId === 'python') {
31+
config.program = editor.document.fileName;
32+
}
33+
}
34+
35+
if (!config.cwd && config.program) {
36+
// fall back if 'cwd' not known: derive it from 'program'
37+
config.cwd = dirname(config.program);
38+
}
39+
}
40+
41+
vscode.commands.executeCommand('vscode.startDebug', config);
42+
});
43+
}

0 commit comments

Comments
 (0)