-
Notifications
You must be signed in to change notification settings - Fork 511
/
Copy pathGetCommands.ts
152 lines (130 loc) · 5.18 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import * as vscode from "vscode";
import { RequestType0 } from "vscode-languageclient";
import { LanguageClient } from "vscode-languageclient/node";
import { ILogger } from "../logging";
import { LanguageClientConsumer } from "../languageClientConsumer";
import { getSettings } from "../settings";
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 RequestType0<ICommand[], void>("powerShell/getCommand");
/**
* A PowerShell Command listing feature. Implements a treeview control.
*/
export class GetCommandsFeature extends LanguageClientConsumer {
private commands: vscode.Disposable[];
private commandsExplorerProvider: CommandsExplorerProvider;
private commandsExplorerTreeView: vscode.TreeView<Command>;
constructor(private logger: ILogger) {
super();
this.commands = [
vscode.commands.registerCommand("PowerShell.RefreshCommandsExplorer",
async () => await this.CommandExplorerRefresh()),
vscode.commands.registerCommand("PowerShell.InsertCommand", async (item) => await this.InsertCommand(item))
];
this.commandsExplorerProvider = new CommandsExplorerProvider();
this.commandsExplorerTreeView = vscode.window.createTreeView<Command>("PowerShellCommands",
{ treeDataProvider: this.commandsExplorerProvider });
// Refresh the command explorer when the view is visible
this.commandsExplorerTreeView.onDidChangeVisibility(async (e) => {
if (e.visible) {
await this.CommandExplorerRefresh();
}
});
}
public dispose() {
for (const command of this.commands) {
command.dispose();
}
}
public override setLanguageClient(languageclient: LanguageClient) {
this.languageClient = languageclient;
if (this.commandsExplorerTreeView.visible) {
void vscode.commands.executeCommand("PowerShell.RefreshCommandsExplorer");
}
}
private async CommandExplorerRefresh() {
if (this.languageClient === undefined) {
this.logger.writeVerbose(`<${GetCommandsFeature.name}>: Unable to send getCommand request`);
return;
}
await this.languageClient.sendRequest(GetCommandRequestType).then((result) => {
const exclusions = getSettings().sideBar.CommandExplorerExcludeFilter;
const excludeFilter = exclusions.map((filter: string) => filter.toLowerCase());
result = result.filter((command) => (!excludeFilter.includes(command.moduleName.toLowerCase())));
this.commandsExplorerProvider.powerShellCommands = result.map(toCommand);
this.commandsExplorerProvider.refresh();
});
}
private async InsertCommand(item: { Name: string; }) {
const editor = vscode.window.activeTextEditor;
if (editor === undefined) {
return;
}
const sls = editor.selection.start;
const sle = editor.selection.end;
const range = new vscode.Range(sls.line, sls.character, sle.line, sle.character);
await 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(undefined);
}
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 override readonly collapsibleState = vscode.TreeItemCollapsibleState.None,
) {
super(Name, collapsibleState);
}
public getTreeItem(): vscode.TreeItem {
return {
label: this.label,
collapsibleState: this.collapsibleState,
};
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/require-await
public async getChildren(_element?: any): Promise<Command[]> {
// Returning an empty array because we need to return something.
return [];
}
}