|
| 1 | +/*--------------------------------------------------------- |
| 2 | + * Copyright (C) Microsoft Corporation. All rights reserved. |
| 3 | + *--------------------------------------------------------*/ |
| 4 | +import * as vscode from "vscode"; |
| 5 | +import { LanguageClient, RequestType } from "vscode-languageclient"; |
| 6 | +import { IFeature } from "../feature"; |
| 7 | +import { Logger } from "../logging"; |
| 8 | + |
| 9 | +interface ICommand { |
| 10 | + name: string; |
| 11 | + moduleName: string; |
| 12 | + defaultParameterSet: string; |
| 13 | + parameterSets: object; |
| 14 | + parameters: object; |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * RequestType sent over to PSES. |
| 19 | + * Expects: ICommand to be returned |
| 20 | + */ |
| 21 | +export const GetCommandRequestType = new RequestType<string, ICommand[], void, void>("powerShell/getCommand"); |
| 22 | + |
| 23 | +/** |
| 24 | + * A PowerShell Command listing feature. Implements a treeview control. |
| 25 | + */ |
| 26 | +export class GetCommandsFeature implements IFeature { |
| 27 | + private command: vscode.Disposable; |
| 28 | + private languageClient: LanguageClient; |
| 29 | + private commandsExplorerProvider: CommandsExplorerProvider; |
| 30 | + |
| 31 | + constructor(private log: Logger) { |
| 32 | + this.command = vscode.commands.registerCommand("PowerShell.RefreshCommandsExplorer", |
| 33 | + () => this.CommandExplorerRefresh()); |
| 34 | + this.commandsExplorerProvider = new CommandsExplorerProvider(); |
| 35 | + vscode.window.registerTreeDataProvider("PowerShellCommands", this.commandsExplorerProvider); |
| 36 | + vscode.commands.registerCommand("PowerShell.InsertCommand", (item) => this.InsertCommand(item)); |
| 37 | + } |
| 38 | + |
| 39 | + public dispose() { |
| 40 | + this.command.dispose(); |
| 41 | + } |
| 42 | + |
| 43 | + public setLanguageClient(languageclient: LanguageClient) { |
| 44 | + this.languageClient = languageclient; |
| 45 | + vscode.commands.executeCommand("PowerShell.RefreshCommandsExplorer"); |
| 46 | + } |
| 47 | + |
| 48 | + private CommandExplorerRefresh() { |
| 49 | + if (this.languageClient === undefined) { |
| 50 | + this.log.writeAndShowError(`<${GetCommandsFeature.name}>: ` + |
| 51 | + "Unable to instantiate; language client undefined."); |
| 52 | + return; |
| 53 | + } |
| 54 | + this.languageClient.sendRequest(GetCommandRequestType, "").then((result) => { |
| 55 | + this.commandsExplorerProvider.powerShellCommands = result.map(toCommand); |
| 56 | + this.commandsExplorerProvider.refresh(); |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + private InsertCommand(item) { |
| 61 | + const editor = vscode.window.activeTextEditor; |
| 62 | + const sls = editor.selection.start; |
| 63 | + const sle = editor.selection.end; |
| 64 | + const range = new vscode.Range(sls.line, sls.character, sle.line, sle.character); |
| 65 | + editor.edit((editBuilder) => { |
| 66 | + editBuilder.replace(range, item.Name); |
| 67 | + }); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +class CommandsExplorerProvider implements vscode.TreeDataProvider<Command> { |
| 72 | + public readonly onDidChangeTreeData: vscode.Event<Command | undefined>; |
| 73 | + public powerShellCommands: Command[]; |
| 74 | + private didChangeTreeData: vscode.EventEmitter<Command | undefined> = new vscode.EventEmitter<Command>(); |
| 75 | + |
| 76 | + constructor() { |
| 77 | + this.onDidChangeTreeData = this.didChangeTreeData.event; |
| 78 | + } |
| 79 | + |
| 80 | + public refresh(): void { |
| 81 | + this.didChangeTreeData.fire(); |
| 82 | + } |
| 83 | + |
| 84 | + public getTreeItem(element: Command): vscode.TreeItem { |
| 85 | + return element; |
| 86 | + } |
| 87 | + |
| 88 | + public getChildren(element?: Command): Thenable<Command[]> { |
| 89 | + return Promise.resolve(this.powerShellCommands || []); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +function toCommand(command: ICommand): Command { |
| 94 | + return new Command( |
| 95 | + command.name, |
| 96 | + command.moduleName, |
| 97 | + command.defaultParameterSet, |
| 98 | + command.parameterSets, |
| 99 | + command.parameters, |
| 100 | + ); |
| 101 | +} |
| 102 | + |
| 103 | +class Command extends vscode.TreeItem { |
| 104 | + constructor( |
| 105 | + public readonly Name: string, |
| 106 | + public readonly ModuleName: string, |
| 107 | + public readonly defaultParameterSet: string, |
| 108 | + public readonly ParameterSets: object, |
| 109 | + public readonly Parameters: object, |
| 110 | + public readonly collapsibleState = vscode.TreeItemCollapsibleState.None, |
| 111 | + ) { |
| 112 | + super(Name, collapsibleState); |
| 113 | + } |
| 114 | + |
| 115 | + public getTreeItem(): vscode.TreeItem { |
| 116 | + return { |
| 117 | + label: this.label, |
| 118 | + collapsibleState: this.collapsibleState, |
| 119 | + }; |
| 120 | + } |
| 121 | + |
| 122 | + public async getChildren(element?): Promise<Command[]> { |
| 123 | + return []; |
| 124 | + // Returning an empty array because we need to return something. |
| 125 | + } |
| 126 | + |
| 127 | +} |
0 commit comments