|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +import { injectable, unmanaged } from 'inversify'; |
| 7 | +import * as path from 'path'; |
| 8 | +import { CancellationToken, DebugConfiguration, DebugConfigurationProvider, ProviderResult, Uri, WorkspaceFolder } from 'vscode'; |
| 9 | +import { IDocumentManager, IWorkspaceService } from '../../common/application/types'; |
| 10 | +import { PythonLanguage } from '../../common/constants'; |
| 11 | +import { IConfigurationService } from '../../common/types'; |
| 12 | +import { IServiceContainer } from '../../ioc/types'; |
| 13 | +import { DebuggerType, LaunchRequestArguments } from '../Common/Contracts'; |
| 14 | + |
| 15 | +// tslint:disable:no-invalid-template-strings |
| 16 | + |
| 17 | +export type PythonDebugConfiguration = DebugConfiguration & LaunchRequestArguments; |
| 18 | + |
| 19 | +@injectable() |
| 20 | +export abstract class BaseConfigurationProvider implements DebugConfigurationProvider { |
| 21 | + constructor(@unmanaged() public debugType: DebuggerType, private serviceContainer: IServiceContainer) { } |
| 22 | + public resolveDebugConfiguration(folder: WorkspaceFolder | undefined, debugConfiguration: DebugConfiguration, token?: CancellationToken): ProviderResult<DebugConfiguration> { |
| 23 | + const config = debugConfiguration as PythonDebugConfiguration; |
| 24 | + const numberOfSettings = Object.keys(config); |
| 25 | + const provideDefaultConfigSettings = (config.noDebug === true && numberOfSettings.length === 1) || numberOfSettings.length === 0; |
| 26 | + const workspaceFolder = this.getWorkspaceFolder(folder, config); |
| 27 | + if (!provideDefaultConfigSettings) { |
| 28 | + this.resolveAndUpdatePythonPath(workspaceFolder, config); |
| 29 | + return config; |
| 30 | + } |
| 31 | + |
| 32 | + const configService = this.serviceContainer.get<IConfigurationService>(IConfigurationService); |
| 33 | + const pythonPath = configService.getSettings(workspaceFolder).pythonPath; |
| 34 | + const defaultProgram = this.getProgram(config); |
| 35 | + const envFile = workspaceFolder ? path.join(workspaceFolder.fsPath, '.env') : ''; |
| 36 | + |
| 37 | + config.name = 'Launch'; |
| 38 | + config.type = this.debugType; |
| 39 | + config.request = 'launch'; |
| 40 | + config.pythonPath = pythonPath; |
| 41 | + config.program = defaultProgram ? defaultProgram : ''; |
| 42 | + config.cwd = workspaceFolder ? workspaceFolder.fsPath : undefined; |
| 43 | + config.envFile = envFile; |
| 44 | + config.env = {}; |
| 45 | + config.debugOptions = []; |
| 46 | + |
| 47 | + this.provideDefaults(config); |
| 48 | + return config; |
| 49 | + } |
| 50 | + protected abstract provideDefaults(debugConfiguration: PythonDebugConfiguration): void; |
| 51 | + private getWorkspaceFolder(folder: WorkspaceFolder | undefined, config: PythonDebugConfiguration): Uri | undefined { |
| 52 | + if (folder) { |
| 53 | + return folder.uri; |
| 54 | + } |
| 55 | + const program = this.getProgram(config); |
| 56 | + const workspaceService = this.serviceContainer.get<IWorkspaceService>(IWorkspaceService); |
| 57 | + if (!Array.isArray(workspaceService.workspaceFolders) || workspaceService.workspaceFolders.length === 0) { |
| 58 | + return program ? Uri.file(path.dirname(program)) : undefined; |
| 59 | + } |
| 60 | + if (workspaceService.workspaceFolders.length === 1) { |
| 61 | + return workspaceService.workspaceFolders[0].uri; |
| 62 | + } |
| 63 | + if (program) { |
| 64 | + const workspaceFolder = workspaceService.getWorkspaceFolder(Uri.file(program)); |
| 65 | + if (workspaceFolder) { |
| 66 | + return workspaceFolder.uri; |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + private getProgram(config: PythonDebugConfiguration): string | undefined { |
| 71 | + const documentManager = this.serviceContainer.get<IDocumentManager>(IDocumentManager); |
| 72 | + const editor = documentManager.activeTextEditor; |
| 73 | + if (editor && editor.document.languageId === PythonLanguage.language) { |
| 74 | + return editor.document.fileName; |
| 75 | + } |
| 76 | + } |
| 77 | + private resolveAndUpdatePythonPath(workspaceFolder: Uri | undefined, debugConfiguration: PythonDebugConfiguration): void { |
| 78 | + if (!debugConfiguration || debugConfiguration.pythonPath !== '${config:python.pythonPath}') { |
| 79 | + return; |
| 80 | + } |
| 81 | + const configService = this.serviceContainer.get<IConfigurationService>(IConfigurationService); |
| 82 | + const pythonPath = configService.getSettings(workspaceFolder).pythonPath; |
| 83 | + debugConfiguration.pythonPath = pythonPath; |
| 84 | + } |
| 85 | +} |
0 commit comments