forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmockCommandManager.ts
More file actions
57 lines (52 loc) · 2.02 KB
/
mockCommandManager.ts
File metadata and controls
57 lines (52 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { noop } from 'lodash';
import { Disposable, TextEditor, TextEditorEdit } from 'vscode';
import { ICommandNameArgumentTypeMapping } from '../../client/common/application/commands';
import { ICommandManager } from '../../client/common/application/types';
// tslint:disable:no-any no-http-string no-multiline-string max-func-body-length
export class MockCommandManager implements ICommandManager {
private commands: Map<string, (...args: any[]) => any> = new Map<string, (...args: any[]) => any>();
public dispose() {
this.commands.clear();
}
public registerCommand<
E extends keyof ICommandNameArgumentTypeMapping,
U extends ICommandNameArgumentTypeMapping[E]
>(command: E, callback: (...args: U) => any, thisArg?: any): Disposable {
this.commands.set(command, thisArg ? (callback.bind(thisArg) as any) : (callback as any));
return {
dispose: () => {
noop();
}
};
}
public registerTextEditorCommand(
_command: string,
_callback: (textEditor: TextEditor, edit: TextEditorEdit, ...args: any[]) => void,
_thisArg?: any
): Disposable {
throw new Error('Method not implemented.');
}
public executeCommand<
T,
E extends keyof ICommandNameArgumentTypeMapping,
U extends ICommandNameArgumentTypeMapping[E]
>(command: E, ...rest: U): Thenable<T | undefined> {
const func = this.commands.get(command);
if (func) {
const result = func(...rest);
const tPromise = result as Promise<T>;
if (tPromise) {
return tPromise;
}
return Promise.resolve(result);
}
return Promise.resolve(undefined);
}
public getCommands(_filterInternal?: boolean): Thenable<string[]> {
const keys = Object.keys(this.commands);
return Promise.resolve(keys);
}
}