-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdebugConfig.ts
More file actions
263 lines (231 loc) · 7.85 KB
/
debugConfig.ts
File metadata and controls
263 lines (231 loc) · 7.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import * as vscode from 'vscode';
import { DebugAdapter } from './debugAdapter';
import {
DebugMode, FunctionDebugConfiguration,
FileDebugConfiguration, WorkspaceDebugConfiguration,
StrictDebugConfiguration,
AttachConfiguration, LaunchConfiguration
} from './debugProtocolModifications';
import { HelpPanel } from './rExtensionApi';
import * as fs from 'fs';
import * as path from 'path';
import { getRpathFromConfig } from './utils';
export class DebugAdapterDescriptorFactory implements vscode.DebugAdapterDescriptorFactory {
helpPanel?: HelpPanel;
constructor(helpPanel?: HelpPanel){
this.helpPanel = helpPanel;
}
createDebugAdapterDescriptor(session: vscode.DebugSession): vscode.ProviderResult<vscode.DebugAdapterDescriptor> {
const config = session.configuration;
if(config.request === 'launch'){
return new vscode.DebugAdapterInlineImplementation(new DebugAdapter(this.helpPanel, <LaunchConfiguration>config));
} else if(config.request === 'attach'){
const port = Number(config.port || 18721);
const host = String(config.host || 'localhost');
return new vscode.DebugAdapterServer(port, host);
} else{
throw new Error('Invalid entry "request" in debug config. Valid entries are "launch" and "attach"');
}
}
}
export class InitialDebugConfigurationProvider implements vscode.DebugConfigurationProvider {
provideDebugConfigurations(folder: vscode.WorkspaceFolder | undefined): vscode.ProviderResult<StrictDebugConfiguration[]>{
return [
{
type: 'R-Debugger',
name: 'Launch R-Workspace',
request: 'launch',
debugMode: 'workspace',
workingDirectory: '${workspaceFolder}'
},
{
type: 'R-Debugger',
name: 'Debug R-File',
request: 'launch',
debugMode: 'file',
workingDirectory: '${workspaceFolder}',
file: '${file}'
},
{
type: 'R-Debugger',
name: 'Debug R-Function',
request: 'launch',
debugMode: 'function',
workingDirectory: '${workspaceFolder}',
file: '${file}',
mainFunction: 'main',
allowGlobalDebugging: false
},
{
type: 'R-Debugger',
name: 'Debug R-Package',
request: 'launch',
debugMode: 'workspace',
workingDirectory: '${workspaceFolder}',
includePackageScopes: true,
loadPackages: ['.']
},
{
type: 'R-Debugger',
request: 'attach',
name: 'Attach to R process',
splitOverwrittenOutput: true
}
];
}
}
export class DynamicDebugConfigurationProvider implements vscode.DebugConfigurationProvider {
provideDebugConfigurations(folder: vscode.WorkspaceFolder | undefined): vscode.ProviderResult<StrictDebugConfiguration[]>{
const doc = vscode.window.activeTextEditor;
const docValid = doc && doc.document.uri.scheme === 'file';
const wd = (folder ? '${workspaceFolder}' : (docValid ? '${fileDirname}' : '.'));
const hasDescription = folder && fs.existsSync(path.join(folder.uri.fsPath, 'DESCRIPTION'));
const configs: StrictDebugConfiguration[] = [];
configs.push({
type: 'R-Debugger',
request: 'launch',
name: 'Launch R-Workspace',
debugMode: 'workspace',
workingDirectory: wd,
allowGlobalDebugging: true
});
if(docValid){
configs.push({
type: 'R-Debugger',
request: 'launch',
name: 'Debug R-File',
debugMode: 'file',
workingDirectory: wd,
file: '${file}',
allowGlobalDebugging: true
});
configs.push({
type: 'R-Debugger',
request: 'launch',
name: 'Debug R-Function',
debugMode: 'function',
workingDirectory: wd,
file: '${file}',
mainFunction: 'main',
allowGlobalDebugging: false
});
}
if(hasDescription){
configs.push({
type: 'R-Debugger',
name: 'Debug R-Package',
request: 'launch',
debugMode: 'workspace',
workingDirectory: wd,
loadPackages: ['.'],
includePackageScopes: true,
allowGlobalDebugging: true
});
}
configs.push({
type: 'R-Debugger',
request: 'attach',
name: 'Attach to R process',
splitOverwrittenOutput: true
});
return configs;
}
}
export class DebugConfigurationResolver implements vscode.DebugConfigurationProvider {
readonly customPort: number;
readonly customHost: string;
readonly supportsHelpViewer: boolean;
constructor(customPort: number, customHost: string = 'localhost', supportsHelpViewer: boolean = false) {
this.customPort = customPort;
this.customHost = customHost;
this.supportsHelpViewer = supportsHelpViewer;
}
resolveDebugConfiguration(folder: vscode.WorkspaceFolder | undefined, config: vscode.DebugConfiguration, token?: vscode.CancellationToken): vscode.ProviderResult<StrictDebugConfiguration> {
let strictConfig: StrictDebugConfiguration|null = null;
const doc = vscode.window.activeTextEditor;
const docValid = doc && doc.document.uri.scheme === 'file';
const wd = (folder ? '${workspaceFolder}' : (docValid ? '${fileDirname}' : '.'));
const hasDescription = folder && fs.existsSync(path.join(folder.uri.fsPath, 'DESCRIPTION'));
// if the debugger was launched without config
if (!config.type && !config.request && !config.name) {
if(hasDescription){
config = {
type: 'R-Debugger',
name: 'Debug R-Package',
request: 'launch',
debugMode: 'workspace',
workingDirectory: wd,
loadPackages: ['.'],
includePackageScopes: true,
allowGlobalDebugging: true
};
} else if(docValid){
// if file is open, debug file
config = {
type: 'R-Debugger',
name: 'Launch R Debugger',
request: 'launch',
debugMode: 'file',
file: '${file}',
workingDirectory: wd
};
} else{
// if folder but no file is open, launch workspace
config = {
type: 'R-Debugger',
name: 'Launch R Debugger',
request: 'launch',
debugMode: 'workspace',
workingDirectory: wd
};
}
}
config.debugMode ||= (docValid ? 'file' : 'workspace');
config.allowGlobalDebugging ??= true;
// fill custom capabilities/socket info/rPath
if(config.request === 'launch'){
// if not specified, set rPath from config
config.rPath ||= getRpathFromConfig();
// capabilities that are always true for this extension:
config.supportsStdoutReading = true;
config.supportsWriteToStdinEvent = true;
config.supportsShowingPromptRequest = true;
// set to true if not specified. necessary since its default in vscDebugger is FALSE:
config.overwriteHelp ??= true;
config.overwriteHelp &&= this.supportsHelpViewer; // check if helpview available
} else if (config.request === 'attach'){
// communication info with TerminalHandler():
config.customPort ??= this.customPort;
config.customHost ||= this.customHost;
config.useCustomSocket ??= true;
config.supportsWriteToStdinEvent ??= true;
config.overwriteLoadAll = false;
}
// make sure the config matches the requirements of one of the debug modes
const debugMode = <DebugMode | undefined>config.debugMode;
if(config.request === 'attach'){
// no fields mandatory
strictConfig = <AttachConfiguration>config;
} else if(debugMode === 'function'){
// make sure that all required fields (workingDirectory, file, function) are filled:
config.workingDirectory ||= wd;
config.file ||= '${file}';
config.mainFunction ||= 'main';
strictConfig = <FunctionDebugConfiguration>config;
} else if(debugMode === 'file'){
// make sure that all required fields (workingDirectory, file) are filled:
config.workingDirectory ||= wd;
config.file ||= '${file}';
strictConfig = <FileDebugConfiguration>config;
} else if(debugMode === 'workspace'){
// make sure that all required fields (workingDirectory) are filled:
config.workingDirectory ||= wd;
strictConfig = <WorkspaceDebugConfiguration>config;
} else{
strictConfig = null;
}
// set launchingDirectory to workingDirectory if not specified otherwise:
config.launchDirectory ||= config.workingDirectory;
return strictConfig;
}
}