11// The module 'vscode' contains the VS Code extensibility API
22// Import the module and reference it with the alias vscode in your code below
33import * as vscode from 'vscode' ;
4- import { WorkspaceFolder , ProviderResult , CancellationToken } from 'vscode' ;
4+ import { WorkspaceFolder , ProviderResult , CancellationToken , DebugConfigurationProviderTriggerKind } from 'vscode' ;
55import { DebugSession } from './debugSession' ;
66import {
7- DebugConfiguration , DebugMode , FunctionDebugConfiguration ,
7+ DebugMode , FunctionDebugConfiguration ,
88 FileDebugConfiguration , WorkspaceDebugConfiguration ,
99 StrictDebugConfiguration ,
1010 AttachConfiguration
@@ -20,9 +20,19 @@ export async function activate(context: vscode.ExtensionContext) {
2020 terminalHandler = new TerminalHandler ( ) ;
2121 const port = await terminalHandler . portPromise ;
2222
23- // register a configuration provider
24- const provider = new DebugConfigurationProvider ( port ) ;
25- context . subscriptions . push ( vscode . debug . registerDebugConfigurationProvider ( 'R-Debugger' , provider ) ) ;
23+ console . log ( 'activate' ) ;
24+
25+ // register configuration resolver
26+ const resolver = new DebugConfigurationResolver ( port ) ;
27+ context . subscriptions . push ( vscode . debug . registerDebugConfigurationProvider ( 'R-Debugger' , resolver ) ) ;
28+
29+ // register dynamic configuration provider
30+ const dynamicProvider = new DynamicDebugConfigurationProvider ( ) ;
31+ context . subscriptions . push ( vscode . debug . registerDebugConfigurationProvider ( 'R-Debugger' , dynamicProvider , DebugConfigurationProviderTriggerKind . Dynamic ) ) ;
32+
33+ // register initial configuration provider
34+ const initialProvider = new InitialDebugConfigurationProvider ( ) ;
35+ context . subscriptions . push ( vscode . debug . registerDebugConfigurationProvider ( 'R-Debugger' , initialProvider , DebugConfigurationProviderTriggerKind . Initial ) ) ;
2636
2737 // register the debug adapter descriptor provider
2838 const factory = new DebugAdapterDescriptorFactory ( ) ;
@@ -48,8 +58,100 @@ export function deactivate() {
4858 }
4959}
5060
61+ class InitialDebugConfigurationProvider implements vscode . DebugConfigurationProvider {
62+ provideDebugConfigurations ( folder : WorkspaceFolder | undefined ) : ProviderResult < StrictDebugConfiguration [ ] > {
63+ return [
64+ {
65+ type : "R-Debugger" ,
66+ request : "launch" ,
67+ name : "Launch Workspace" ,
68+ debugMode : "workspace" ,
69+ workingDirectory : "${workspaceFolder}" ,
70+ allowGlobalDebugging : true
71+ } ,
72+ {
73+ type : "R-Debugger" ,
74+ request : "launch" ,
75+ name : "Debug R-File" ,
76+ debugMode : "file" ,
77+ workingDirectory : "${workspaceFolder}" ,
78+ file : "${file}" ,
79+ allowGlobalDebugging : true
80+ } ,
81+ {
82+ type : "R-Debugger" ,
83+ request : "launch" ,
84+ name : "Debug R-Function" ,
85+ debugMode : "function" ,
86+ workingDirectory : "${workspaceFolder}" ,
87+ file : "${file}" ,
88+ mainFunction : "main" ,
89+ allowGlobalDebugging : false
90+ } ,
91+ {
92+ type : "R-Debugger" ,
93+ request : "attach" ,
94+ name : "Attach to R process" ,
95+ splitOverwrittenOutput : true
96+ }
97+ ] ;
98+ }
99+ }
51100
52- class DebugConfigurationProvider implements vscode . DebugConfigurationProvider {
101+ class DynamicDebugConfigurationProvider implements vscode . DebugConfigurationProvider {
102+
103+ provideDebugConfigurations ( folder : WorkspaceFolder | undefined ) : ProviderResult < StrictDebugConfiguration [ ] > {
104+
105+ const doc = vscode . window . activeTextEditor ;
106+ const docValid = doc && doc . document . uri . scheme === 'file' ;
107+ const wd = ( folder ? '${workspaceFolder}' : ( docValid ? '${fileDirname}' : '.' ) ) ;
108+
109+ let configs : StrictDebugConfiguration [ ] = [ ] ;
110+
111+ configs . push ( {
112+ type : "R-Debugger" ,
113+ request : "launch" ,
114+ name : "Launch Workspace" ,
115+ debugMode : "workspace" ,
116+ workingDirectory : wd ,
117+ allowGlobalDebugging : true
118+ } ) ;
119+
120+ if ( docValid ) {
121+ configs . push ( {
122+ type : "R-Debugger" ,
123+ request : "launch" ,
124+ name : "Debug R-File" ,
125+ debugMode : "file" ,
126+ workingDirectory : wd ,
127+ file : "${file}" ,
128+ allowGlobalDebugging : true
129+ } ) ;
130+
131+ configs . push ( {
132+ type : "R-Debugger" ,
133+ request : "launch" ,
134+ name : "Debug R-Function" ,
135+ debugMode : "function" ,
136+ workingDirectory : wd ,
137+ file : "${file}" ,
138+ mainFunction : "main" ,
139+ allowGlobalDebugging : false
140+ } ) ;
141+ } ;
142+
143+ configs . push ( {
144+ type : "R-Debugger" ,
145+ request : "attach" ,
146+ name : "Attach to R process" ,
147+ splitOverwrittenOutput : true
148+ } ) ;
149+
150+ return configs ;
151+ }
152+ }
153+
154+ class DebugConfigurationResolver implements vscode . DebugConfigurationProvider {
53155
54156 readonly customPort : number ;
55157 readonly customHost : string ;
@@ -63,11 +165,13 @@ class DebugConfigurationProvider implements vscode.DebugConfigurationProvider {
63165
64166 let strictConfig : StrictDebugConfiguration | null = null ;
65167
66- // if launch.json is missing or empty
168+ // if the debugger was launched without config
67169 if ( ! config . type && ! config . request && ! config . name ) {
170+
68171 const doc = vscode . window . activeTextEditor ;
69- const wd = ( folder ? '{$workspaceFolder}' : ( doc ? '${fileDirname}' : '~' ) ) ;
70- if ( doc ) {
172+ const docValid = doc && doc . document . uri . scheme === 'file' ;
173+ const wd = ( folder ? '${workspaceFolder}' : ( docValid ? '${fileDirname}' : '.' ) ) ;
174+ if ( docValid ) {
71175 // if file is open, debug file
72176 config = {
73177 type : "R-Debugger" ,
@@ -77,22 +181,15 @@ class DebugConfigurationProvider implements vscode.DebugConfigurationProvider {
77181 file : "${file}" ,
78182 workingDirectory : wd
79183 } ;
80- } else if ( wd ) {
184+ } else {
81185 // if folder but no file is open, launch workspace
82186 config = {
83187 type : "R-Debugger" ,
84188 name : "Launch R Debugger" ,
85189 request : "launch" ,
86- debugMode : "file " ,
190+ debugMode : "workspace " ,
87191 workingDirectory : wd
88192 } ;
89- } else {
90- // if no file/folder open, attach
91- config = {
92- type : 'R-Debugger' ,
93- name : 'Launch' ,
94- request : 'attach'
95- } ;
96193 }
97194 }
98195
0 commit comments