From 0aa2135f4147513c73314733f74ad815386e4dee Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Mon, 9 Oct 2017 10:47:12 -0700 Subject: [PATCH 1/2] add onDebug activation --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index c764221f46cd..d4bd517c18ea 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,7 @@ "Other" ], "activationEvents": [ + "onDebug", "onLanguage:python", "onCommand:python.python-debug.startSession", "onCommand:python.execInTerminal", @@ -1570,4 +1571,4 @@ "vscode": "^1.0.3", "webpack": "^1.13.2" } -} +} \ No newline at end of file From a34964749cdcd0d048dcd646312928242277e871 Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Mon, 9 Oct 2017 12:23:13 -0700 Subject: [PATCH 2/2] use debug config provider for non workspace debugging --- package.json | 4 +- .../configProviders/simpleProvider.ts | 66 +++++++++++++++++++ src/client/debugger/index.ts | 1 + src/client/extension.ts | 4 +- src/client/singleFileDebug.ts | 51 -------------- src/test/autocomplete/base.test.ts | 2 +- 6 files changed, 71 insertions(+), 57 deletions(-) create mode 100644 src/client/debugger/configProviders/simpleProvider.ts create mode 100644 src/client/debugger/index.ts delete mode 100644 src/client/singleFileDebug.ts diff --git a/package.json b/package.json index d4bd517c18ea..30c5284fbcb4 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "theme": "dark" }, "engines": { - "vscode": "^1.9.0" + "vscode": "^1.17.0" }, "recommendations": [ "donjayamanne.jupyter" @@ -46,7 +46,6 @@ "activationEvents": [ "onDebug", "onLanguage:python", - "onCommand:python.python-debug.startSession", "onCommand:python.execInTerminal", "onCommand:python.sortImports", "onCommand:python.runtests", @@ -279,7 +278,6 @@ "languages": [ "python" ], - "startSessionCommand": "python.python-debug.startSession", "enableBreakpointsFor": { "languageIds": [ "python", diff --git a/src/client/debugger/configProviders/simpleProvider.ts b/src/client/debugger/configProviders/simpleProvider.ts new file mode 100644 index 000000000000..efb069a67b16 --- /dev/null +++ b/src/client/debugger/configProviders/simpleProvider.ts @@ -0,0 +1,66 @@ +import * as path from 'path'; +import { PythonSettings } from '../../common/configSettings'; +import { CancellationToken, DebugConfiguration, DebugConfigurationProvider, ProviderResult, Uri, window, workspace, WorkspaceFolder } from 'vscode'; + +type PythonDebugConfiguration = DebugConfiguration & { + stopOnEntry?: boolean, + pythonPath?: string, + program?: string, + cwd?: string, + env?: object, + envFile?: string, + debugOptions?: string[] +}; + +export class SimpleConfigurationProvider implements DebugConfigurationProvider { + private getProgram(config: PythonDebugConfiguration): string | undefined { + const editor = window.activeTextEditor; + if (editor && editor.document.languageId === 'python') { + return editor.document.fileName; + } + return undefined; + } + private getWorkspaceFolder(config: PythonDebugConfiguration): string | undefined { + const program = this.getProgram(config); + if (!Array.isArray(workspace.workspaceFolders) || workspace.workspaceFolders.length === 0) { + return program ? path.dirname(program) : undefined; + } + if (workspace.workspaceFolders.length === 1) { + return workspace.workspaceFolders[0].uri.fsPath; + } + if (program) { + const workspaceFolder = workspace.getWorkspaceFolder(Uri.file(program)); + if (workspaceFolder) { + return workspaceFolder.uri.fsPath; + } + } + + return undefined; + } + resolveDebugConfiguration(folder: WorkspaceFolder | undefined, debugConfiguration: DebugConfiguration, token?: CancellationToken): ProviderResult { + if (Object.keys(debugConfiguration).length > 0) { + return debugConfiguration; + } + + const config = debugConfiguration as PythonDebugConfiguration; + const defaultProgram = this.getProgram(config); + const workspaceFolder = this.getWorkspaceFolder(config); + const envFile = workspaceFolder ? path.join(workspaceFolder, '.env') : undefined; + return { + name: 'Launch', + type: 'python', + request: 'launch', + stopOnEntry: true, + pythonPath: PythonSettings.getInstance().pythonPath, + program: defaultProgram, + cwd: workspaceFolder, + envFile, + env: {}, + debugOptions: [ + 'WaitOnAbnormalExit', + 'WaitOnNormalExit', + 'RedirectOutput' + ] + }; + } +} diff --git a/src/client/debugger/index.ts b/src/client/debugger/index.ts new file mode 100644 index 000000000000..d2343dd26a9e --- /dev/null +++ b/src/client/debugger/index.ts @@ -0,0 +1 @@ +export * from './configProviders/simpleProvider'; \ No newline at end of file diff --git a/src/client/extension.ts b/src/client/extension.ts index 4a468327b959..867d2ab113b6 100644 --- a/src/client/extension.ts +++ b/src/client/extension.ts @@ -28,11 +28,11 @@ import { WorkspaceSymbols } from './workspaceSymbols/main'; import { BlockFormatProviders } from './typeFormatters/blockFormatProvider'; import * as os from 'os'; import * as fs from 'fs'; -import { activateSingleFileDebug } from './singleFileDebug'; import { getPathFromPythonCommand } from './common/utils'; import { JupyterProvider } from './jupyter/provider'; import { activateGoToObjectDefinitionProvider } from './providers/objectDefinitionProvider'; import { InterpreterManager } from './interpreter'; +import { SimpleConfigurationProvider } from './debugger'; const PYTHON: vscode.DocumentFilter = { language: 'python' }; let unitTestOutChannel: vscode.OutputChannel; @@ -152,7 +152,7 @@ export async function activate(context: vscode.ExtensionContext) { const hepProvider = new HelpProvider(); context.subscriptions.push(hepProvider); - context.subscriptions.push(activateSingleFileDebug()); + context.subscriptions.push(vscode.debug.registerDebugConfigurationProvider('python', new SimpleConfigurationProvider())); } class PythonExt implements vscode.Disposable { diff --git a/src/client/singleFileDebug.ts b/src/client/singleFileDebug.ts deleted file mode 100644 index 540840fdd00d..000000000000 --- a/src/client/singleFileDebug.ts +++ /dev/null @@ -1,51 +0,0 @@ -import * as vscode from 'vscode'; -import { dirname, join } from 'path'; -import { PythonSettings } from './common/configSettings'; - -export function activateSingleFileDebug() { - return vscode.commands.registerCommand('python.python-debug.startSession', config => { - - if (!config.request) { // if 'request' is missing interpret this as a missing launch.json - config.type = 'python'; - config.name = 'Launch'; - config.request = 'launch'; - config.pythonPath = PythonSettings.getInstance().pythonPath; - config.debugOptions = [ - "WaitOnAbnormalExit", - "WaitOnNormalExit", - "RedirectOutput" - ]; - config.stopOnEntry = true; - config.module = ''; - config.args = []; - config.console = "none"; - config.exceptionHandling = []; - config.env = {}; - - if (vscode.workspace.rootPath) { - config.cwd = vscode.workspace.rootPath; - } - - if (!config.program) { - const editor = vscode.window.activeTextEditor; - if (editor && editor.document.languageId === 'python') { - config.program = editor.document.fileName; - } - } - - if (!config.cwd && config.program) { - // fall back if 'cwd' not known: derive it from 'program' - config.cwd = dirname(config.program); - } - if (vscode.workspace && vscode.workspace.rootPath) { - config.envFile = join(vscode.workspace.rootPath, '.env'); - } - if (!config.envFile && typeof config.cwd === 'string' && config.cwd.lengths > 0) { - config.envFile = join(config.cwd, '.env'); - } - - } - - vscode.commands.executeCommand('vscode.startDebug', config); - }); -} \ No newline at end of file diff --git a/src/test/autocomplete/base.test.ts b/src/test/autocomplete/base.test.ts index 609bed153456..83123b308f91 100644 --- a/src/test/autocomplete/base.test.ts +++ b/src/test/autocomplete/base.test.ts @@ -114,7 +114,7 @@ suite('Autocomplete', () => { const position = new vscode.Position(10, 9); const list = await vscode.commands.executeCommand('vscode.executeCompletionItemProvider', textDocument.uri, position); assert.notEqual(list.items.filter(item => item.label === 'sleep').length, 0, 'sleep not found'); - 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'); + 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'); }); test('For custom class', done => {