-
Notifications
You must be signed in to change notification settings - Fork 12
feat: API for registering custom callbacks for Commands #117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
fc6e182
4b54305
672bd41
eb1cc74
46a486d
d569a46
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { ExecuteCommandParams, ServerCapabilities } from "../languageclient"; | ||
| import { LanguageClientConnection } from "../main"; | ||
|
|
||
| export type CommandCustomCallbackFunction = (command: ExecuteCommandParams) => Promise<any | void>; | ||
|
|
||
| export default class CommandExecutionAdapter { | ||
| private static commandsCustomCallbacks = new Map<string, CommandCustomCallbackFunction>(); | ||
|
|
||
| public static canAdapt(serverCapabilities: ServerCapabilities): boolean { | ||
| return serverCapabilities.executeCommandProvider != null; | ||
| } | ||
|
|
||
| public static registerCustomCallbackForCommand(command: string, callback: CommandCustomCallbackFunction): void { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How are we exposing this function to the package author?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Come to think about it, I just assumed one would invoke that method directly via the EDIT: Just realized you were referring to the fact I forgot to expose the Adapter itself out. Fixed it and added canAdapt and tests for it as well (I thought it is a necessity for all language servers for some reason). So do you think using |
||
| this.commandsCustomCallbacks.set(command, callback); | ||
| } | ||
|
|
||
| public static async executeCommand(connection: LanguageClientConnection, command: string, commandArgs?: any[]): Promise<any | void> { | ||
| const executeCommandParams = CommandExecutionAdapter.createExecuteCommandParams(command, commandArgs); | ||
| const commandCustomCallback = this.commandsCustomCallbacks.get(command); | ||
|
|
||
| return commandCustomCallback !== undefined ? await commandCustomCallback(executeCommandParams) : await connection.executeCommand(executeCommandParams); | ||
| } | ||
|
|
||
| private static createExecuteCommandParams(command: string, commandArgs?: any[]): ExecuteCommandParams { | ||
| return { | ||
| command: command, | ||
| arguments: commandArgs | ||
| }; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import { expect } from 'chai'; | ||
| import * as sinon from 'sinon'; | ||
| import * as ls from '../../lib/languageclient'; | ||
| import CommandExecutionAdapter, { CommandCustomCallbackFunction } from '../../lib/adapters/command-execution-adapter'; | ||
| import { createSpyConnection } from '../helpers.js'; | ||
| import { ExecuteCommandParams } from '../../lib/languageclient'; | ||
|
|
||
| describe('CommandExecutionAdapter', () => { | ||
| describe('canAdapt', () => { | ||
| it('returns true if command execution is supported', () => { | ||
| const result = CommandExecutionAdapter.canAdapt({ | ||
| executeCommandProvider: {commands: []}, | ||
| }); | ||
| expect(result).to.be.true; | ||
| }); | ||
|
|
||
| it('returns false it no formatting supported', () => { | ||
| const result = CommandExecutionAdapter.canAdapt({}); | ||
| expect(result).to.be.false; | ||
| }); | ||
| }); | ||
|
|
||
| describe('executeCommand', () => { | ||
| it('invokes an executeCommand object from given inputs', async () => { | ||
| const connection = createSpyConnection(); | ||
| const languageClient = new ls.LanguageClientConnection(connection); | ||
| const testCommand = { | ||
| command: 'testCommand', | ||
| arguments: ['a', 'b'], | ||
| }; | ||
| sinon.stub(languageClient, 'executeCommand').returns(Promise.resolve(testCommand)); | ||
|
|
||
| const result = await CommandExecutionAdapter.executeCommand( | ||
| languageClient, | ||
| testCommand.command, | ||
| testCommand.arguments | ||
| ); | ||
|
|
||
| expect(result.command).to.equal(testCommand.command); | ||
| expect(result.arguments).to.equal(testCommand.arguments); | ||
|
|
||
| expect((languageClient as any).executeCommand.called).to.be.true; | ||
| expect((languageClient as any).executeCommand.getCalls()[0].args).to.deep.equal([{ | ||
| command: testCommand.command, | ||
| arguments: testCommand.arguments | ||
| } as ExecuteCommandParams]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('registerCustomCallbackForCommand', () => { | ||
| it('registers a custom callback for a command, to be executed on executeCommand', async () => { | ||
| const connection = createSpyConnection(); | ||
| const languageClient = new ls.LanguageClientConnection(connection); | ||
| const testCallback: CommandCustomCallbackFunction = (command: ExecuteCommandParams) => Promise.resolve(command.command); | ||
| const testCommand = { | ||
| command: 'testCommand', | ||
| arguments: ['a', 'b'], | ||
| }; | ||
|
|
||
| const spiedCallback = sinon.spy(testCallback); | ||
| sinon.spy(languageClient, 'executeCommand'); | ||
|
|
||
| CommandExecutionAdapter.registerCustomCallbackForCommand(testCommand.command, spiedCallback); | ||
|
|
||
| const result = await CommandExecutionAdapter.executeCommand( | ||
| languageClient, | ||
| testCommand.command, | ||
| testCommand.arguments | ||
| ); | ||
|
|
||
| expect(spiedCallback.called).to.be.true; | ||
|
|
||
| expect((languageClient as any).executeCommand.called).to.be.false; | ||
|
|
||
| expect(result).to.equal(testCommand.command); | ||
| }); | ||
| }); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.