1
1
import * as vscode from "vscode" ;
2
- import { DebugSession , TerminatedEvent } from "@vscode/debugadapter" ;
3
- import { getWorkspaceFolder } from "./workspace" ;
4
- import { Configuration } from "./configuration" ;
5
- import { logToast } from "./logger" ;
6
- import { isValidExecutable } from "./extension" ;
7
-
8
- function getTerminal ( name : string ) : vscode . Terminal {
9
- let i : number ;
10
- for ( i = 0 ; i < vscode . window . terminals . length ; i ++ ) {
11
- if ( vscode . window . terminals [ i ] . name === name ) {
12
- return vscode . window . terminals [ i ] ;
13
- }
14
- }
15
- return vscode . window . createTerminal ( name ) ;
16
- }
2
+ import { DebugSession , ExitedEvent , InitializedEvent , TerminatedEvent } from "@vscode/debugadapter" ;
3
+ import { ExecuteRunpyRun } from "./extension" ;
4
+ import { DebugProtocol } from "@vscode/debugprotocol" ;
5
+ import { logMessage } from "./logger" ;
6
+ import { ChildProcessWithoutNullStreams } from "child_process" ;
17
7
18
8
export class RenpyAdapterDescriptorFactory implements vscode . DebugAdapterDescriptorFactory {
19
9
createDebugAdapterDescriptor ( session : vscode . DebugSession ) : vscode . ProviderResult < vscode . DebugAdapterDescriptor > {
20
- return new vscode . DebugAdapterInlineImplementation ( new RenpyDebugSession ( session . configuration . command , session . configuration . args ) ) ;
10
+ return new vscode . DebugAdapterInlineImplementation ( new RenpyDebugSession ( ) ) ;
21
11
}
22
12
}
23
13
24
14
class RenpyDebugSession extends DebugSession {
25
- private command = "run" ;
26
- private args ?: string [ ] ;
15
+ childProcess : ChildProcessWithoutNullStreams | null = null ;
27
16
28
- public constructor ( command : string , args ?: string [ ] ) {
29
- super ( ) ;
30
- this . command = command ;
31
- if ( args ) {
32
- this . args = args ;
17
+ protected override initializeRequest ( response : DebugProtocol . InitializeResponse ) : void {
18
+ this . sendEvent ( new InitializedEvent ( ) ) ;
19
+
20
+ response . body = { supportTerminateDebuggee : true } ;
21
+
22
+ const childProcess = ExecuteRunpyRun ( ) ;
23
+ if ( childProcess === null ) {
24
+ logMessage ( vscode . LogLevel . Error , "Ren'Py executable location not configured or is invalid." ) ;
25
+ return ;
33
26
}
27
+ this . childProcess = childProcess ;
28
+
29
+ childProcess
30
+ . addListener ( "spawn" , ( ) => {
31
+ const processEvent : DebugProtocol . ProcessEvent = {
32
+ event : "process" ,
33
+ body : {
34
+ name : "Ren'Py" ,
35
+ isLocalProcess : true ,
36
+ startMethod : "launch" ,
37
+ } ,
38
+ seq : 0 ,
39
+ type : "event" ,
40
+ } ;
41
+ if ( childProcess . pid !== undefined ) {
42
+ processEvent . body . systemProcessId = childProcess . pid ;
43
+ }
44
+ this . sendEvent ( processEvent ) ;
45
+ this . sendResponse ( response ) ;
46
+ } )
47
+ . addListener ( "exit" , ( code ) => {
48
+ this . sendEvent ( new ExitedEvent ( code ?? 1 ) ) ;
49
+ this . sendEvent ( new TerminatedEvent ( ) ) ;
50
+ } ) ;
51
+ childProcess . stdout . on ( "data" , ( data ) => {
52
+ logMessage ( vscode . LogLevel . Info , `Ren'Py stdout: ${ data } ` ) ;
53
+ } ) ;
54
+ childProcess . stderr . on ( "data" , ( data ) => {
55
+ logMessage ( vscode . LogLevel . Error , `Ren'Py stderr: ${ data } ` ) ;
56
+ } ) ;
34
57
}
35
58
36
- protected override initializeRequest ( ) : void {
37
- const terminal = getTerminal ( "Ren'py Debug" ) ;
38
- terminal . show ( ) ;
39
- let program = Configuration . getRenpyExecutablePath ( ) ;
59
+ protected override terminateRequest ( ) : void {
60
+ this . terminate ( ) ;
61
+ }
40
62
41
- if ( ! isValidExecutable ( program ) ) {
42
- logToast ( vscode . LogLevel . Error , "Ren'Py executable location not configured or is invalid." ) ;
43
- return ;
63
+ protected override disconnectRequest ( response : DebugProtocol . DisconnectResponse , args : DebugProtocol . DisconnectArguments ) : void {
64
+ if ( args . terminateDebuggee ) {
65
+ this . terminate ( ) ;
66
+ } else {
67
+ this . disconnect ( ) ;
68
+ this . sendEvent ( new TerminatedEvent ( ) ) ;
44
69
}
70
+ }
45
71
46
- program += " " + getWorkspaceFolder ( ) ;
47
- if ( this . command ) {
48
- program += " " + this . command ;
72
+ private terminate ( ) {
73
+ if ( this . childProcess === null ) {
74
+ return ;
49
75
}
50
- if ( this . args ) {
51
- program += " " + this . args . join ( " " ) ;
76
+ this . childProcess . kill ( ) ;
77
+ this . childProcess = null ;
78
+ }
79
+
80
+ private disconnect ( ) {
81
+ if ( this . childProcess === null ) {
82
+ return ;
52
83
}
53
- terminal . sendText ( program ) ;
54
- this . sendEvent ( new TerminatedEvent ( ) ) ;
84
+ this . childProcess . disconnect ( ) ;
85
+ this . childProcess = null ;
55
86
}
56
87
}
57
88
@@ -63,8 +94,6 @@ export class RenpyConfigurationProvider implements vscode.DebugConfigurationProv
63
94
config . type = "renpy" ;
64
95
config . request = "launch" ;
65
96
config . name = "Ren'Py: Launch" ;
66
- config . command = "run" ;
67
- config . args = [ ] ;
68
97
}
69
98
}
70
99
return config ;
0 commit comments