Newer
Older

s1995588
committed
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
import * as vscode from "vscode";
import {
TextDocument
} from "vscode";
import {
NotificationType,
ProgressType,
TextDocumentIdentifier
} from "vscode-languageclient/node";
import { analysisResultsProvider, client, provider } from './extension';
export let toolNames: Array<string> | undefined;
export let constants: Array<{ uri: string, constants: Array<{ name: string, value: string }> }> = [];
export let parameters: Array<{ toolName: string, parameters: Array<{ id: string, value: string, type: ParameterType, category: string }> }> = [];
//#region interfaces
interface ParameterDefinitions {
parameterDefinitions: Array<ParameterDefinition>,
}
interface ParameterDefinition {
id: string,
name: string,
description: string,
category: string,
type: ParameterType
isOptional: boolean,
defaultValue: string,
}
interface ParameterType {
valueType: string,
innerType: Array<ParameterType>,
possibleValues: Array<string>,
}
interface ProgressIndication {
message: string,
progress: number
}
interface ResultNotification {
progressToken: string,
data: string
}
//#endregion
export function initializeTools() {
client?.sendRequest<any>("modest/getTools").then(data => {
toolNames = data.availableTools;
provider.sendMessage({
type: "fillTools",
tools: toolNames
});
});
}
export function getConstants(document: TextDocument) {
if (document.languageId === "modest") {
if (document.uri) {
let uri = document.uri.toString();
let jsonObject = { "textDocument": TextDocumentIdentifier.create(uri) };
client?.sendRequest<Array<string>>("modest/getConstants", jsonObject).then(data => {
const index = constants.findIndex(x => x.uri === uri);
const newConstants = data.map(constant => {
return { name: constant, value: "" };
});
if (index === -1) {
constants.push({ "uri": uri, constants: newConstants });
} else {
constants[index].constants = newConstants;
}
provider.sendMessage({
type: "updateConstants",
constants: constants,
"uri": uri
});
});
}
}
}
function getParameters(toolName: string) {
let jsonObject = { "toolName": toolName };
client?.sendRequest<ParameterDefinitions>("modest/getParameters", jsonObject).then(data => {
const index = parameters.findIndex(x => x.toolName === toolName);
const newParameters = data.parameterDefinitions.map(parameter => {
return { id: parameter.id, value: parameter.defaultValue, type: parameter.type, category: parameter.category };
});
if (index === -1) {
parameters.push({ toolName: toolName, parameters: newParameters });
} else {
parameters[index].parameters = newParameters;
}
provider.sendMessage({
type: "updateParameters",
parameters: parameters,
toolName: toolName
});
});
}
function runTool(uri: string, toolName: string, constants: { name: string; value: string; }[], suppliedParameters: { id: string; value: string; }[]) {
const toolIndex = parameters.findIndex(x => x.toolName === toolName);
let serverParameters: Array<{ id: string, value: string }> = [];
if (toolIndex !== -1) {
for (const parameter of suppliedParameters) {
const parameterIndex = parameters[toolIndex].parameters.findIndex(x => x.id === parameter.id);
if (parameterIndex !== -1) {
if (parameters[toolIndex].parameters[parameterIndex].value !== parameter.value) {
serverParameters.push(parameter);
}
}
}
}
let jsonObject = {
textDocument: TextDocumentIdentifier.create(uri),
toolName: toolName,
constants: constants,
parameters: serverParameters,
progressToken: uri + toolName + constants + serverParameters + Date.now()
};
vscode.window.activeTextEditor?.document.save();
vscode.window.withProgress({ location: vscode.ProgressLocation.Notification, cancellable: false, title: "Running " + toolName }, async (progress, token) => {
await new Promise<null>(async (resolve, _) => {
let progressHandler = client?.onProgress(new ProgressType<ProgressIndication>(), jsonObject.progressToken, indication => {
progress.report({ message: indication.message, increment: indication.progress * 100 });
});
let resultHandler = client?.onNotification(new NotificationType<ResultNotification>("modest/toolResult"), data => {
if (data.progressToken === jsonObject.progressToken) {
if (data.data && data.data !== "") {
try {
analysisResultsProvider.setJsonString(data.data);
} catch(error) {
console.error(error);
}
}

s1995588
committed
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
264
265
266
267
268
269
progressHandler?.dispose();
resultHandler?.dispose();
resolve(null);
}
});
await client?.sendRequest<string>("modest/runTool", jsonObject, token);
});
});
}
export class ModestSidebarProvider implements vscode.WebviewViewProvider {
public static readonly viewType = "modest.modestSidebar";
private _view?: vscode.WebviewView;
constructor(private readonly _extensionUri: vscode.Uri) { }
resolveWebviewView(
webviewView: vscode.WebviewView,
context: vscode.WebviewViewResolveContext<unknown>,
token: vscode.CancellationToken
): void | Thenable<void> {
this._view = webviewView;
webviewView.webview.options = {
// Allow scripts in the webview
enableScripts: true,
localResourceRoots: [this._extensionUri],
};
webviewView.webview.html = this._getHtmlForWebview(webviewView.webview);
webviewView.webview.onDidReceiveMessage(data => {
console.log(data);
switch (data.type) {
case 'init': {
if (toolNames) {
provider.sendMessage({
type: "fillTools",
tools: toolNames
});
}
if (constants) {
provider.sendMessage({
type: "updateConstants",
constants: constants
});
}
break;
}
case 'toolSelected': {
getParameters(data.toolName);
break;
}
case 'runTool': {
runTool(data.uri, data.toolName, data.constants, data.parameters);
break;
}
}
});
}
/**
* sendMessage
* @param {any} message
*/
public sendMessage(message: any) {
this._view?.show(true);
this._view?.webview?.postMessage(message);
}
private _getHtmlForWebview(webview: vscode.Webview) {
// Get the local path to main script run in the webview, then convert it to a uri we can use in the webview.
const scriptUri = webview.asWebviewUri(
vscode.Uri.joinPath(this._extensionUri, "media", "main.js")
);
// Do the same for the stylesheet.
const styleResetUri = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, 'media', 'reset.css'));
const styleVSCodeUri = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, 'media', 'vscode.css'));
const styleMainUri = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, 'media', 'main.css'));
const styleCodicons = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, 'node_modules', 'vscode-codicons', 'dist', 'codicon.css'));
const fontCodicons = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, 'node_modules', 'vscode-codicons', 'dist', 'codicon.ttf'));
// Use a nonce to only allow a specific script to be run.
const nonce = getNonce();
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!--
Use a content security policy to only allow loading images from https or from our extension directory,
and only allow scripts that have a specific nonce.
-->
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; font-src ${fontCodicons}; style-src ${webview.cspSource}; script-src 'nonce-${nonce}';">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="${styleResetUri}" rel="stylesheet">
<link href="${styleVSCodeUri}" rel="stylesheet">
<link href="${styleMainUri}" rel="stylesheet">
<link href="${styleCodicons}" rel="stylesheet">
<title>Modest run dialog</title>
</head>
<body>
<h3>Select tool</h3>
<div id="run-box">
<select class="tools-dropdown" id="tools"> </select>
<button id="run-button"><i class="codicon codicon-play"></i></button>
</div>
<h3>Open constants</h3>
<ul class="option-list" id="constants">There are no undefined constants.</ul>
<h3>Parameters</h3>
<ul class="option-list" id="parameters">There are no parameters.</ul>
<script nonce="${nonce}" src="${scriptUri}"></script>
</body>
</html>`;
function getNonce() {
let text = "";
const possible =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (let i = 0; i < 32; i++) {
text += possible.charAt(
Math.floor(Math.random() * possible.length)
);
}
return text;
}
}
}