|
| 1 | +// |
| 2 | +// Copyright (c) Microsoft. All rights reserved. |
| 3 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 4 | +// |
| 5 | + |
| 6 | +using System.Collections.Concurrent; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Management.Automation; |
| 9 | +using Microsoft.PowerShell.EditorServices.Symbols; |
| 10 | + |
| 11 | +namespace Microsoft.PowerShell.EditorServices |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// A class for containing the commandName, the command's |
| 15 | + /// possible signatures, and the script extent of the command |
| 16 | + /// </summary> |
| 17 | + public class ParameterSetSignatures |
| 18 | + { |
| 19 | + #region Properties |
| 20 | + /// <summary> |
| 21 | + /// Gets the name of the command |
| 22 | + /// </summary> |
| 23 | + public string CommandName { get; internal set; } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Gets the collection of signatures for the command |
| 27 | + /// </summary> |
| 28 | + public ParameterSetSignature[] Signatures { get; internal set; } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Gets the script extent of the command |
| 32 | + /// </summary> |
| 33 | + public ScriptRegion ScriptRegion { get; internal set; } |
| 34 | + #endregion |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Constructs an instance of a ParameterSetSignatures object |
| 38 | + /// </summary> |
| 39 | + /// <param name="commandInfoSet">Collection of parameter set info</param> |
| 40 | + /// <param name="foundSymbol"> The SymbolReference of the command</param> |
| 41 | + public ParameterSetSignatures(IEnumerable<CommandParameterSetInfo> commandInfoSet, SymbolReference foundSymbol) |
| 42 | + { |
| 43 | + List<ParameterSetSignature> paramSetSignatures = new List<ParameterSetSignature>(); |
| 44 | + foreach (CommandParameterSetInfo setInfo in commandInfoSet) |
| 45 | + { |
| 46 | + paramSetSignatures.Add(new ParameterSetSignature(setInfo)); |
| 47 | + } |
| 48 | + Signatures = paramSetSignatures.ToArray(); |
| 49 | + CommandName = foundSymbol.ScriptRegion.Text; |
| 50 | + ScriptRegion = foundSymbol.ScriptRegion; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// A class for containing the signature text and the collection of parameters for a signature |
| 56 | + /// </summary> |
| 57 | + public class ParameterSetSignature |
| 58 | + { |
| 59 | + private static readonly ConcurrentDictionary<string, bool> commonParameterNames = |
| 60 | + new ConcurrentDictionary<string, bool>(); |
| 61 | + |
| 62 | + static ParameterSetSignature() |
| 63 | + { |
| 64 | + commonParameterNames.TryAdd("Verbose", true); |
| 65 | + commonParameterNames.TryAdd("Debug", true); |
| 66 | + commonParameterNames.TryAdd("ErrorAction", true); |
| 67 | + commonParameterNames.TryAdd("WarningAction", true); |
| 68 | + commonParameterNames.TryAdd("InformationAction", true); |
| 69 | + commonParameterNames.TryAdd("ErrorVariable", true); |
| 70 | + commonParameterNames.TryAdd("WarningVariable", true); |
| 71 | + commonParameterNames.TryAdd("InformationVariable", true); |
| 72 | + commonParameterNames.TryAdd("OutVariable", true); |
| 73 | + commonParameterNames.TryAdd("OutBuffer", true); |
| 74 | + commonParameterNames.TryAdd("PipelineVariable", true); |
| 75 | + } |
| 76 | + |
| 77 | + #region Properties |
| 78 | + /// <summary> |
| 79 | + /// Gets the signature text |
| 80 | + /// </summary> |
| 81 | + public string SignatureText { get; internal set; } |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Gets the collection of parameters for the signature |
| 85 | + /// </summary> |
| 86 | + public IEnumerable<ParameterInfo> Parameters { get; internal set; } |
| 87 | + #endregion |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// Constructs an instance of a ParameterSetSignature |
| 91 | + /// </summary> |
| 92 | + /// <param name="commandParamInfoSet">Collection of parameter info</param> |
| 93 | + public ParameterSetSignature(CommandParameterSetInfo commandParamInfoSet) |
| 94 | + { |
| 95 | + List<ParameterInfo> parameterInfo = new List<ParameterInfo>(); |
| 96 | + foreach (CommandParameterInfo commandParameterInfo in commandParamInfoSet.Parameters) |
| 97 | + { |
| 98 | + if (!commonParameterNames.ContainsKey(commandParameterInfo.Name)) |
| 99 | + { |
| 100 | + parameterInfo.Add(new ParameterInfo(commandParameterInfo)); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + SignatureText = commandParamInfoSet.ToString(); |
| 105 | + Parameters = parameterInfo.ToArray(); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// A class for containing the parameter info of a parameter |
| 111 | + /// </summary> |
| 112 | + public class ParameterInfo |
| 113 | + { |
| 114 | + #region Properties |
| 115 | + /// <summary> |
| 116 | + /// Gets the name of the parameter |
| 117 | + /// </summary> |
| 118 | + public string Name { get; internal set; } |
| 119 | + |
| 120 | + /// <summary> |
| 121 | + /// Gets the type of the parameter |
| 122 | + /// </summary> |
| 123 | + public string ParameterType { get; internal set; } |
| 124 | + |
| 125 | + /// <summary> |
| 126 | + /// Gets the position of the parameter |
| 127 | + /// </summary> |
| 128 | + public int Position { get; internal set; } |
| 129 | + |
| 130 | + /// <summary> |
| 131 | + /// Gets a boolean for whetheer or not the parameter is required |
| 132 | + /// </summary> |
| 133 | + public bool IsMandatory { get; internal set; } |
| 134 | + |
| 135 | + /// <summary> |
| 136 | + /// Gets the help message of the parameter |
| 137 | + /// </summary> |
| 138 | + public string HelpMessage { get; internal set; } |
| 139 | + #endregion |
| 140 | + |
| 141 | + /// <summary> |
| 142 | + /// Constructs an instance of a ParameterInfo object |
| 143 | + /// </summary> |
| 144 | + /// <param name="parameterInfo">Parameter info of the parameter</param> |
| 145 | + public ParameterInfo(CommandParameterInfo parameterInfo) |
| 146 | + { |
| 147 | + this.Name = "-" + parameterInfo.Name; |
| 148 | + this.ParameterType = parameterInfo.ParameterType.FullName; |
| 149 | + this.Position = parameterInfo.Position; |
| 150 | + this.IsMandatory = parameterInfo.IsMandatory; |
| 151 | + this.HelpMessage = parameterInfo.HelpMessage; |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments