Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite127345

Browse files
authored
Add-PropertyType argument completer forNew-ItemProperty (#21117)
1 parent8017195 commite127345

File tree

3 files changed

+223
-0
lines changed

3 files changed

+223
-0
lines changed

‎src/Microsoft.PowerShell.Commands.Management/commands/management/NewPropertyCommand.cs

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
usingSystem.Collections;
5+
usingSystem.Collections.Generic;
6+
usingSystem.Collections.ObjectModel;
47
usingSystem.Management.Automation;
8+
usingSystem.Management.Automation.Language;
59

610
namespaceMicrosoft.PowerShell.Commands
711
{
@@ -63,6 +67,9 @@ public string[] LiteralPath
6367
/// </summary>
6468
[Parameter(ValueFromPipelineByPropertyName=true)]
6569
[Alias("Type")]
70+
#if!UNIX
71+
[ArgumentCompleter(typeof(PropertyTypeArgumentCompleter))]
72+
#endif
6673
publicstringPropertyType{get;set;}
6774

6875
/// <summary>
@@ -175,4 +182,118 @@ protected override void ProcessRecord()
175182
#endregion Command code
176183

177184
}
185+
186+
#if!UNIX
187+
/// <summary>
188+
/// Provides argument completion for PropertyType parameter.
189+
/// </summary>
190+
publicclassPropertyTypeArgumentCompleter:IArgumentCompleter
191+
{
192+
privatestaticreadonlystring[]s_RegistryPropertyTypes=newstring[]
193+
{
194+
"String",
195+
"ExpandString",
196+
"Binary",
197+
"DWord",
198+
"MultiString",
199+
"QWord",
200+
"Unknown"
201+
};
202+
203+
privatestaticstringGetRegistryPropertyTypeToolTip(stringpropertyTypeName)=>propertyTypeNameswitch
204+
{
205+
"String"=>TabCompletionStrings.RegistryStringToolTip,
206+
"ExpandString"=>TabCompletionStrings.RegistryExpandStringToolTip,
207+
"Binary"=>TabCompletionStrings.RegistryBinaryToolTip,
208+
"DWord"=>TabCompletionStrings.RegistryDWordToolTip,
209+
"MultiString"=>TabCompletionStrings.RegistryMultiStringToolTip,
210+
"QWord"=>TabCompletionStrings.RegistryQWordToolTip,
211+
_=>TabCompletionStrings.RegistryUnknownToolTip
212+
};
213+
214+
/// <summary>
215+
/// Returns completion results for PropertyType parameter.
216+
/// </summary>
217+
/// <param name="commandName">The command name.</param>
218+
/// <param name="parameterName">The parameter name.</param>
219+
/// <param name="wordToComplete">The word to complete.</param>
220+
/// <param name="commandAst">The command AST.</param>
221+
/// <param name="fakeBoundParameters">The fake bound parameters.</param>
222+
/// <returns>List of Completion Results.</returns>
223+
publicIEnumerable<CompletionResult>CompleteArgument(
224+
stringcommandName,
225+
stringparameterName,
226+
stringwordToComplete,
227+
CommandAstcommandAst,
228+
IDictionaryfakeBoundParameters)
229+
{
230+
if(!IsRegistryProvider(fakeBoundParameters))
231+
{
232+
yieldbreak;
233+
}
234+
235+
stringquote=CompletionCompleters.HandleDoubleAndSingleQuote(refwordToComplete);
236+
varpropertyTypePattern=WildcardPattern.Get(wordToComplete+"*",WildcardOptions.IgnoreCase);
237+
238+
foreach(stringpropertyTypeins_RegistryPropertyTypes)
239+
{
240+
if(propertyTypePattern.IsMatch(propertyType))
241+
{
242+
stringcompletionText=quote==string.Empty
243+
?propertyType
244+
:quote+propertyType+quote;
245+
246+
yieldreturnnewCompletionResult(
247+
completionText,
248+
propertyType,
249+
CompletionResultType.ParameterValue,
250+
GetRegistryPropertyTypeToolTip(propertyType));
251+
}
252+
}
253+
}
254+
255+
/// <summary>
256+
/// Checks if parameter paths are from Registry provider.
257+
/// </summary>
258+
/// <param name="fakeBoundParameters">The fake bound parameters.</param>
259+
/// <returns>Boolean indicating if paths are from Registry Provider.</returns>
260+
privatestaticboolIsRegistryProvider(IDictionaryfakeBoundParameters)
261+
{
262+
Collection<PathInfo>paths;
263+
264+
if(fakeBoundParameters.Contains("Path"))
265+
{
266+
paths=ResolvePath(fakeBoundParameters["Path"],isLiteralPath:false);
267+
}
268+
elseif(fakeBoundParameters.Contains("LiteralPath"))
269+
{
270+
paths=ResolvePath(fakeBoundParameters["LiteralPath"],isLiteralPath:true);
271+
}
272+
else
273+
{
274+
paths=ResolvePath(@".\",isLiteralPath:false);
275+
}
276+
277+
returnpaths.Count>0&&paths[0].Provider.NameEquals("Registry");
278+
}
279+
280+
/// <summary>
281+
/// Resolve path or literal path using Resolve-Path.
282+
/// </summary>
283+
/// <param name="path">The path to resolve.</param>
284+
/// <param name="isLiteralPath">Specifies if path is literal path.</param>
285+
/// <returns>Collection of Pathinfo objects.</returns>
286+
privatestaticCollection<PathInfo>ResolvePath(objectpath,boolisLiteralPath)
287+
{
288+
usingvarps=System.Management.Automation.PowerShell.Create(RunspaceMode.CurrentRunspace);
289+
290+
ps.AddCommand("Microsoft.PowerShell.Management\\Resolve-Path");
291+
ps.AddParameter(isLiteralPath?"LiteralPath":"Path",path);
292+
293+
Collection<PathInfo>output=ps.Invoke<PathInfo>();
294+
295+
returnoutput;
296+
}
297+
}
298+
#endif
178299
}

‎src/System.Management.Automation/resources/TabCompletionStrings.resx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,4 +353,25 @@ using namespace &lt;AliasName&gt; = &lt;.NET-namespace&gt;</value>
353353

354354
using type&lt;AliasName&gt; =&lt;.NET-type&gt;</value>
355355
</data>
356+
<dataname="RegistryStringToolTip"xml:space="preserve">
357+
<value>A normal string.</value>
358+
</data>
359+
<dataname="RegistryExpandStringToolTip"xml:space="preserve">
360+
<value>A string that contains unexpanded references to environment variables that are expanded when the value is retrieved.</value>
361+
</data>
362+
<dataname="RegistryBinaryToolTip"xml:space="preserve">
363+
<value>Binary data in any form.</value>
364+
</data>
365+
<dataname="RegistryDWordToolTip"xml:space="preserve">
366+
<value>A 32-bit binary number.</value>
367+
</data>
368+
<dataname="RegistryMultiStringToolTip"xml:space="preserve">
369+
<value>An array of strings.</value>
370+
</data>
371+
<dataname="RegistryQWordToolTip"xml:space="preserve">
372+
<value>A 64-bit binary number.</value>
373+
</data>
374+
<dataname="RegistryUnknownToolTip"xml:space="preserve">
375+
<value>An unsupported registry data type.</value>
376+
</data>
356377
</root>

‎test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,87 @@ ConstructorTestClass(int i, bool b)
931931
}
932932
}
933933

