Skip to content
Snippets Groups Projects
Commit 70accc23 authored by s1995588's avatar s1995588
Browse files

Added a tool dropdown which is initialized by initializeTools.

Added a getParameters function to get parameters from LanguageServer.
parent e98bf5d0
No related branches found
No related tags found
No related merge requests found
//@ts-check
// This script will be run within the webview itself
// It cannot access the main VS Code APIs directly.
(function () {
// @ts-ignore
const vscode = acquireVsCodeApi();
const oldState = vscode.getState() || { definedConstants: [], undefinedConstants: [], options:[] };
const oldState = vscode.getState() || { definedConstants: [], undefinedConstants: [], options: [] };
let undefinedConstants = oldState.undefinedConstants;
let definedConstants = oldState.definedConstants;
let options = oldState.options;
console.log("Started");
/**
* @type {HTMLSelectElement}
*/
const toolDropDown = document.getElementById("tools");
toolDropDown.addEventListener('change', event => {
vscode.postMessage({ type: "toolSelected", toolName: toolDropDown.options[toolDropDown.selectedIndex].value })
})
// Handle messages sent from the extension to the webview
window.addEventListener('message', event => {
......@@ -25,10 +28,25 @@
update(message.undefinedConstants, message.definedConstants, message.options);
break;
}
case 'fillTools':
{
fillTools(message.tools);
break;
}
}
});
function fillTools(tools) {
const select = document.querySelector("#tools");
for (const tool of tools) {
const option = document.createElement("option");
option.value = tool;
option.text = tool;
select.appendChild(option);
}
}
/**
* @param {Array<{name: string, value: string}>} undefinedConstants
* @param {Array<{name: string, value: string}>} definedConstants
......@@ -39,7 +57,7 @@
const li = document.createElement("li");
const nameBox = document.createElement("input");
nameBox.id = "name-"+name;
nameBox.id = "name-" + name;
nameBox.type = "text";
nameBox.value = name;
nameBox.classList.add("name");
......@@ -47,7 +65,7 @@
li.appendChild(nameBox);
const valueBox = document.createElement("input");
valueBox.id = "value-"+name;
valueBox.id = "value-" + name;
valueBox.type = "text";
valueBox.value = value;
valueBox.classList.add("value");
......@@ -76,5 +94,3 @@
vscode.setState({ definedConstants: definedConstants, undefinedConstants: undefinedConstants, options: options });
}
}());
......@@ -74,7 +74,7 @@ button.secondary:hover {
}
input:not([type='checkbox']),
textarea {
textarea, select {
display: block;
width: 100%;
border: none;
......
import { pathToFileURL } from 'url';
import { provider, modestExecutable } from './extension';
import {getParameters, client, provider, modestExecutable } from './extension';
import * as vscode from 'vscode';
import * as path from "path";
import { LanguageClient } from 'vscode-languageclient/node';
export namespace ModestCommands {
const paramHistory: Map<string, string> = new Map();
export let addParameters = vscode.commands.registerCommand('modest.add-parameters', async () => {
provider.sendMessage({
type: "update",
......
......@@ -11,6 +11,7 @@ import {
LanguageClient,
LanguageClientOptions,
ServerOptions,
TextDocumentIdentifier,
TextEdit,
Trace,
TransportKind
......@@ -19,9 +20,23 @@ import { ModestCommands } from "./commands";
import * as path from "path";
import { fstat, existsSync } from 'fs';
let client: LanguageClient | undefined;
export let client: LanguageClient | undefined;
export let provider: ModestSidebarProvider;
interface ModestTools {
availableTools: Array<ModestTool>
}
interface ModestTool {
name: string,
parameters: Array<ModestParameter>
}
interface ModestParameter {
usage: string,
description: string
}
export function modestExecutable(): string | undefined {
let executable: string | undefined =
vscode.workspace.getConfiguration("modest").get("executableLocation");
......@@ -39,7 +54,7 @@ export function modestExecutable(): string | undefined {
return executable;
}
function createClient(): LanguageClient | undefined{
function createClient(): LanguageClient | undefined {
// TODO: Check if path exists for a better user experience(so it doesn't spam the user with errors)
// The server is implemented in node
......@@ -104,6 +119,9 @@ function createClient(): LanguageClient | undefined{
clientOptions);
client.registerProposedFeatures();
client.trace = Trace.Verbose;
client.onReady().then(() =>
initializeTools()
)
return client;
}
......@@ -140,6 +158,26 @@ export function activate(context: ExtensionContext) {
vscode.window.registerWebviewViewProvider(ModestSidebarProvider.viewType, provider));
}
export function initializeTools() {
client?.sendRequest<any>("modest/getTools").then(data => {
const toolNames: Array<string> = data.availableTools;
provider.sendMessage({
type: "fillTools",
tools: toolNames
})
})
}
export function getParameters(toolName: string) {
let uri = vscode.window.activeTextEditor?.document.uri;
if (uri) {
let JSONObject = { "TextDocument": TextDocumentIdentifier.create(uri.toString()), "ToolName": toolName }
client?.sendRequest<any>("modest/getParameters", JSONObject).then(data => {
console.log(data);
})
}
}
export function deactivate(): Thenable<void> | undefined {
if (!client) {
......@@ -147,6 +185,7 @@ export function deactivate(): Thenable<void> | undefined {
}
return client.stop();
}
class ModestSidebarProvider implements vscode.WebviewViewProvider {
public static readonly viewType = 'modest.modestSidebar';
......@@ -170,12 +209,12 @@ class ModestSidebarProvider implements vscode.WebviewViewProvider {
};
webviewView.webview.html = this._getHtmlForWebview(webviewView.webview);
webviewView.webview.onDidReceiveMessage(data => {
console.log(data)
switch (data.type) {
case 'colorSelected':
case 'toolSelected':
{
vscode.window.activeTextEditor?.insertSnippet(new vscode.SnippetString(`#${data.value}`));
getParameters(data.toolName);
break;
}
}
......@@ -220,6 +259,7 @@ class ModestSidebarProvider implements vscode.WebviewViewProvider {
<title>Modest run dialog</title>
</head>
<body>
<select class="tools-dropdown" id="tools"> </select>
<h3>Undefined constants</h3>
<ul class="option-list" id="undefined-constants"> </ul>
<h3>Defined constants</h3>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment