openquest-vscode/client/src/extension.ts

88 lines
2.9 KiB
TypeScript

// Local Imports
import constants = require('./constants');
import * as client_handler from "./client_handler"
// External Imports
import { workspace, ExtensionContext } from 'vscode';
import * as vscode from "vscode";
import { LanguageClient } from 'vscode-languageclient/node';
import { startClientsForFolder } from './client_handler';
// Common and Persistent variables
export const outputChannel = vscode.window.createOutputChannel(
constants.DISPLAY_NAME, constants.LANGUAGE_ID
);
export const clients: Map<string, LanguageClient> = new Map();
/**
* This function gets called when the extension is activated,
* depending on what is the trigger configuration on `package.json#activationEvents
*/
export function activate(context: ExtensionContext) {
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
outputChannel.appendLine('[INFO] Extension is now active, good deving and have fun !');
// Commands (these are defined in `package.json#contributes.configurations`)
context.subscriptions.push(
vscode.commands.registerCommand(`${constants.SETTINGS_NAMESPACE}.reload_server`, () => {
// The code placed here will be executed every time this command is ran
vscode.window.showInformationMessage(`[INFO] Reloading Language Server`);
})
);
// Start Language Clients for each folder in the workspace
for (const folder of workspace.workspaceFolders || []) {
startClientsForFolder(folder, context);
}
// Client to Server synchronization
context.subscriptions.push(
workspace.onDidChangeWorkspaceFolders(client_handler.updateClients(context))
);
// Refresh client and server settings when changes happen
context.subscriptions.push(workspace.onDidChangeConfiguration(
(event) => refreshSettings(event, context))
);
}
export async function deactivate(): Promise<void> {
await Promise.all([...clients.values()].map(client_handler.stopClient));
}
/**
* Checks what changes were made in workspace and user settings and acts
* accordingly, like reconnecting/relaunching language servers
*/
async function refreshSettings(event: vscode.ConfigurationChangeEvent, context: ExtensionContext) {
// Check if changes to language server paths were made
if (event.affectsConfiguration(`${constants.SETTINGS_NAMESPACE}.serverPath`)) {
for (const client of clients.values()) {
const folder = client.clientOptions.workspaceFolder;
await client_handler.stopClient(client);
if (folder) {
await startClientsForFolder(folder, context);
}
}
}
//
else {
for (const client of clients.values()) {
client.sendNotification("workspace/didChangeConfiguration", {
settings: {
...workspace.getConfiguration(constants.SETTINGS_NAMESPACE),
checkForUpdates: false,
},
});
}
}
}