934+
Context'New-ItemProperty -PropertyType parameter completion' {
935+
BeforeAll {
936+
if ($IsWindows) {
937+
$allRegistryValueKinds='String ExpandString Binary DWord MultiString QWord Unknown'
938+
$allRegistryValueKindsWithQuotes="'String' 'ExpandString' 'Binary' 'DWord' 'MultiString' 'QWord' 'Unknown'"
939+
$dwordValueKind='DWord'
940+
$qwordValueKind='QWord'
941+
$binaryValueKind='Binary'
942+
$multiStringValueKind='MultiString'
943+
$registryPath="HKCU:\test1\sub"
944+
New-Item-Path$registryPath-Force
945+
$registryLiteralPath="HKCU:\test2\*\sub"
946+
New-Item-Path$registryLiteralPath-Force
947+
$fileSystemPath="TestDrive:\test1.txt"
948+
New-Item-Path$fileSystemPath-Force
949+
$fileSystemLiteralPathDir="TestDrive:\[]"
950+
$fileSystemLiteralPath="$fileSystemLiteralPathDir\test2.txt"
951+
New-Item-Path$fileSystemLiteralPath-Force
952+
}
953+
}
954+
955+
It"Should complete Property Type for '<TextInput>'"-Skip:(!$IsWindows)-TestCases@(
956+
# -Path completions
957+
@{TextInput="New-ItemProperty -Path$registryPath -PropertyType";ExpectedPropertyTypes=$allRegistryValueKinds }
958+
@{TextInput="New-ItemProperty -Path$registryPath -PropertyType d";ExpectedPropertyTypes=$dwordValueKind }
959+
@{TextInput="New-ItemProperty -Path$registryPath -PropertyType q";ExpectedPropertyTypes=$qwordValueKind }
960+
@{TextInput="New-ItemProperty -Path$registryPath -PropertyType bin";ExpectedPropertyTypes=$binaryValueKind }
961+
@{TextInput="New-ItemProperty -Path$registryPath -PropertyType multi";ExpectedPropertyTypes=$multiStringValueKind }
962+
@{TextInput="New-ItemProperty -Path$registryPath -PropertyType invalidproptype";ExpectedPropertyTypes='' }
963+
@{TextInput="New-ItemProperty -Path$fileSystemPath -PropertyType";ExpectedPropertyTypes='' }
964+
965+
# -LiteralPath completions
966+
@{TextInput="New-ItemProperty -LiteralPath$registryLiteralPath -PropertyType";ExpectedPropertyTypes=$allRegistryValueKinds }
967+
@{TextInput="New-ItemProperty -LiteralPath$registryLiteralPath -PropertyType d";ExpectedPropertyTypes=$dwordValueKind }
968+
@{TextInput="New-ItemProperty -LiteralPath$registryLiteralPath -PropertyType q";ExpectedPropertyTypes=$qwordValueKind }
969+
@{TextInput="New-ItemProperty -LiteralPath$registryLiteralPath -PropertyType bin";ExpectedPropertyTypes=$binaryValueKind }
970+
@{TextInput="New-ItemProperty -LiteralPath$registryLiteralPath -PropertyType multi";ExpectedPropertyTypes=$multiStringValueKind }
971+
@{TextInput="New-ItemProperty -LiteralPath$registryLiteralPath -PropertyType invalidproptype";ExpectedPropertyTypes='' }
972+
@{TextInput="New-ItemProperty -LiteralPath$fileSystemLiteralPath -PropertyType";ExpectedPropertyTypes='' }
973+
974+
# All of these should return no completion since they don't specify -Path/-LiteralPath
975+
@{TextInput="New-ItemProperty -PropertyType";ExpectedPropertyTypes='' }
976+
@{TextInput="New-ItemProperty -PropertyType d";ExpectedPropertyTypes='' }
977+
@{TextInput="New-ItemProperty -PropertyType q";ExpectedPropertyTypes='' }
978+
@{TextInput="New-ItemProperty -PropertyType bin";ExpectedPropertyTypes='' }
979+
@{TextInput="New-ItemProperty -PropertyType multi";ExpectedPropertyTypes='' }
980+
@{TextInput="New-ItemProperty -PropertyType invalidproptype";ExpectedPropertyTypes='' }
981+
982+
# All of these should return completion even with quotes included
983+
@{TextInput="New-ItemProperty -Path$registryPath -PropertyType '";ExpectedPropertyTypes=$allRegistryValueKindsWithQuotes }
984+
@{TextInput="New-ItemProperty -Path$registryPath -PropertyType 'bin";ExpectedPropertyTypes="'$binaryValueKind'" }
985+
) {
986+
param($TextInput,$ExpectedPropertyTypes)
987+
$res= TabExpansion2-inputScript$TextInput-cursorColumn$TextInput.Length
988+
$completionText=$res.CompletionMatches.CompletionText
989+
$completionText-join''| Should-BeExactly$ExpectedPropertyTypes
990+
}
991+
992+
It"Test fallback to provider of current location if no path specified"-Skip:(!$IsWindows) {
993+
try {
994+
Push-Location HKCU:\
995+
$textInput="New-ItemProperty -PropertyType"
996+
$res= TabExpansion2-inputScript$textInput-cursorColumn$textInput.Length
997+
$completionText=$res.CompletionMatches.CompletionText
998+
$completionText-join''| Should-BeExactly$allRegistryValueKinds
999+
}
1000+
finally {
1001+
Pop-Location
1002+
}
1003+
}
1004+
1005+
AfterAll {
1006+
if ($IsWindows) {
1007+
Remove-Item-Path$registryPath-Force
1008+
Remove-Item-LiteralPath$registryLiteralPath-Force
1009+
Remove-Item-Path$fileSystemPath-Force
1010+
Remove-Item-LiteralPath$fileSystemLiteralPathDir-Recurse-Force
1011+
}
1012+
}
1013+
}
1014+
9341015
Context"Format cmdlet's View paramter completion" {
9351016
BeforeAll {
9361017
$viewDefinition=@'

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp