MegaBites-AI/Windows-powershell
0372
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System;5 6namespace System.Management.Automation7{8 /// <summary>9 /// Possible types of CompletionResults.10 /// </summary>11 public enum CompletionResultType12 {13 /// <summary> An unknown result type, kept as text only.</summary>14 Text = 0,15 16 /// <summary>A history result type like the items out of get-history.</summary>17 History = 1,18 19 /// <summary>A command result type like the items out of get-command.</summary>20 Command = 2,21 22 /// <summary>A provider item.</summary>23 ProviderItem = 3,24 25 /// <summary>A provider container.</summary>26 ProviderContainer = 4,27 28 /// <summary>A property result type like the property items out of get-member.</summary>29 Property = 5,30 31 /// <summary>A method result type like the method items out of get-member.</summary>32 Method = 6,33 34 /// <summary>A parameter name result type like the Parameters property out of get-command items.</summary>35 ParameterName = 7,36 37 /// <summary>A parameter value result type.</summary>38 ParameterValue = 8,39 40 /// <summary>A variable result type like the items out of get-childitem variable.</summary>41 Variable = 9,42 43 /// <summary>A namespace.</summary>44 Namespace = 10,45 46 /// <summary>A type name.</summary>47 Type = 11,48 49 /// <summary>A keyword.</summary>50 Keyword = 12,51 52 /// <summary>A dynamic keyword.</summary>53 DynamicKeyword = 13,54 55 // If a new enum is added, there is a range test that uses DynamicKeyword for parameter validation56 // that needs to be updated to use the new enum.57 // We can't use a "MaxValue" enum because it's value would preclude ever adding a new enum.58 }59 60 /// <summary>61 /// Class used to store a tab completion or Intellisense result.62 /// </summary>63 public class CompletionResult64 {65 /// <summary>66 /// Text to be used as the auto completion result.67 /// </summary>68 private readonly string _completionText;69 70 /// <summary>71 /// Text to be displayed in a list.72 /// </summary>73 private readonly string _listItemText;74 75 /// <summary>76 /// The text for the tooltip with details to be displayed about the object.77 /// </summary>78 private readonly string _toolTip;79 80 /// <summary>81 /// Type of completion result.82 /// </summary>83 private readonly CompletionResultType _resultType;84 85 /// <summary>86 /// Private member for null instance.87 /// </summary>88 private static readonly CompletionResult s_nullInstance = new CompletionResult();89 90 /// <summary>91 /// Gets the text to be used as the auto completion result.92 /// </summary>93 public string CompletionText94 {95 get96 {97 if (this == s_nullInstance)98 {99 throw PSTraceSource.NewInvalidOperationException(TabCompletionStrings.NoAccessToProperties);100 }101 102 return _completionText;103 }104 }105 106 /// <summary>107 /// Gets the text to be displayed in a list.108 /// </summary>109 public string ListItemText110 {111 get112 {113 if (this == s_nullInstance)114 {115 throw PSTraceSource.NewInvalidOperationException(TabCompletionStrings.NoAccessToProperties);116 }117 118 return _listItemText;119 }120 }121 122 /// <summary>123 /// Gets the type of completion result.124 /// </summary>125 public CompletionResultType ResultType126 {127 get128 {129 if (this == s_nullInstance)130 {131 throw PSTraceSource.NewInvalidOperationException(TabCompletionStrings.NoAccessToProperties);132 }133 134 return _resultType;135 }136 }137 138 /// <summary>139 /// Gets the text for the tooltip with details to be displayed about the object.140 /// </summary>141 public string ToolTip142 {143 get144 {145 if (this == s_nullInstance)146 {147 throw PSTraceSource.NewInvalidOperationException(TabCompletionStrings.NoAccessToProperties);148 }149 150 return _toolTip;151 }152 }153 154 /// <summary>155 /// Gets the null instance of type CompletionResult.156 /// </summary>157 internal static CompletionResult Null158 {159 get { return s_nullInstance; }160 }161 162 /// <summary>163 /// Initializes a new instance of the CompletionResult class.164 /// </summary>165 /// <param name="completionText">The text to be used as the auto completion result.</param>166 /// <param name="listItemText">The text to be displayed in a list.</param>167 /// <param name="resultType">The type of completion result.</param>168 /// <param name="toolTip">The text for the tooltip with details to be displayed about the object.</param>169 public CompletionResult(string completionText, string listItemText, CompletionResultType resultType, string toolTip)170 {171 ArgumentException.ThrowIfNullOrEmpty(completionText);172 ArgumentException.ThrowIfNullOrEmpty(listItemText);173 ArgumentException.ThrowIfNullOrEmpty(toolTip);174 175 if (resultType < CompletionResultType.Text || resultType > CompletionResultType.DynamicKeyword)176 {177 throw PSTraceSource.NewArgumentOutOfRangeException(nameof(resultType), resultType);178 }179 180 _completionText = completionText;181 _listItemText = listItemText;182 _toolTip = toolTip;183 _resultType = resultType;184 }185 186 /// <summary>187 /// Initializes a new instance of this class internally if the result out of TabExpansion is a string.188 /// </summary>189 /// <param name="completionText">Completion text.</param>190 public CompletionResult(string completionText)191 : this(completionText, completionText, CompletionResultType.Text, completionText)192 {193 }194 195 /// <summary>196 /// An null instance of CompletionResult.197 /// </summary>198 /// <remarks>199 /// This can be used in argument completion, to indicate that the completion attempt has gone through the200 /// native command argument completion methods.201 /// </remarks>202 private CompletionResult() { }203 }204}205 