@@ -24,29 +24,20 @@ internal class CommandSearcher : IEnumerable<CommandInfo>, IEnumerator<CommandIn
24
24
/// Constructs a command searching enumerator that resolves the location
25
25
/// to a command using a standard algorithm.
26
26
/// </summary>
27
- /// <param name="commandName">
28
- /// The name of the command to look for.
29
- /// </param>
30
- /// <param name="options">
31
- /// Determines which types of commands glob resolution of the name will take place on.
32
- /// </param>
33
- /// <param name="commandTypes">
34
- /// The types of commands to look for.
35
- /// </param>
36
- /// <param name="context">
37
- /// The execution context for this engine instance...
38
- /// </param>
39
- /// <exception cref="ArgumentNullException">
40
- /// If <paramref name="context"/> is null.
41
- /// </exception>
42
- /// <exception cref="PSArgumentException">
43
- /// If <paramref name="commandName"/> is null or empty.
44
- /// </exception>
27
+ /// <param name="commandName">The name of the command to look for.</param>
28
+ /// <param name="options">Determines which types of commands glob resolution of the name will take place on.</param>
29
+ /// <param name="commandTypes">The types of commands to look for.</param>
30
+ /// <param name="context">The execution context for this engine instance.</param>
31
+ /// <param name="fuzzyMatcher">The fuzzy matcher to use for fuzzy searching.</param>
32
+ ///
33
+ /// <exception cref="ArgumentNullException">If <paramref name="context"/> is null.</exception>
34
+ /// <exception cref="PSArgumentException">If <paramref name="commandName"/> is null or empty.</exception>
45
35
internal CommandSearcher (
46
36
string commandName ,
47
37
SearchResolutionOptions options ,
48
38
CommandTypes commandTypes ,
49
- ExecutionContext context )
39
+ ExecutionContext context ,
40
+ FuzzyMatcher ? fuzzyMatcher = null )
50
41
{
51
42
Diagnostics . Assert ( context != null , "caller to verify context is not null" ) ;
52
43
Diagnostics . Assert ( ! string . IsNullOrEmpty ( commandName ) , "caller to verify commandName is valid" ) ;
@@ -55,6 +46,7 @@ internal CommandSearcher(
55
46
_context = context ;
56
47
_commandResolutionOptions = options ;
57
48
_commandTypes = commandTypes ;
49
+ _fuzzyMatcher = fuzzyMatcher ;
58
50
59
51
// Initialize the enumerators
60
52
this . Reset ( ) ;
@@ -705,8 +697,7 @@ private static bool checkPath(string path, string commandName)
705
697
foreach ( KeyValuePair < string , AliasInfo > aliasEntry in _context . EngineSessionState . GetAliasTable ( ) )
706
698
{
707
699
if ( aliasMatcher . IsMatch ( aliasEntry . Key ) ||
708
- ( _commandResolutionOptions . HasFlag ( SearchResolutionOptions . FuzzyMatch ) &&
709
- FuzzyMatcher . IsFuzzyMatch ( aliasEntry . Key , _commandName ) ) )
700
+ ( _fuzzyMatcher is not null && _fuzzyMatcher . IsFuzzyMatch ( aliasEntry . Key , _commandName ) ) )
710
701
{
711
702
matchingAliases . Add ( aliasEntry . Value ) ;
712
703
}
@@ -785,8 +776,7 @@ private static bool checkPath(string path, string commandName)
785
776
foreach ( ( string functionName , FunctionInfo functionInfo ) in _context . EngineSessionState . GetFunctionTable ( ) )
786
777
{
787
778
if ( functionMatcher . IsMatch ( functionName ) ||
788
- ( _commandResolutionOptions . HasFlag ( SearchResolutionOptions . FuzzyMatch ) &&
789
- FuzzyMatcher . IsFuzzyMatch ( functionName , _commandName ) ) )
779
+ ( _fuzzyMatcher is not null && _fuzzyMatcher . IsFuzzyMatch ( functionName , _commandName ) ) )
790
780
{
791
781
matchingFunction . Add ( functionInfo ) ;
792
782
}
@@ -1018,10 +1008,8 @@ private static bool ShouldSkipCommandResolutionForConstrainedLanguage(CommandInf
1018
1008
{
1019
1009
foreach ( CmdletInfo cmdlet in cmdletList )
1020
1010
{
1021
- if ( cmdletMatcher != null &&
1022
- cmdletMatcher . IsMatch ( cmdlet . Name ) ||
1023
- ( _commandResolutionOptions . HasFlag ( SearchResolutionOptions . FuzzyMatch ) &&
1024
- FuzzyMatcher . IsFuzzyMatch ( cmdlet . Name , _commandName ) ) )
1011
+ if ( ( cmdletMatcher is not null && cmdletMatcher . IsMatch ( cmdlet . Name ) ) ||
1012
+ ( _fuzzyMatcher is not null && _fuzzyMatcher . IsFuzzyMatch ( cmdlet . Name , _commandName ) ) )
1025
1013
{
1026
1014
if ( string . IsNullOrEmpty ( moduleName ) || moduleName . Equals ( cmdlet . ModuleName , StringComparison . OrdinalIgnoreCase ) )
1027
1015
{
@@ -1496,6 +1484,11 @@ private static CanDoPathLookupResult CanDoPathLookup(string possiblePath)
1496
1484
/// </summary>
1497
1485
private readonly ExecutionContext _context ;
1498
1486
1487
+ /// <summary>
1488
+ /// The fuzzy matcher to use for fuzzy searching.
1489
+ /// </summary>
1490
+ private readonly FuzzyMatcher ? _fuzzyMatcher ;
1491
+
1499
1492
/// <summary>
1500
1493
/// A routine to initialize the path searcher...
1501
1494
/// </summary>
@@ -1528,7 +1521,7 @@ private void setupPathSearcher()
1528
1521
_context . CommandDiscovery . GetLookupDirectoryPaths ( ) ,
1529
1522
_context ,
1530
1523
acceptableCommandNames : null ,
1531
- useFuzzyMatch : _commandResolutionOptions . HasFlag ( SearchResolutionOptions . FuzzyMatch ) ) ;
1524
+ _fuzzyMatcher ) ;
1532
1525
}
1533
1526
else
1534
1527
{
@@ -1544,7 +1537,7 @@ private void setupPathSearcher()
1544
1537
_context . CommandDiscovery . GetLookupDirectoryPaths ( ) ,
1545
1538
_context ,
1546
1539
ConstructSearchPatternsFromName ( _commandName , commandDiscovery : true ) ,
1547
- useFuzzyMatch : false ) ;
1540
+ fuzzyMatcher : null ) ;
1548
1541
}
1549
1542
else if ( _canDoPathLookupResult == CanDoPathLookupResult . PathIsRooted )
1550
1543
{
@@ -1568,7 +1561,7 @@ private void setupPathSearcher()
1568
1561
directoryCollection ,
1569
1562
_context ,
1570
1563
ConstructSearchPatternsFromName ( fileName , commandDiscovery : true ) ,
1571
- useFuzzyMatch : false ) ;
1564
+ fuzzyMatcher : null ) ;
1572
1565
}
1573
1566
else
1574
1567
{
@@ -1608,7 +1601,7 @@ private void setupPathSearcher()
1608
1601
directoryCollection ,
1609
1602
_context ,
1610
1603
ConstructSearchPatternsFromName ( fileName , commandDiscovery : true ) ,
1611
- useFuzzyMatch : false ) ;
1604
+ fuzzyMatcher : null ) ;
1612
1605
}
1613
1606
else
1614
1607
{
@@ -1727,17 +1720,14 @@ internal enum SearchResolutionOptions
1727
1720
CommandNameIsPattern = 0x04 ,
1728
1721
SearchAllScopes = 0x08 ,
1729
1722
1730
- /// <summary>Use fuzzy matching.</summary>
1731
- FuzzyMatch = 0x10 ,
1732
-
1733
1723
/// <summary>
1734
1724
/// Enable searching for cmdlets/functions by abbreviation expansion.
1735
1725
/// </summary>
1736
- UseAbbreviationExpansion = 0x20 ,
1726
+ UseAbbreviationExpansion = 0x10 ,
1737
1727
1738
1728
/// <summary>
1739
1729
/// Enable resolving wildcard in paths.
1740
1730
/// </summary>
1741
- ResolveLiteralThenPathPatterns = 0x40
1731
+ ResolveLiteralThenPathPatterns = 0x20
1742
1732
}
1743
1733
}
0 commit comments