@@ -11,34 +11,55 @@ import { PythonLanguage } from '../../common/constants';
1111import { IFileSystem , IPlatformService } from '../../common/platform/types' ;
1212import { IConfigurationService } from '../../common/types' ;
1313import { IServiceContainer } from '../../ioc/types' ;
14- import { DebuggerType , DebugOptions , LaunchRequestArguments } from '../Common/Contracts' ;
14+ import { AttachRequestArguments , DebuggerType , DebugOptions , LaunchRequestArguments } from '../Common/Contracts' ;
1515
1616// tslint:disable:no-invalid-template-strings
1717
18- export type PythonDebugConfiguration = DebugConfiguration & LaunchRequestArguments ;
18+ export type PythonLaunchDebugConfiguration = DebugConfiguration & LaunchRequestArguments ;
19+ export type PythonAttachDebugConfiguration = DebugConfiguration & AttachRequestArguments ;
1920
2021@injectable ( )
2122export abstract class BaseConfigurationProvider implements DebugConfigurationProvider {
2223 constructor ( @unmanaged ( ) public debugType : DebuggerType , protected serviceContainer : IServiceContainer ) { }
2324 public resolveDebugConfiguration ( folder : WorkspaceFolder | undefined , debugConfiguration : DebugConfiguration , token ?: CancellationToken ) : ProviderResult < DebugConfiguration > {
24- const config = debugConfiguration as PythonDebugConfiguration ;
25- const numberOfSettings = Object . keys ( config ) ;
26- const workspaceFolder = this . getWorkspaceFolder ( folder , config ) ;
25+ const workspaceFolder = this . getWorkspaceFolder ( folder ) ;
2726
28- if ( ( config . noDebug === true && numberOfSettings . length === 1 ) || numberOfSettings . length === 0 ) {
29- const defaultProgram = this . getProgram ( config ) ;
27+ if ( debugConfiguration . request === 'attach' ) {
28+ this . provideAttachDefaults ( workspaceFolder , debugConfiguration as PythonAttachDebugConfiguration ) ;
29+ } else {
30+ const config = debugConfiguration as PythonLaunchDebugConfiguration ;
31+ const numberOfSettings = Object . keys ( config ) ;
3032
31- config . name = 'Launch' ;
32- config . type = this . debugType ;
33- config . request = 'launch' ;
34- config . program = defaultProgram ? defaultProgram : '' ;
35- config . env = { } ;
36- }
33+ if ( ( config . noDebug === true && numberOfSettings . length === 1 ) || numberOfSettings . length === 0 ) {
34+ const defaultProgram = this . getProgram ( ) ;
35+
36+ config . name = 'Launch' ;
37+ config . type = this . debugType ;
38+ config . request = 'launch' ;
39+ config . program = defaultProgram ? defaultProgram : '' ;
40+ config . env = { } ;
41+ }
3742
38- this . provideDefaults ( workspaceFolder , config ) ;
39- return config ;
43+ this . provideLaunchDefaults ( workspaceFolder , config ) ;
44+ }
45+ return debugConfiguration ;
46+ }
47+ protected provideAttachDefaults ( workspaceFolder : Uri | undefined , debugConfiguration : PythonAttachDebugConfiguration ) : void {
48+ if ( ! Array . isArray ( debugConfiguration . debugOptions ) ) {
49+ debugConfiguration . debugOptions = [ ] ;
50+ }
51+ // Always redirect output.
52+ if ( debugConfiguration . debugOptions . indexOf ( DebugOptions . RedirectOutput ) === - 1 ) {
53+ debugConfiguration . debugOptions . push ( DebugOptions . RedirectOutput ) ;
54+ }
55+ if ( ! debugConfiguration . host ) {
56+ debugConfiguration . host = 'localhost' ;
57+ }
58+ if ( ! debugConfiguration . localRoot && workspaceFolder ) {
59+ debugConfiguration . localRoot = workspaceFolder . fsPath ;
60+ }
4061 }
41- protected provideDefaults ( workspaceFolder : Uri | undefined , debugConfiguration : PythonDebugConfiguration ) : void {
62+ protected provideLaunchDefaults ( workspaceFolder : Uri | undefined , debugConfiguration : PythonLaunchDebugConfiguration ) : void {
4263 this . resolveAndUpdatePythonPath ( workspaceFolder , debugConfiguration ) ;
4364 if ( typeof debugConfiguration . cwd !== 'string' && workspaceFolder ) {
4465 debugConfiguration . cwd = workspaceFolder . fsPath ;
@@ -75,11 +96,11 @@ export abstract class BaseConfigurationProvider implements DebugConfigurationPro
7596 }
7697 }
7798 }
78- private getWorkspaceFolder ( folder : WorkspaceFolder | undefined , config : PythonDebugConfiguration ) : Uri | undefined {
99+ private getWorkspaceFolder ( folder : WorkspaceFolder | undefined ) : Uri | undefined {
79100 if ( folder ) {
80101 return folder . uri ;
81102 }
82- const program = this . getProgram ( config ) ;
103+ const program = this . getProgram ( ) ;
83104 const workspaceService = this . serviceContainer . get < IWorkspaceService > ( IWorkspaceService ) ;
84105 if ( ! Array . isArray ( workspaceService . workspaceFolders ) || workspaceService . workspaceFolders . length === 0 ) {
85106 return program ? Uri . file ( path . dirname ( program ) ) : undefined ;
@@ -94,14 +115,14 @@ export abstract class BaseConfigurationProvider implements DebugConfigurationPro
94115 }
95116 }
96117 }
97- private getProgram ( config : PythonDebugConfiguration ) : string | undefined {
118+ private getProgram ( ) : string | undefined {
98119 const documentManager = this . serviceContainer . get < IDocumentManager > ( IDocumentManager ) ;
99120 const editor = documentManager . activeTextEditor ;
100121 if ( editor && editor . document . languageId === PythonLanguage . language ) {
101122 return editor . document . fileName ;
102123 }
103124 }
104- private resolveAndUpdatePythonPath ( workspaceFolder : Uri | undefined , debugConfiguration : PythonDebugConfiguration ) : void {
125+ private resolveAndUpdatePythonPath ( workspaceFolder : Uri | undefined , debugConfiguration : PythonLaunchDebugConfiguration ) : void {
105126 if ( ! debugConfiguration ) {
106127 return ;
107128 }
0 commit comments