Newer
Older

s1995588
committed
import * as vscode from "vscode";
import {
TextDocument
} from "vscode";
import {
CancellationToken,

s1995588
committed
NotificationType,
ProgressType,
TextDocumentIdentifier
} from "vscode-languageclient/node";
import { analysisResultsProvider, client, clientReady, disposalQueue, provider, resultHandlers, treeView } from './extension';
import { ParameterDefinitions, ProgressIndication, ResultNotification, getNonce } from "./utils";

s1995588
committed
let localToolNames: Array<string> | undefined;
let localDocumentVars: Array<{ uri: string, constants: Array<{ name: string, value: string }>, distributions: Array<{ name: string, value: string }> }> = [];
let localParameters: Array<{ toolName: string, parameters: Array<{ id: string, name: string, value: string, type: string, category: string }> }> = [];

s1995588
committed
export function initializeTools() {
client?.sendRequest<any>("modest/getTools").then(data => {
localToolNames = data.availableTools;

s1995588
committed
provider.sendMessage({
type: "fillTools",

s1995588
committed
});
});
}
export function getDocumentVars(document: TextDocument) {

s1995588
committed
if (document.languageId === "modest") {
if (document.uri) {
let uri = document.uri.toString();
let jsonObject = { "textDocument": TextDocumentIdentifier.create(uri) };
client?.sendRequest<{ constants: Array<string>, distributions: Array<string> }>("modest/getDocumentVars", jsonObject).then(data => {
const index = localDocumentVars.findIndex(x => x.uri === uri);
const newConstants = data.constants.map(constant => {

s1995588
committed
return { name: constant, value: "" };
});
const newDistributions = data.distributions.map(distribution => {
return { name: distribution, value: "" };
});

s1995588
committed
if (index === -1) {
localDocumentVars.push({ "uri": uri, constants: newConstants, distributions: newDistributions });

s1995588
committed
} else {
localDocumentVars[index].constants = newConstants;
localDocumentVars[index].distributions = newDistributions;

s1995588
committed
}
provider.sendMessage({
type: "updateDocumentVars",
documentVars: localDocumentVars,

s1995588
committed
"uri": uri
});
});
}
}
}
function getParameters(toolName: string) {
let jsonObject = { "toolName": toolName };
client?.sendRequest<ParameterDefinitions>("modest/getParameters", jsonObject).then(data => {
const index = localParameters.findIndex(x => x.toolName === toolName);

s1995588
committed
const newParameters = data.parameterDefinitions.map(parameter => {

Stekelenburg, A.V. (Alexander, Student )
committed
if (parameter.type.valueType === "Enum") {
return { id: parameter.id, name: parameter.name, value: parameter.defaultValue, type: "Enum", category: parameter.category, description: parameter.description, possibleValues: parameter.type.possibleValues };

Stekelenburg, A.V. (Alexander, Student )
committed
}
return { id: parameter.id, name: parameter.name, value: parameter.defaultValue, type: parameter.type.valueType, category: parameter.category, description: parameter.description };

s1995588
committed
});
if (index === -1) {
localParameters.push({ toolName: toolName, parameters: newParameters });

s1995588
committed
} else {
localParameters[index].parameters = newParameters;

s1995588
committed
}
provider.sendMessage({
type: "updateParameters",
parameters: localParameters,

s1995588
committed
toolName: toolName
});
});
}
function runTool(uri: string, toolName: string, constants: { name: string; value: string; }[], suppliedDistributions: { name: string, value: string }[], suppliedParameters: { id: string; value: string; }[]) {
const toolIndex = localParameters.findIndex(x => x.toolName === toolName);
let serverParameters: Array<{ id: string, value: string }> = [];

s1995588
committed
if (toolIndex !== -1) {
for (const parameter of suppliedParameters) {
const parameterIndex = localParameters[toolIndex].parameters.findIndex(x => x.id === parameter.id);

s1995588
committed
if (parameterIndex !== -1) {

Stekelenburg, A.V. (Alexander, Student )
committed
if (String(localParameters[toolIndex].parameters[parameterIndex].value) !== String(parameter.value)) {

s1995588
committed
serverParameters.push(parameter);
}
}
}
}
const fileIndex = localDocumentVars.findIndex(x => x.uri === uri);
if (fileIndex !== -1) {
for (const distribution of suppliedDistributions) {
const distributionIndex = localDocumentVars[fileIndex].distributions.findIndex(x => x.name === distribution.name);
if (distributionIndex !== -1) {
if (String(localDocumentVars[fileIndex].distributions[distributionIndex].value) !== String(distribution.value)) {
constants.push(distribution);
}
}
}

s1995588
committed
}
let jsonObject = {
textDocument: TextDocumentIdentifier.create(uri),
toolName: toolName,
constants: constants,

s1995588
committed
parameters: serverParameters,
runToken: uri + toolName + JSON.stringify(constants) + JSON.stringify(serverParameters) + Date.now()

s1995588
committed
};

s1995588
committed
vscode.window.activeTextEditor?.document.save();
vscode.window.withProgress({ location: vscode.ProgressLocation.Notification, cancellable: true, title: "Running " + toolName }, async (progress, token) => {

s1995588
committed
await new Promise<null>(async (resolve, _) => {

Stekelenburg, A.V. (Alexander, Student )
committed
if (!clientReady) {

Stekelenburg, A.V. (Alexander, Student )
committed
vscode.window.showErrorMessage("Server not ready yet, try again later");
resolve(null);

Stekelenburg, A.V. (Alexander, Student )
committed
return;
}
try {
let progressHandler = client?.onProgress(new ProgressType<ProgressIndication>(), jsonObject.runToken, indication => {

Stekelenburg, A.V. (Alexander, Student )
committed
progress.report({ message: indication.message, increment: indication.progress * 100 });
});
if (progressHandler) { disposalQueue.push(progressHandler); }
let resultHandler = (data: ResultNotification) => {
if (data.runToken === jsonObject.runToken) {

Stekelenburg, A.V. (Alexander, Student )
committed
if (data.data && data.data !== "") {
try {

Stekelenburg, A.V. (Alexander, Student )
committed
analysisResultsProvider.setJsonObject(JSON.parse(data.data.trim()));

Stekelenburg, A.V. (Alexander, Student )
committed
const treeRoot = analysisResultsProvider.getTreeRoot();
treeView.reveal(treeRoot, { focus: false, select: false, expand: true });

Stekelenburg, A.V. (Alexander, Student )
committed
} catch (error) {
let language = "plaintext";

Stekelenburg, A.V. (Alexander, Student )
committed
if (toolName === "mopy (export-to-python)") {
language = "python";

Stekelenburg, A.V. (Alexander, Student )
committed
} else if (toolName === "moconv (convert)") {
language = "json";

Stekelenburg, A.V. (Alexander, Student )
committed
}
// TODO: Look at parameters for mosta to automatically determine extension?
vscode.workspace.openTextDocument({ language: language, content: data.data }).then(document => {
vscode.window.showTextDocument(document);

Stekelenburg, A.V. (Alexander, Student )
committed
});

Stekelenburg, A.V. (Alexander, Student )
committed
}
}
progressHandler?.dispose();
resultHandlers.splice(resultHandlers.indexOf(resultHandler), 1);

Stekelenburg, A.V. (Alexander, Student )
committed
resolve(null);
}
};
resultHandlers.push(resultHandler);
disposalQueue.push({
dispose: function () {
vscode.window.showErrorMessage("Language server restarting, operation cancelled");
resultHandlers.splice(resultHandlers.indexOf(resultHandler), 1);
resolve(null);
token.isCancellationRequested = true;
}
});
token.onCancellationRequested(_ => {
client?.sendRequest("modest/cancelRun", { runToken: jsonObject.runToken });
}, null, disposalQueue);
await client?.sendRequest<string>("modest/runTool", jsonObject);

Stekelenburg, A.V. (Alexander, Student )
committed
} catch (e) {
vscode.window.showErrorMessage("Internal error: " + e);

Stekelenburg, A.V. (Alexander, Student )
committed
resolve(null);
return;

Stekelenburg, A.V. (Alexander, Student )
committed
}

s1995588
committed
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
});
});
}
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': {

