Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/bundleTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface YAMLLanguageServerBundle {
name: string;
version: string;
commandFunctions: Map<string, Function>;
}
23 changes: 23 additions & 0 deletions src/languageservice/bundleCommandManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ExecuteCommandParams } from 'vscode-languageserver';

export class BundleCommandManager {

private commands = new Map<string, Function>();

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 null;
}

}
27 changes: 27 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { BundleCommandManager } from './languageservice/bundleCommandManager';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
nls.config(process.env['VSCODE_NLS_CONFIG'] as any);
Expand Down Expand Up @@ -103,6 +104,7 @@ interface JSONSchemaSettings {
****************/

// Language server configuration
const commandManager = new BundleCommandManager();
let yamlConfigurationSettings: JSONSchemaSettings[] = undefined;
let schemaAssociations: ISchemaAssociations | SchemaConfiguration[] | undefined = undefined;
let formatterRegistration: Thenable<Disposable> = null;
Expand Down Expand Up @@ -417,6 +419,24 @@ 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 and gather server side commands
for (const path of bundlePath) {
const bundle = require(path) as YAMLLanguageServerBundle;
const cmdsFunctions = bundle.commandFunctions;
if (cmdsFunctions) {
cmdsFunctions.forEach((action: Function, commandID: string) => {
commandManager.registerCommand(commandID, action);
serverCommands.push(commandID);
});
}
}
return {
capabilities: {
textDocumentSync: documents.syncKind,
Expand All @@ -432,6 +452,9 @@ connection.onInitialize(
supported: true,
},
},
executeCommandProvider: {
commands: serverCommands
}
},
};
}
Expand Down Expand Up @@ -679,5 +702,9 @@ connection.onRequest(SchemaModificationNotification.type, (modifications: Schema
return Promise.resolve();
});

connection.onExecuteCommand(e => {
return commandManager.executeCommand(e);
});

// Start listening for any messages from the client
connection.listen();