|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as vscode from 'vscode'; |
| 7 | +import type { ICompletionResource } from '../types'; |
| 8 | +import { type ExecOptionsWithStringEncoding } from 'node:child_process'; |
| 9 | +import { execHelper } from './common'; |
| 10 | + |
| 11 | +export async function getPwshGlobals(options: ExecOptionsWithStringEncoding, existingCommands?: Set<string>): Promise<(string | ICompletionResource)[]> { |
| 12 | + return [ |
| 13 | + ...await getAliases(options, existingCommands), |
| 14 | + ...await getCommands(options, existingCommands), |
| 15 | + ]; |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * The numeric values associated with CommandType from Get-Command. It appears that this is a |
| 20 | + * bitfield based on the values but I think it's actually used as an enum where a CommandType can |
| 21 | + * only be a single one of these. |
| 22 | + * |
| 23 | + * Source: |
| 24 | + * |
| 25 | + * ``` |
| 26 | + * [enum]::GetValues([System.Management.Automation.CommandTypes]) | ForEach-Object { |
| 27 | + * [pscustomobject]@{ |
| 28 | + * Name = $_ |
| 29 | + * Value = [int]$_ |
| 30 | + * } |
| 31 | + * } |
| 32 | + * ``` |
| 33 | + */ |
| 34 | +const enum PwshCommandType { |
| 35 | + Alias = 1, |
| 36 | + Function = 2, |
| 37 | + Filter = 4, |
| 38 | + Cmdlet = 8, |
| 39 | + ExternalScript = 16, |
| 40 | + Application = 32, |
| 41 | + Script = 64, |
| 42 | + Configuration = 256, |
| 43 | + // All = 383, |
| 44 | +} |
| 45 | + |
| 46 | +const pwshCommandTypeToCompletionKind: Map<PwshCommandType, vscode.TerminalCompletionItemKind> = new Map([ |
| 47 | + [PwshCommandType.Alias, vscode.TerminalCompletionItemKind.Alias], |
| 48 | + [PwshCommandType.Function, vscode.TerminalCompletionItemKind.Method], |
| 49 | + [PwshCommandType.Filter, vscode.TerminalCompletionItemKind.Method], |
| 50 | + [PwshCommandType.Cmdlet, vscode.TerminalCompletionItemKind.Method], |
| 51 | + [PwshCommandType.ExternalScript, vscode.TerminalCompletionItemKind.Method], |
| 52 | + [PwshCommandType.Application, vscode.TerminalCompletionItemKind.Method], |
| 53 | + [PwshCommandType.Script, vscode.TerminalCompletionItemKind.Method], |
| 54 | + [PwshCommandType.Configuration, vscode.TerminalCompletionItemKind.Argument], |
| 55 | +]); |
| 56 | + |
| 57 | +async function getAliases(options: ExecOptionsWithStringEncoding, existingCommands?: Set<string>): Promise<ICompletionResource[]> { |
| 58 | + const output = await execHelper('Get-Command -CommandType Alias | Select-Object Name, CommandType, Definition, DisplayName, ModuleName, @{Name="Version";Expression={$_.Version.ToString()}} | ConvertTo-Json', { |
| 59 | + ...options, |
| 60 | + maxBuffer: 1024 * 1024 * 100 // This is a lot of content, increase buffer size |
| 61 | + }); |
| 62 | + let json: any; |
| 63 | + try { |
| 64 | + json = JSON.parse(output); |
| 65 | + } catch (e) { |
| 66 | + console.error('Error parsing output:', e); |
| 67 | + return []; |
| 68 | + } |
| 69 | + return (json as any[]).map(e => { |
| 70 | + // Aliases sometimes use the same Name and DisplayName, show them as methods in this case. |
| 71 | + const isAlias = e.Name !== e.DisplayName; |
| 72 | + const detailParts: string[] = []; |
| 73 | + if (e.Definition) { |
| 74 | + detailParts.push(e.Definition); |
| 75 | + } |
| 76 | + if (e.ModuleName && e.Version) { |
| 77 | + detailParts.push(`${e.ModuleName} v${e.Version}`); |
| 78 | + } |
| 79 | + return { |
| 80 | + label: e.Name, |
| 81 | + detail: detailParts.join('\n\n'), |
| 82 | + kind: (isAlias |
| 83 | + ? vscode.TerminalCompletionItemKind.Alias |
| 84 | + : vscode.TerminalCompletionItemKind.Method), |
| 85 | + }; |
| 86 | + }); |
| 87 | +} |
| 88 | + |
| 89 | +async function getCommands(options: ExecOptionsWithStringEncoding, existingCommands?: Set<string>): Promise<ICompletionResource[]> { |
| 90 | + const output = await execHelper('Get-Command -All | Select-Object Name, CommandType, Definition, ModuleName, @{Name="Version";Expression={$_.Version.ToString()}} | ConvertTo-Json', { |
| 91 | + ...options, |
| 92 | + maxBuffer: 1024 * 1024 * 100 // This is a lot of content, increase buffer size |
| 93 | + }); |
| 94 | + let json: any; |
| 95 | + try { |
| 96 | + json = JSON.parse(output); |
| 97 | + } catch (e) { |
| 98 | + console.error('Error parsing pwsh output:', e); |
| 99 | + return []; |
| 100 | + } |
| 101 | + return ( |
| 102 | + (json as any[]) |
| 103 | + .filter(e => e.CommandType !== PwshCommandType.Alias) |
| 104 | + .map(e => { |
| 105 | + const detailParts: string[] = []; |
| 106 | + if (e.Definition) { |
| 107 | + detailParts.push(e.Definition.trim()); |
| 108 | + } |
| 109 | + if (e.ModuleName && e.Version) { |
| 110 | + detailParts.push(`${e.ModuleName} v${e.Version}`); |
| 111 | + } |
| 112 | + return { |
| 113 | + label: e.Name, |
| 114 | + detail: detailParts.join('\n\n'), |
| 115 | + kind: pwshCommandTypeToCompletionKind.get(e.CommandType) |
| 116 | + }; |
| 117 | + }) |
| 118 | + ); |
| 119 | +} |
0 commit comments