-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathactivateMockDebug.ts
164 lines (147 loc) · 5.22 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
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
'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
});
}
}),
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
/*
vscode.languages.registerEvaluatableExpressionProvider('markdown', {
provideEvaluatableExpression(document: vscode.TextDocument, position: vscode.Position): vscode.ProviderResult<vscode.EvaluatableExpression> {
const wordRange = document.getWordRangeAtPosition(position);
return wordRange ? new vscode.EvaluatableExpression(wordRange) : undefined;
}
});
*/
}
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 = {
async readFile(path: string) {
try {
const uri = vscode.Uri.file(path);
const bytes = await vscode.workspace.fs.readFile(uri);
const contents = Buffer.from(bytes).toString('utf8');
return contents;
} catch(e) {
try {
const uri = vscode.Uri.parse(path);
const bytes = await vscode.workspace.fs.readFile(uri);
const contents = Buffer.from(bytes).toString('utf8');
return contents;
} catch (e) {
return `cannot read '${path}'`;
}
}
}
};
class InlineDebugAdapterFactory implements vscode.DebugAdapterDescriptorFactory {
createDebugAdapterDescriptor(_session: vscode.DebugSession): ProviderResult<vscode.DebugAdapterDescriptor> {
return new vscode.DebugAdapterInlineImplementation(new MockDebugSession(workspaceFileAccessor));
}
}