-
Notifications
You must be signed in to change notification settings - Fork 510
/
Copy pathGetCommands.ts
130 lines (112 loc) · 4.33 KB
/
GetCommands.ts
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import * as vscode from "vscode";
import { LanguageClient, RequestType } from "vscode-languageclient";
import { IFeature } from "../feature";
import { Logger } from "../logging";
interface ICommand {
name: string;
moduleName: string;
defaultParameterSet: string;
parameterSets: object;
parameters: object;
}
/**
* RequestType sent over to PSES.
* Expects: ICommand to be returned
*/
export const GetCommandRequestType = new RequestType<string, ICommand[], void, void>("powerShell/getCommand");
/**
* A PowerShell Command listing feature. Implements a treeview control.
*/
export class GetCommandsFeature implements IFeature {
private command: vscode.Disposable;
private languageClient: LanguageClient;
private commandsExplorerProvider: CommandsExplorerProvider;
constructor(private log: Logger) {
this.command = vscode.commands.registerCommand("PowerShell.RefreshCommandsExplorer",
() => this.CommandExplorerRefresh());
this.commandsExplorerProvider = new CommandsExplorerProvider();
vscode.window.registerTreeDataProvider("PowerShellCommands", this.commandsExplorerProvider);
vscode.commands.registerCommand("PowerShell.InsertCommand", (item) => this.InsertCommand(item));
}
public dispose() {
this.command.dispose();
}
public setLanguageClient(languageclient: LanguageClient) {
this.languageClient = languageclient;
const SideBarConfiguration = vscode.workspace.getConfiguration("powershell.sideBar");
if (SideBarConfiguration.CommandExplorerInitialRefresh) {
vscode.commands.executeCommand("PowerShell.RefreshCommandsExplorer");
}
}
private CommandExplorerRefresh() {
if (this.languageClient === undefined) {
this.log.writeAndShowError(`<${GetCommandsFeature.name}>: ` +
"Unable to instantiate; language client undefined.");
return;
}
this.languageClient.sendRequest(GetCommandRequestType, "").then((result) => {
this.commandsExplorerProvider.powerShellCommands = result.map(toCommand);
this.commandsExplorerProvider.refresh();
});
}
private InsertCommand(item) {
const editor = vscode.window.activeTextEditor;
const sls = editor.selection.start;
const sle = editor.selection.end;
const range = new vscode.Range(sls.line, sls.character, sle.line, sle.character);
editor.edit((editBuilder) => {
editBuilder.replace(range, item.Name);
});
}
}
class CommandsExplorerProvider implements vscode.TreeDataProvider<Command> {
public readonly onDidChangeTreeData: vscode.Event<Command | undefined>;
public powerShellCommands: Command[];
private didChangeTreeData: vscode.EventEmitter<Command | undefined> = new vscode.EventEmitter<Command>();
constructor() {
this.onDidChangeTreeData = this.didChangeTreeData.event;
}
public refresh(): void {
this.didChangeTreeData.fire();
}
public getTreeItem(element: Command): vscode.TreeItem {
return element;
}
public getChildren(element?: Command): Thenable<Command[]> {
return Promise.resolve(this.powerShellCommands || []);
}
}
function toCommand(command: ICommand): Command {
return new Command(
command.name,
command.moduleName,
command.defaultParameterSet,
command.parameterSets,
command.parameters,
);
}
class Command extends vscode.TreeItem {
constructor(
public readonly Name: string,
public readonly ModuleName: string,
public readonly defaultParameterSet: string,
public readonly ParameterSets: object,
public readonly Parameters: object,
public readonly collapsibleState = vscode.TreeItemCollapsibleState.None,
) {
super(Name, collapsibleState);
}
public getTreeItem(): vscode.TreeItem {
return {
label: this.label,
collapsibleState: this.collapsibleState,
};
}
public async getChildren(element?): Promise<Command[]> {
return [];
// Returning an empty array because we need to return something.
}
}