|
| 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 | +} |
0 commit comments