Skip to content

Commit fc6e182

Browse files
deankevorkianaminya
authored andcommitted
feat: introduce an Adapter for executeCommand that also allows registering custom callbacks
1 parent 2557ebb commit fc6e182

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

lib/adapters/code-action-adapter.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
Range,
1616
TextEditor,
1717
} from 'atom';
18+
import CommandExecutionAdapter from './command-execution-adapter';
1819

1920
export default class CodeActionAdapter {
2021
/**
@@ -88,10 +89,7 @@ export default class CodeActionAdapter {
8889
connection: LanguageClientConnection,
8990
): Promise<void> {
9091
if (Command.is(command)) {
91-
await connection.executeCommand({
92-
command: command.command,
93-
arguments: command.arguments,
94-
});
92+
await CommandExecutionAdapter.executeCommand(connection, command.command, command.arguments);
9593
}
9694
}
9795

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Command, ExecuteCommandParams } from "../languageclient";
2+
import { LanguageClientConnection } from "../main";
3+
4+
export type CommandCustomCallbackFunction = (command: ExecuteCommandParams) => Promise<void>;
5+
6+
export default class CommandExecutionAdapter {
7+
private static commandsCustomCallbacks: Map<string, CommandCustomCallbackFunction> = new Map<string, CommandCustomCallbackFunction>();
8+
9+
public static registerCustomCallbackForCommand(command: string, callback: CommandCustomCallbackFunction) {
10+
this.commandsCustomCallbacks.set(command, callback);
11+
}
12+
13+
public static async executeCommand(connection: LanguageClientConnection, command: string, commandArgs?: any[] | undefined): Promise<any | void> {
14+
const executeCommandParams = CommandExecutionAdapter.createExecuteCommandParams(command, commandArgs);
15+
const commandCustomCallback = this.commandsCustomCallbacks.get(command);
16+
17+
return commandCustomCallback != null ? await commandCustomCallback(executeCommandParams) : await connection.executeCommand(executeCommandParams);
18+
}
19+
20+
public static createExecuteCommandParams(command: string, commandArgs?: any[] | undefined): ExecuteCommandParams {
21+
return {
22+
command: command,
23+
arguments: commandArgs
24+
};
25+
}
26+
}

0 commit comments

Comments
 (0)