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

Commit8cdb728

Browse files
authored
Add completion of modules by their shortname (#20330)
1 parentb74e708 commit8cdb728

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

‎src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -423,16 +423,34 @@ public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst fun
423423

424424
internalstaticList<CompletionResult>CompleteModuleName(CompletionContextcontext,boolloadedModulesOnly,boolskipEditionCheck=false)
425425
{
426-
varmoduleName=context.WordToComplete??string.Empty;
426+
varwordToComplete=context.WordToComplete??string.Empty;
427427
varresult=newList<CompletionResult>();
428-
varquote=HandleDoubleAndSingleQuote(refmoduleName);
428+
varquote=HandleDoubleAndSingleQuote(refwordToComplete);
429429

430-
if(!moduleName.EndsWith('*'))
430+
// Indicates if we should search for modules where the last part of the name matches the input text
431+
// eg: Host<Tab> finds Microsoft.PowerShell.Host
432+
// If the user has entered a manual wildcard, or a module name that contains a "." we assume they only want results that matches the input exactly.
433+
boolshortNameSearch=wordToComplete.Length>0&&!WildcardPattern.ContainsWildcardCharacters(wordToComplete)&&!wordToComplete.Contains('.');
434+
435+
if(!wordToComplete.EndsWith('*'))
436+
{
437+
wordToComplete+="*";
438+
}
439+
440+
string[]moduleNames;
441+
WildcardPatternshortNamePattern;
442+
if(shortNameSearch)
443+
{
444+
moduleNames=newstring[]{wordToComplete,"*."+wordToComplete};
445+
shortNamePattern=newWildcardPattern(wordToComplete,WildcardOptions.IgnoreCase);
446+
}
447+
else
431448
{
432-
moduleName+="*";
449+
moduleNames=newstring[]{wordToComplete};
450+
shortNamePattern=null;
433451
}
434452

435-
varpowershell=context.Helper.AddCommandWithPreferenceSetting("Get-Module",typeof(GetModuleCommand)).AddParameter("Name",moduleName);
453+
varpowershell=context.Helper.AddCommandWithPreferenceSetting("Get-Module",typeof(GetModuleCommand)).AddParameter("Name",moduleNames);
436454
if(!loadedModulesOnly)
437455
{
438456
powershell.AddParameter("ListAvailable",true);
@@ -444,18 +462,26 @@ internal static List<CompletionResult> CompleteModuleName(CompletionContext cont
444462
}
445463
}
446464

447-
ExceptionexceptionThrown;
448-
varpsObjects=context.Helper.ExecuteCurrentPowerShell(outexceptionThrown);
465+
Collection<PSObject>psObjects=context.Helper.ExecuteCurrentPowerShell(out_);
449466

450467
if(psObjects!=null)
451468
{
452-
foreach(dynamicmoduleInfoinpsObjects)
469+
foreach(PSObjectiteminpsObjects)
453470
{
454-
varcompletionText=moduleInfo.Name.ToString();
471+
varmoduleInfo=(PSModuleInfo)item.BaseObject;
472+
varcompletionText=moduleInfo.Name;
455473
varlistItemText=completionText;
456-
vartoolTip="Description: "+moduleInfo.Description.ToString()+"\r\nModuleType: "
474+
if(shortNameSearch
475+
&&completionText.Contains('.')
476+
&&!shortNamePattern.IsMatch(completionText.Substring(completionText.LastIndexOf('.')+1))
477+
&&!shortNamePattern.IsMatch(completionText))
478+
{
479+
continue;
480+
}
481+
482+
vartoolTip="Description: "+moduleInfo.Description+"\r\nModuleType: "
457483
+moduleInfo.ModuleType.ToString()+"\r\nPath: "
458-
+moduleInfo.Path.ToString();
484+
+moduleInfo.Path;
459485

460486
if(CompletionRequiresQuotes(completionText,false))
461487
{

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ Describe "TabCompletion" -Tags CI {
2323
$res| Should-BeExactly'Test-AbbreviatedFunctionExpansion'
2424
}
2525

26+
It'Should complete module by shortname' {
27+
$res= TabExpansion2-inputScript'Get-Module -ListAvailable -Name Host'
28+
$res.CompletionMatches[0].CompletionText| Should-BeExactly'Microsoft.PowerShell.Host'
29+
}
30+
2631
It'Should complete native exe'-Skip:(!$IsWindows) {
2732
$res= TabExpansion2-inputScript'notep'-cursorColumn'notep'.Length
2833
$res.CompletionMatches[0].CompletionText| Should-BeExactly'notepad.exe'

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp