-
Notifications
You must be signed in to change notification settings - Fork 185
/
activateMockDebug.ts
215 lines (185 loc) · 6.93 KB
/
activateMockDebug.ts
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
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
/*
* activateMockDebug.ts containes the shared extension code that can be executed both in node.js and the browser.
*/
'use strict';
import * as vscode from 'vscode';
import { WorkspaceFolder, DebugConfiguration, ProviderResult, CancellationToken } from 'vscode';
import { MockDebugSession } from './mockDebug';
import { FileAccessor } from './mockRuntime';
export function activateMockDebug(context: vscode.ExtensionContext, factory?: vscode.DebugAdapterDescriptorFactory) {
context.subscriptions.push(
vscode.commands.registerCommand('extension.mock-debug.runEditorContents', (resource: vscode.Uri) => {
let targetResource = resource;
if (!targetResource && vscode.window.activeTextEditor) {
targetResource = vscode.window.activeTextEditor.document.uri;
}
if (targetResource) {
vscode.debug.startDebugging(undefined, {
type: 'mock',
name: 'Run File',
request: 'launch',
program: targetResource.fsPath
},
{ noDebug: true }
);
}
}),
vscode.commands.registerCommand('extension.mock-debug.debugEditorContents', (resource: vscode.Uri) => {
let targetResource = resource;
if (!targetResource && vscode.window.activeTextEditor) {
targetResource = vscode.window.activeTextEditor.document.uri;
}
if (targetResource) {
vscode.debug.startDebugging(undefined, {
type: 'mock',
name: 'Debug File',
request: 'launch',
program: targetResource.fsPath,
stopOnEntry: true
});
}
}),
vscode.commands.registerCommand('extension.mock-debug.toggleFormatting', (variable) => {
const ds = vscode.debug.activeDebugSession;
if (ds) {
ds.customRequest('toggleFormatting');
}
})
);
context.subscriptions.push(vscode.commands.registerCommand('extension.mock-debug.getProgramName', config => {
return vscode.window.showInputBox({
placeHolder: "Please enter the name of a markdown file in the workspace folder",
value: "readme.md"
});
}));
// register a configuration provider for 'mock' debug type
const provider = new MockConfigurationProvider();
context.subscriptions.push(vscode.debug.registerDebugConfigurationProvider('mock', provider));
// register a dynamic configuration provider for 'mock' debug type
context.subscriptions.push(vscode.debug.registerDebugConfigurationProvider('mock', {
provideDebugConfigurations(folder: WorkspaceFolder | undefined): ProviderResult<DebugConfiguration[]> {
return [
{
name: "Dynamic Launch",
request: "launch",
type: "mock",
program: "${file}"
},
{
name: "Another Dynamic Launch",
request: "launch",
type: "mock",
program: "${file}"
},
{
name: "Mock Launch",
request: "launch",
type: "mock",
program: "${file}"
}
];
}
}, vscode.DebugConfigurationProviderTriggerKind.Dynamic));
if (!factory) {
factory = new InlineDebugAdapterFactory();
}
context.subscriptions.push(vscode.debug.registerDebugAdapterDescriptorFactory('mock', factory));
if ('dispose' in factory) {
context.subscriptions.push(factory);
}
// override VS Code's default implementation of the debug hover
// here we match only Mock "variables", that are words starting with an '$'
context.subscriptions.push(vscode.languages.registerEvaluatableExpressionProvider('markdown', {
provideEvaluatableExpression(document: vscode.TextDocument, position: vscode.Position): vscode.ProviderResult<vscode.EvaluatableExpression> {
const VARIABLE_REGEXP = /\$[a-z][a-z0-9]*/ig;
const line = document.lineAt(position.line).text;
let m: RegExpExecArray | null;
while (m = VARIABLE_REGEXP.exec(line)) {
const varRange = new vscode.Range(position.line, m.index, position.line, m.index + m[0].length);
if (varRange.contains(position)) {
return new vscode.EvaluatableExpression(varRange);
}
}
return undefined;
}
}));
// override VS Code's default implementation of the "inline values" feature"
context.subscriptions.push(vscode.languages.registerInlineValuesProvider('markdown', {
provideInlineValues(document: vscode.TextDocument, viewport: vscode.Range, context: vscode.InlineValueContext) : vscode.ProviderResult<vscode.InlineValue[]> {
const allValues: vscode.InlineValue[] = [];
for (let l = viewport.start.line; l <= context.stoppedLocation.end.line; l++) {
const line = document.lineAt(l);
var regExp = /\$([a-z][a-z0-9]*)/ig; // variables are words starting with '$'
do {
var m = regExp.exec(line.text);
if (m) {
const varName = m[1];
const varRange = new vscode.Range(l, m.index, l, m.index + varName.length);
// some literal text
//allValues.push(new vscode.InlineValueText(varRange, `${varName}: ${viewport.start.line}`));
// value found via variable lookup
allValues.push(new vscode.InlineValueVariableLookup(varRange, varName, false));
// value determined via expression evaluation
//allValues.push(new vscode.InlineValueEvaluatableExpression(varRange, varName));
}
} while (m);
}
return allValues;
}
}));
}
class MockConfigurationProvider implements vscode.DebugConfigurationProvider {
/**
* Massage a debug configuration just before a debug session is being launched,
* e.g. add all missing attributes to the debug configuration.
*/
resolveDebugConfiguration(folder: WorkspaceFolder | undefined, config: DebugConfiguration, token?: CancellationToken): ProviderResult<DebugConfiguration> {
// if launch.json is missing or empty
if (!config.type && !config.request && !config.name) {
const editor = vscode.window.activeTextEditor;
if (editor && editor.document.languageId === 'markdown') {
config.type = 'mock';
config.name = 'Launch';
config.request = 'launch';
config.program = '${file}';
config.stopOnEntry = true;
}
}
if (!config.program) {
return vscode.window.showInformationMessage("Cannot find a program to debug").then(_ => {
return undefined; // abort launch
});
}
return config;
}
}
export const workspaceFileAccessor: FileAccessor = {
isWindows: false,
async readFile(path: string): Promise<Uint8Array> {
let uri: vscode.Uri;
try {
uri = pathToUri(path);
} catch (e) {
return new TextEncoder().encode(`cannot read '${path}'`);
}
return await vscode.workspace.fs.readFile(uri);
},
async writeFile(path: string, contents: Uint8Array) {
await vscode.workspace.fs.writeFile(pathToUri(path), contents);
}
};
function pathToUri(path: string) {
try {
return vscode.Uri.file(path);
} catch (e) {
return vscode.Uri.parse(path);
}
}
class InlineDebugAdapterFactory implements vscode.DebugAdapterDescriptorFactory {
createDebugAdapterDescriptor(_session: vscode.DebugSession): ProviderResult<vscode.DebugAdapterDescriptor> {
return new vscode.DebugAdapterInlineImplementation(new MockDebugSession(workspaceFileAccessor));
}
}