@@ -423,16 +423,34 @@ public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst fun
423
423
424
424
internal static List < CompletionResult > CompleteModuleName ( CompletionContext context , bool loadedModulesOnly , bool skipEditionCheck = false )
425
425
{
426
- var moduleName = context . WordToComplete ?? string . Empty ;
426
+ var wordToComplete = context . WordToComplete ?? string . Empty ;
427
427
var result = new List < CompletionResult > ( ) ;
428
- var quote = HandleDoubleAndSingleQuote ( ref moduleName ) ;
428
+ var quote = HandleDoubleAndSingleQuote ( ref wordToComplete ) ;
429
429
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
+ bool shortNameSearch = wordToComplete . Length > 0 && ! WildcardPattern . ContainsWildcardCharacters ( wordToComplete ) && ! wordToComplete . Contains ( '.' ) ;
434
+
435
+ if ( ! wordToComplete . EndsWith ( '*' ) )
436
+ {
437
+ wordToComplete += "*" ;
438
+ }
439
+
440
+ string [ ] moduleNames ;
441
+ WildcardPattern shortNamePattern ;
442
+ if ( shortNameSearch )
443
+ {
444
+ moduleNames = new string [ ] { wordToComplete , "*." + wordToComplete } ;
445
+ shortNamePattern = new WildcardPattern ( wordToComplete , WildcardOptions . IgnoreCase ) ;
446
+ }
447
+ else
431
448
{
432
- moduleName += "*" ;
449
+ moduleNames = new string [ ] { wordToComplete } ;
450
+ shortNamePattern = null ;
433
451
}
434
452
435
- var powershell = context . Helper . AddCommandWithPreferenceSetting ( "Get-Module" , typeof ( GetModuleCommand ) ) . AddParameter ( "Name" , moduleName ) ;
453
+ var powershell = context . Helper . AddCommandWithPreferenceSetting ( "Get-Module" , typeof ( GetModuleCommand ) ) . AddParameter ( "Name" , moduleNames ) ;
436
454
if ( ! loadedModulesOnly )
437
455
{
438
456
powershell . AddParameter ( "ListAvailable" , true ) ;
@@ -444,18 +462,26 @@ internal static List<CompletionResult> CompleteModuleName(CompletionContext cont
444
462
}
445
463
}
446
464
447
- Exception exceptionThrown ;
448
- var psObjects = context . Helper . ExecuteCurrentPowerShell ( out exceptionThrown ) ;
465
+ Collection < PSObject > psObjects = context . Helper . ExecuteCurrentPowerShell ( out _ ) ;
449
466
450
467
if ( psObjects != null )
451
8000
code>
468
{
452
- foreach ( dynamic moduleInfo in psObjects )
469
+ foreach ( PSObject item in psObjects )
453
470
{
454
- var completionText = moduleInfo . Name . ToString ( ) ;
471
+ var moduleInfo = ( PSModuleInfo ) item . BaseObject ;
472
+ var completionText = moduleInfo . Name ;
455
473
var listItemText = completionText ;
456
- var toolTip = "Description: " + moduleInfo . Description . ToString ( ) + "\r \n ModuleType: "
474
+ if ( shortNameSearch
475
+ && completionText . Contains ( '.' )
476
+ && ! shortNamePattern . IsMatch ( completionText . Substring ( completionText . LastIndexOf ( '.' ) + 1 ) )
477
+ && ! shortNamePattern . IsMatch ( completionText ) )
478
+ {
479
+ continue ;
480
+ }
481
+
482
+ var toolTip = "Description: " + moduleInfo . Description + "\r \n ModuleType: "
457
483
+ moduleInfo . ModuleType . ToString ( ) + "\r \n Path: "
458
- + moduleInfo . Path . ToString ( ) ;
484
+ + moduleInfo . Path ;
459
485
460
486
if ( CompletionRequiresQuotes ( completionText , false ) )
461
487
{
0 commit comments