Repository: PowerShell/PowerShell
File: src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs (C#)

Code before:
                    }
                    else
                    {
                        index = 0;
                    }

                    for (; index < commandInfoArray.Length; index++)
                    {
                        CommandInfo commandInfo = commandInfoArray[index];
                        if (commandInfo.CommandType == CommandTypes.Application)
                        {
                            results.Add(GetCommandNameCompletionResult(commandInfo.Definition, commandInfo, addAmpersandIfNecessary, quote));
                        }
                        else if (!string.IsNullOrEmpty(commandInfo.ModuleName) && modulesWithCommand.Add(commandInfo.ModuleName))
                        {
                            var name = commandInfo.ModuleName + "\\" + commandInfo.Name;
                            results.Add(GetCommandNameCompletionResult(name, commandInfo, addAmpersandIfNecessary, quote));
                        }
                    }
                }
                else
                {
                    // The first command might be an un-prefixed commandInfo that we get by importing a module with the -Prefix parameter,
                    // in that case, we should add the module name qualification because if the module is not in the module path, calling
                    // 'Get-Foo' directly doesn't work
                    string completionName = keyValuePair.Key;
                    if (!includeModulePrefix)
                    {
                        var commandInfo = keyValuePair.Value as CommandInfo;
                        if (commandInfo != null && !string.IsNullOrEmpty(commandInfo.Prefix))
                        {
                            Diagnostics.Assert(!string.IsNullOrEmpty(commandInfo.ModuleName), "the module name should exist if commandInfo.Prefix is not an empty string");
                            if (!ModuleCmdletBase.IsPrefixedCommand(commandInfo))
                            {
                                completionName = commandInfo.ModuleName + "\\" + completionName;
                            }
                        }
                    }

                    results.Add(GetCommandNameCompletionResult(completionName, keyValuePair.Value, addAmpersandIfNecessary, quote));
                }
            }

            if (endResults != null && endResults.Count > 0)
            {
                results.AddRange(endResults);
            }

            return results;
        }


Review comment (suggestion):
Hmm, you are not using `endResults` anymore, meaning that you are not putting all the module-name-prefixed results at the end. Can you confirm if that expected and why? Also, if it's expected, you should remove the `endResults` declaration above and the use of it below.

Code after:
                    int moduleCount = modulesWithCommand.Count;
                    modulesWithCommand.Clear();
                    int index;
                    if (commandInfoList[0].CommandType == CommandTypes.Application
                        || importedModules.Count == 1
                        || moduleCount < 2)
                    {
                        // We can use the short name for this command because there's no ambiguity about which command it resolves to.
                        // If the first element is an application then we know there's no conflicting commands/aliases (because of the command precedence).
                        // If there's just 1 module imported then the short name refers to that module (and it will be the first element in the list)
                        // If there's less than 2 unique modules exporting that command then we can use the short name because it can only refer to that module.
                        index = 1;
                        results.Add(GetCommandNameCompletionResult(keyValuePair.Key, commandInfoList[0], addAmpersandIfNecessary, quote));
                        modulesWithCommand.Add(commandInfoList[0].ModuleName);
                    }
                    else
                    {
                        index = 0;
                    }

                    for (; index < commandInfoList.Count; index++)
                    {
                        CommandInfo commandInfo = commandInfoList[index];
                        if (commandInfo.CommandType == CommandTypes.Application)
                        {
                            results.Add(GetCommandNameCompletionResult(commandInfo.Definition, commandInfo, addAmpersandIfNecessary, quote));
                        }
                        else if (!string.IsNullOrEmpty(commandInfo.ModuleName) && modulesWithCommand.Add(commandInfo.ModuleName))
                        {
                            var name = commandInfo.ModuleName + "\\" + commandInfo.Name;
                            results.Add(GetCommandNameCompletionResult(name, commandInfo, addAmpersandIfNecessary, quote));
                        }
                    }
                }
                else
                {
                    // The first command might be an un-prefixed commandInfo that we get by importing a module with the -Prefix parameter,
                    // in that case, we should add the module name qualification because if the module is not in the module path, calling
                    // 'Get-Foo' directly doesn't work
                    string completionName = keyValuePair.Key;
                    if (!includeModulePrefix)
                    {
                        var commandInfo = keyValuePair.Value as CommandInfo;
                        if (commandInfo != null && !string.IsNullOrEmpty(commandInfo.Prefix))
                        {
                            Diagnostics.Assert(!string.IsNullOrEmpty(commandInfo.ModuleName), "the module name should exist if commandInfo.Prefix is not an empty string");
                            if (!ModuleCmdletBase.IsPrefixedCommand(commandInfo))
                            {
                                completionName = commandInfo.ModuleName + "\\" + completionName;
                            }
                        }