s1995588
committed
provider.sendMessage({
type: "fillTools",

s1995588
committed
});
}

s1995588
committed
provider.sendMessage({
type: "updateDocumentVars",
documentVars: localDocumentVars

s1995588
committed
});
}
break;
}
case 'toolSelected': {
if (data.toolName !== "") {
getParameters(data.toolName);
}
if (vscode.window.activeTextEditor) {
getDocumentVars(vscode.window.activeTextEditor.document);
}

s1995588
committed
break;
}
case 'runTool': {
runTool(data.uri, data.toolName, data.constants, data.distributions, data.parameters);

s1995588
committed
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
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">
<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">

s1995588
committed
<title>Modest run dialog</title>
</head>
<body>

Stekelenburg, A.V. (Alexander, Student )
committed
<h3 title="Select tool">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>
<div class="pane-view">
<div class="split-view-view">
<div class="pane vertical">
<div class="pane-header">

Stekelenburg, A.V. (Alexander, Student )
committed
<div class="codicon codicon-chevron-down"></div>

Stekelenburg, A.V. (Alexander, Student )
committed
<h3 class="title" title="Open constants">Open constants</h3>
</div>
<div class="pane-body">
<ul class="option-list" id="constants">There are no undefined constants.</ul>
</div>
</div>
<div class="pane vertical">
<div class="pane-header">

Stekelenburg, A.V. (Alexander, Student )
committed
<div class="codicon codicon-chevron-right"></div>
<h3 class="title" title="Distributions">Distributions</h3>
</div>
<div class="pane-body">
<ul class="option-list" id="distributions">There are no distributions.</ul>
</div>
</div>
<div class="pane vertical">
<div class="pane-header">

Stekelenburg, A.V. (Alexander, Student )
committed
<div class="codicon codicon-chevron-right"></div>
<h3 class="title" title="Parameters">Parameters</h3>
<div id="parameters"class="pane-body">

s1995588
committed
</div>
</div>
<script nonce="${nonce}" src="${scriptUri}"></script>
</body>
</html>
`;