From d883792b737b24e39ebf9c261c1c33ecc3e5303a Mon Sep 17 00:00:00 2001 From: Josh Pinkney Date: Mon, 30 Nov 2020 17:09:26 -0500 Subject: [PATCH 1/4] Bundle support P.O.C Signed-off-by: Josh Pinkney --- src/languageservice/commandManager.ts | 23 ++++++++++++++++++ src/server.ts | 35 +++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 src/languageservice/commandManager.ts diff --git a/src/languageservice/commandManager.ts b/src/languageservice/commandManager.ts new file mode 100644 index 00000000..60cb9bbf --- /dev/null +++ b/src/languageservice/commandManager.ts @@ -0,0 +1,23 @@ +import { ExecuteCommandParams } from 'vscode-languageserver'; + +export class CommandManager { + + private commands = new Map(); + + registerCommand(name: string, action: Function) { + this.commands.set(name, action); + } + + /** + * Execute a registered command if found + * @param e the ExecuteCommandParams you want to use + */ + executeCommand(e: ExecuteCommandParams): any { + const com = this.commands.get(e.command); + if (com) { + return com.apply(e.arguments); + } + return undefined; + } + +} \ No newline at end of file diff --git a/src/server.ts b/src/server.ts index d87390fe..93c06182 100755 --- a/src/server.ts +++ b/src/server.ts @@ -52,6 +52,7 @@ import { isRelativePath, relativeToAbsolutePath, workspaceFoldersChanged } from import { URI } from 'vscode-uri'; import { KUBERNETES_SCHEMA_URL, JSON_SCHEMASTORE_URL } from './languageservice/utils/schemaUrls'; import { schemaRequestHandler } from './languageservice/services/schemaRequestHandler'; +import { CommandManager } from './languageservice/commandManager'; // eslint-disable-next-line @typescript-eslint/no-explicit-any nls.config(process.env['VSCODE_NLS_CONFIG'] as any); @@ -103,6 +104,7 @@ interface JSONSchemaSettings { ****************/ // Language server configuration +const commandManager = new CommandManager(); let yamlConfigurationSettings: JSONSchemaSettings[] = undefined; let schemaAssociations: ISchemaAssociations | SchemaConfiguration[] | undefined = undefined; let formatterRegistration: Thenable = null; @@ -388,6 +390,12 @@ const schemaRequestService = schemaRequestHandlerWrapper.bind(this, connection); export const customLanguageService = getCustomLanguageService(schemaRequestService, workspaceContext); +export interface YAMLLanguageServerBundle { + name: string; + version: string; + commandFunctions: Map; +} + /*********************** * Connection listeners **********************/ @@ -417,6 +425,25 @@ connection.onInitialize( capabilities.textDocument.rangeFormatting.dynamicRegistration ); hasWorkspaceFolderCapability = capabilities.workspace && !!capabilities.workspace.workspaceFolders; + + // Path to an array of javascript bundles + const bundlePath = params.initializationOptions?.bundles ? params.initializationOptions.bundles : []; + + // The commands you want run on the server side + const serverCommands = []; + + // register everything with the command manager + for (const path of bundlePath) { + const c = require(path) as YAMLLanguageServerBundle; + const cmdsFunctions = c.commandFunctions; + if (cmdsFunctions) { + cmdsFunctions.forEach((action: Function, commandID: string) => { + console.log(commandID, action); + commandManager.registerCommand(commandID, action); + serverCommands.push(commandID); + }); + } + } return { capabilities: { textDocumentSync: documents.syncKind, @@ -432,6 +459,9 @@ connection.onInitialize( supported: true, }, }, + executeCommandProvider: { + commands: serverCommands + } }, }; } @@ -679,5 +709,10 @@ connection.onRequest(SchemaModificationNotification.type, (modifications: Schema return Promise.resolve(); }); +// Handler for workspace/executeCommand request +connection.onExecuteCommand(e => { + return commandManager.executeCommand(e); +}); + // Start listening for any messages from the client connection.listen(); From 57ee800d37bf63822275825441b50427674bd7ac Mon Sep 17 00:00:00 2001 From: Josh Pinkney Date: Mon, 30 Nov 2020 17:12:52 -0500 Subject: [PATCH 2/4] Update executeCommand response Signed-off-by: Josh Pinkney --- src/languageservice/commandManager.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/languageservice/commandManager.ts b/src/languageservice/commandManager.ts index 60cb9bbf..1e1a8389 100644 --- a/src/languageservice/commandManager.ts +++ b/src/languageservice/commandManager.ts @@ -17,7 +17,7 @@ export class CommandManager { if (com) { return com.apply(e.arguments); } - return undefined; + return null; } -} \ No newline at end of file +} From 132a4d909b8fc0651c2649c1f4c426a3df6d5d53 Mon Sep 17 00:00:00 2001 From: Josh Pinkney Date: Mon, 30 Nov 2020 17:13:50 -0500 Subject: [PATCH 3/4] Update comments Signed-off-by: Josh Pinkney --- src/server.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/server.ts b/src/server.ts index 93c06182..c5956915 100755 --- a/src/server.ts +++ b/src/server.ts @@ -432,7 +432,7 @@ connection.onInitialize( // The commands you want run on the server side const serverCommands = []; - // register everything with the command manager + // register everything with the command manager and gather server side commands for (const path of bundlePath) { const c = require(path) as YAMLLanguageServerBundle; const cmdsFunctions = c.commandFunctions; @@ -709,7 +709,6 @@ connection.onRequest(SchemaModificationNotification.type, (modifications: Schema return Promise.resolve(); }); -// Handler for workspace/executeCommand request connection.onExecuteCommand(e => { return commandManager.executeCommand(e); }); From 7c3587173625cf883b2804252a99888d8e2c0bf2 Mon Sep 17 00:00:00 2001 From: Josh Pinkney Date: Mon, 30 Nov 2020 17:15:56 -0500 Subject: [PATCH 4/4] Rework layout of bundle Signed-off-by: Josh Pinkney --- src/bundleTypes.ts | 5 +++++ ...{commandManager.ts => bundleCommandManager.ts} | 2 +- src/server.ts | 15 ++++----------- 3 files changed, 10 insertions(+), 12 deletions(-) create mode 100644 src/bundleTypes.ts rename src/languageservice/{commandManager.ts => bundleCommandManager.ts} (93%) diff --git a/src/bundleTypes.ts b/src/bundleTypes.ts new file mode 100644 index 00000000..6a6b62b0 --- /dev/null +++ b/src/bundleTypes.ts @@ -0,0 +1,5 @@ +export interface YAMLLanguageServerBundle { + name: string; + version: string; + commandFunctions: Map; +} diff --git a/src/languageservice/commandManager.ts b/src/languageservice/bundleCommandManager.ts similarity index 93% rename from src/languageservice/commandManager.ts rename to src/languageservice/bundleCommandManager.ts index 1e1a8389..03b61561 100644 --- a/src/languageservice/commandManager.ts +++ b/src/languageservice/bundleCommandManager.ts @@ -1,6 +1,6 @@ import { ExecuteCommandParams } from 'vscode-languageserver'; -export class CommandManager { +export class BundleCommandManager { private commands = new Map(); diff --git a/src/server.ts b/src/server.ts index c5956915..99c493db 100755 --- a/src/server.ts +++ b/src/server.ts @@ -52,7 +52,7 @@ import { isRelativePath, relativeToAbsolutePath, workspaceFoldersChanged } from import { URI } from 'vscode-uri'; import { KUBERNETES_SCHEMA_URL, JSON_SCHEMASTORE_URL } from './languageservice/utils/schemaUrls'; import { schemaRequestHandler } from './languageservice/services/schemaRequestHandler'; -import { CommandManager } from './languageservice/commandManager'; +import { BundleCommandManager } from './languageservice/bundleCommandManager'; // eslint-disable-next-line @typescript-eslint/no-explicit-any nls.config(process.env['VSCODE_NLS_CONFIG'] as any); @@ -104,7 +104,7 @@ interface JSONSchemaSettings { ****************/ // Language server configuration -const commandManager = new CommandManager(); +const commandManager = new BundleCommandManager(); let yamlConfigurationSettings: JSONSchemaSettings[] = undefined; let schemaAssociations: ISchemaAssociations | SchemaConfiguration[] | undefined = undefined; let formatterRegistration: Thenable = null; @@ -390,12 +390,6 @@ const schemaRequestService = schemaRequestHandlerWrapper.bind(this, connection); export const customLanguageService = getCustomLanguageService(schemaRequestService, workspaceContext); -export interface YAMLLanguageServerBundle { - name: string; - version: string; - commandFunctions: Map; -} - /*********************** * Connection listeners **********************/ @@ -434,11 +428,10 @@ connection.onInitialize( // register everything with the command manager and gather server side commands for (const path of bundlePath) { - const c = require(path) as YAMLLanguageServerBundle; - const cmdsFunctions = c.commandFunctions; + const bundle = require(path) as YAMLLanguageServerBundle; + const cmdsFunctions = bundle.commandFunctions; if (cmdsFunctions) { cmdsFunctions.forEach((action: Function, commandID: string) => { - console.log(commandID, action); commandManager.registerCommand(commandID, action); serverCommands.push(commandID); });