Skip to content
Snippets Groups Projects
extension.ts 5.71 KiB
Newer Older
// tslint:disable
"use strict";

import { workspace, ExtensionContext } from 'vscode';
import * as vscode from 'vscode';
    LanguageClient,
    LanguageClientOptions,
    ServerOptions,
    Trace,
    TransportKind
} from 'vscode-languageclient/node';
import { ModestCommands } from "./commands";

let client: LanguageClient;
export let provider: ModestSidebarProvider;

export function activate(context: ExtensionContext) {
    // The server is implemented in node
    //let serverExe = "D:\\Desktop\\designproject\\modest-toolset\\Binaries\\Release\\win-x64\\Modest.LanguageServer.exe";
    let serverExe = "C:\\Users\\anoni\\Desktop\\University projects\\module 11\\toolset\\Binaries\\Debug\\win-x64\\Modest.LanguageServer.exe";
    // If the extension is launched in debug mode then the debug server options are used
    // Otherwise the run options are used
    let serverOptions: ServerOptions = {
        // run: { command: serverExe, args: ['-lsp', '-d'] },
        run: {
            command: serverExe,
            transport: TransportKind.stdio,
        },
        // debug: { command: serverExe, args: ['-lsp', '-d'] }
        debug: {
            command: serverExe,
            transport: TransportKind.stdio,
    // Options to control the language client
    let clientOptions: LanguageClientOptions = {
        // Register the server for plain text documents
        documentSelector: [
            {
                pattern: "**/*.modest",
            },
        progressOnInitialization: true,
        //synchronize: {
        // Synchronize the setting section 'languageServerExample' to the server
        //    configurationSection: "languageServerExample",
        //    fileEvents: workspace.createFileSystemWatcher("**/*.modest"),
        //},
    context.subscriptions.push(ModestCommands.simCommand);
    context.subscriptions.push(ModestCommands.addParameters);


    // Create the language client and start the client.
    client = new LanguageClient(
        "ModestExtension",
        "Modest Extension",
        serverOptions,
        clientOptions);
    client.registerProposedFeatures();
    client.trace = Trace.Verbose;
    let langClient = client.start();

    // Push the disposable to the context's subscriptions so that the
    // client can be deactivated on extension deactivation
    context.subscriptions.push(langClient);

    provider = new ModestSidebarProvider(context.extensionUri);

	context.subscriptions.push(
		vscode.window.registerWebviewViewProvider(ModestSidebarProvider.viewType, provider));
    


export function deactivate(): Thenable<void> | undefined {
    if (!client) {
    }
    return client.stop();
}

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 => {
			switch (data.type) {
				case 'colorSelected':
					{
						vscode.window.activeTextEditor?.insertSnippet(new vscode.SnippetString(`#${data.value}`));
						break;
					}
			}
		});
    }

    /**
     * sendMessage
     */
    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'));

		// 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'; 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">
				
				<title>Modest run dialog</title>
			</head>
			<body>
				<ul id="undefined-constants" />
                <ul id="defined-constants />
                <ul id="options" />
				<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;
            }
	    }
}