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

Code before:

        #endregion
    }

    /// <summary>
    /// Provides argument completion for Noun parameter. 
    /// </summary>
    public class NounArgumentCompleter : IArgumentCompleter
    {
        /// </summary>
        /// Returns completion results for Noun parameter.
        /// </summary>
        /// <param name="commandName">The command name.</param>
        /// <param name="parameterName">The parameter name.</param>
        /// <param name="wordToComplete">The word to complete.</param>
        /// <param name="commandAst">The command AST.</param>
        /// <param name="fakeBoundParameters">The fake bound parameters.</param>
        /// <returns>List of completion results.</returns>
        public IEnumerable<CompletionResult> CompleteArgument(
            string commandName,
            string parameterName,
            string wordToComplete,
            CommandAst commandAst,
            IDictionary fakeBoundParameters) => CompletionCompleters.GetMatchingResults(
                wordToComplete,
                possibleCompletionValues: GetCommandNouns(fakeBoundParameters).Order());

        /// <summary>
        /// Get command nouns using Get-Command.
        /// </summary>
        /// <param name="fakeBoundParameters">The fake bound parameters.</param>
        /// <returns>List of command nouns.</returns>
        private static HashSet<string> GetCommandNouns(IDictionary fakeBoundParameters)
        {
            using var ps = System.Management.Automation.PowerShell.Create(RunspaceMode.CurrentRunspace);

            ps.AddCommand("Get-Command");

            // Completion: Get-Command -Module <module> -Noun <wordToComplete>
            if (fakeBoundParameters.Contains("Module"))
            {
                ps.AddParameter("Module", fakeBoundParameters["Module"]);
            }

            // Completion: Get-Command -Verb <verb> -Noun <wordToComplete>
            if (fakeBoundParameters.Contains("Verb"))
            {
                ps.AddParameter("Verb", fakeBoundParameters["Verb"]);
            }

            Collection<CommandInfo> commands = ps.Invoke<CommandInfo>();

Review comment (suggestion):
I do wonder if the overhead from `Order()` for LINQ is necessary. I brought this over from the previous code.

Probably too late to change that now 😄.

Code after:

        #endregion
    }

    /// <summary>
    /// Provides argument completion for Noun parameter. 
    /// </summary>
    public class NounArgumentCompleter : IArgumentCompleter
    {
        /// </summary>
        /// Returns completion results for Noun parameter.
        /// </summary>
        /// <param name="commandName">The command name.</param>
        /// <param name="parameterName">The parameter name.</param>
        /// <param name="wordToComplete">The word to complete.</param>
        /// <param name="commandAst">The command AST.</param>
        /// <param name="fakeBoundParameters">The fake bound parameters.</param>
        /// <returns>List of completion results.</returns>
        public IEnumerable<CompletionResult> CompleteArgument(
            string commandName,
            string parameterName,
            string wordToComplete,
            CommandAst commandAst,
            IDictionary fakeBoundParameters) => CompletionCompleters.GetMatchingResults(
                wordToComplete,
                possibleCompletionValues: GetCommandNouns(fakeBoundParameters));

        /// <summary>
        /// Get sorted set of command nouns using Get-Command.
        /// </summary>
        /// <param name="fakeBoundParameters">The fake bound parameters.</param>
        /// <returns>Sorted set of command nouns.</returns>
        private static SortedSet<string> GetCommandNouns(IDictionary fakeBoundParameters)
        {
            Collection<CommandInfo> commands = CompletionCompleters.GetCommandInfo(fakeBoundParameters, "Module", "Verb");
            SortedSet<string> nouns = new(StringComparer.OrdinalIgnoreCase);

            foreach (CommandInfo command in commands)
            {
                string commandName = command.Name;
                int dashIndex = commandName.IndexOf('-');
                if (dashIndex != -1)
                {
                    string noun = commandName.Substring(dashIndex + 1);
                    nouns.Add(noun);
                }
            }

            return nouns;
        }
    }