@@ -24,29 +24,20 @@ internal class CommandSearcher : IEnumerable<CommandInfo>, IEnumerator<CommandIn
2424 /// Constructs a command searching enumerator that resolves the location
2525 /// to a command using a standard algorithm.
2626 /// </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>
4535 internal CommandSearcher (
4636 string commandName ,
4737 SearchResolutionOptions options ,
4838 CommandTypes commandTypes ,
49- ExecutionContext context )
39+ ExecutionContext context ,
40+ FuzzyMatcher ? fuzzyMatcher = null )
5041 {
5142 Diagnostics . Assert ( context != null , "caller to verify context is not null" ) ;
5243 Diagnostics . Assert ( ! string . IsNullOrEmpty ( commandName ) , "caller to verify commandName is valid" ) ;
@@ -55,6 +46,7 @@ internal CommandSearcher(
5546 _context = context ;
5647 _commandResolutionOptions = options ;
5748 _commandTypes = commandTypes ;
49+ _fuzzyMatcher = fuzzyMatcher ;
5850
5951 // Initialize the enumerators
6052 this . Reset ( ) ;
@@ -705,8 +697,7 @@ private static bool checkPath(string path, string commandName)
705697 foreach ( KeyValuePair < string , AliasInfo > aliasEntry in _context . EngineSessionState . GetAliasTable ( ) )
706698 {
707699 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 ) ) )
710701 {
711702 matchingAliases . Add ( aliasEntry . Value ) ;
712703 }
@@ -785,8 +776,7 @@ private static bool checkPath(string path, string commandName)
785776 foreach ( ( string functionName , FunctionInfo functionInfo ) in _context . EngineSessionState . GetFunctionTable ( ) )
786777 {
787778 if ( functionMatcher . IsMatch ( functionName ) ||
788- ( _commandResolutionOptions . HasFlag ( SearchResolutionOptions . FuzzyMatch ) &&
789- FuzzyMatcher . IsFuzzyMatch ( functionName , _commandName ) ) )
779+ ( _fuzzyMatcher is not null && _fuzzyMatcher . IsFuzzyMatch ( functionName , _commandName ) ) )
790780 {
791781 matchingFunction . Add ( functionInfo ) ;
792782 }
@@ -1018,10 +1008,8 @@ private static bool ShouldSkipCommandResolutionForConstrainedLanguage(CommandInf
10181008 {
10191009 foreach ( CmdletInfo cmdlet in cmdletList )
10201010 {
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 ) ) )
10251013 {
10261014 if ( string . IsNullOrEmpty ( moduleName ) || moduleName . Equals ( cmdlet . ModuleName , StringComparison . OrdinalIgnoreCase ) )
10271015 {
@@ -1496,6 +1484,11 @@ private static CanDoPathLookupResult CanDoPathLookup(string possiblePath)
14961484 /// </summary>
14971485 private readonly ExecutionContext _context ;
14981486
1487+ /// <summary>
1488+ /// The fuzzy matcher to use for fuzzy searching.
1489+ /// </summary>
1490+ private readonly FuzzyMatcher ? _fuzzyMatcher ;
1491+
14991492 /// <summary>
15001493 /// A routine to initialize the path searcher...
15011494 /// </summary>
@@ -1528,7 +1521,7 @@ private void setupPathSearcher()
15281521 _context . CommandDiscovery . GetLookupDirectoryPaths ( ) ,
15291522 _context ,
15301523 acceptableCommandNames : null ,
1531- useFuzzyMatch : _commandResolutionOptions . HasFlag ( SearchResolutionOptions . FuzzyMatch ) ) ;
1524+ _fuzzyMatcher ) ;
15321525 }
15331526 else
15341527 {
@@ -1544,7 +1537,7 @@ private void setupPathSearcher()
15441537 _context . CommandDiscovery . GetLookupDirectoryPaths ( ) ,
15451538 _context ,
15461539 ConstructSearchPatternsFromName ( _commandName , commandDiscovery : true ) ,
1547- useFuzzyMatch : false ) ;
1540+ fuzzyMatcher : null ) ;
15481541 }
15491542 else if ( _canDoPathLookupResult == CanDoPathLookupResult . PathIsRooted )
15501543 {
@@ -1568,7 +1561,7 @@ private void setupPathSearcher()
15681561 directoryCollection ,
15691562 _context ,
15701563 ConstructSearchPatternsFromName ( fileName , commandDiscovery : true ) ,
1571- useFuzzyMatch : false ) ;
1564+ fuzzyMatcher : null ) ;
15721565 }
15731566 else
15741567 {
@@ -1608,7 +1601,7 @@ private void setupPathSearcher()
16081601 directoryCollection ,
16091602 _context ,
16101603 ConstructSearchPatternsFromName ( fileName , commandDiscovery : true ) ,
1611- useFuzzyMatch : false ) ;
1604+ fuzzyMatcher : null ) ;
16121605 }
16131606 else
16141607 {
@@ -1727,17 +1720,14 @@ internal enum SearchResolutionOptions
17271720 CommandNameIsPattern = 0x04 ,
17281721 SearchAllScopes = 0x08 ,
17291722
1730- /// <summary>Use fuzzy matching.</summary>
1731- FuzzyMatch = 0x10 ,
1732-
17331723 /// <summary>
17341724 /// Enable searching for cmdlets/functions by abbreviation expansion.
17351725 /// </summary>
1736- UseAbbreviationExpansion = 0x20 ,
1726+ UseAbbreviationExpansion = 0x10 ,
17371727
17381728 /// <summary>
17391729 /// Enable resolving wildcard in paths.
17401730 /// </summary>
1741- ResolveLiteralThenPathPatterns = 0x40
1731+ ResolveLiteralThenPathPatterns = 0x20
17421732 }
17431733}
0 commit comments