Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 34 additions & 18 deletions src/System.Management.Automation/engine/GetCommandCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -886,46 +886,40 @@ private void AccumulateMatchingCommands(IEnumerable<string> commandNames)
{
if (TotalCount < 0 || count < TotalCount)
{
IEnumerable<CommandInfo> commands;
if (UseFuzzyMatching)
{
foreach (var commandScore in ModuleUtils.GetFuzzyMatchingCommands(
foreach (CommandScore commandScore in ModuleUtils.GetFuzzyMatchingCommands(
plainCommandName,
Context,
MyInvocation.CommandOrigin,
_fuzzyMatcher,
rediscoverImportedModules: true,
moduleVersionRequired: _isFullyQualifiedModuleSpecified))
{
_commandScores.Add(commandScore);
}
CommandInfo current = commandScore.Command;

commands = _commandScores.Select(static x => x.Command);
if (TryAddCommandToResults(ref current, commandScore.Score, ref count, out isDuplicate))
{
break;
}
Comment thread
KirtiRamchandani marked this conversation as resolved.
}
}
else
{
commands = ModuleUtils.GetMatchingCommands(
IEnumerable<CommandInfo> commands = ModuleUtils.GetMatchingCommands(
plainCommandName,
Context,
MyInvocation.CommandOrigin,
rediscoverImportedModules: true,
moduleVersionRequired: _isFullyQualifiedModuleSpecified,
useAbbreviationExpansion: UseAbbreviationExpansion);
}

foreach (CommandInfo command in commands)
{
// Cannot pass in "command" by ref (foreach iteration variable)
CommandInfo current = command;

if (IsCommandMatch(ref current, out isDuplicate) && (!IsCommandInResult(current)) && IsParameterMatch(current))
foreach (CommandInfo command in commands)
{
_accumulatedResults.Add(current);

// Make sure we don't exceed the TotalCount parameter
++count;
// Cannot pass in "command" by ref (foreach iteration variable)
CommandInfo current = command;

if (TotalCount >= 0 && count >= TotalCount)
if (TryAddCommandToResults(ref current, null, ref count, out isDuplicate))
{
break;
}
Expand Down Expand Up @@ -966,6 +960,28 @@ private void AccumulateMatchingCommands(IEnumerable<string> commandNames)
}
}

private bool TryAddCommandToResults(ref CommandInfo current, int? fuzzyScore, ref int currentCount, out bool isDuplicate)
{
if (IsCommandMatch(ref current, out isDuplicate) && (!IsCommandInResult(current)) && IsParameterMatch(current))
{
_accumulatedResults.Add(current);
if (fuzzyScore is int score)
{
_commandScores.Add(new CommandScore(current, score));
}

// Make sure we don't exceed the TotalCount parameter
++currentCount;

if (TotalCount >= 0 && currentCount >= TotalCount)
{
return true;
}
}

return false;
}

private bool FindCommandForName(SearchResolutionOptions options, string commandName, bool isPattern, bool emitErrors, ref int currentCount, out bool isDuplicate)
{
var searcher = new CommandSearcher(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ Describe "Get-Command Feature tests" -Tag Feature {
$cmd.Score | Should -BeLessOrEqual 3
}
}

It "Should respect CommandType when fuzzy matching" {
try {
function Invoke-ZzqFuzzyCommandTypeThing { }
Set-Alias -Name Invoke-ZzqFuzzyCommandTypeThang -Value Invoke-ZzqFuzzyCommandTypeThing -Force

$cmds = Get-Command Invoke-ZzqFuzzyCommandTypeThng -UseFuzzyMatching -CommandType Function

$cmds.Name | Should -Contain 'Invoke-ZzqFuzzyCommandTypeThing'
$cmds.Name | Should -Not -Contain 'Invoke-ZzqFuzzyCommandTypeThang'
$cmds.CommandType | Should -Not -Contain ([System.Management.Automation.CommandTypes]::Alias)
}
finally {
Remove-Item -Path Function:\Invoke-ZzqFuzzyCommandTypeThing -ErrorAction SilentlyContinue
Remove-Item -Path Alias:\Invoke-ZzqFuzzyCommandTypeThang -ErrorAction SilentlyContinue
}
}
}

Context "-UseAbbreviationExpansion tests" {
Expand Down