Repository: microsoft/terminal
File: src/cascadia/TerminalApp/CommandPalette.cpp (C++)

Code before:
            if (parsedCommands.size() >= CommandLineHistoryLength)
            {
                // Don't load more than CommandLineHistoryLength commands
                break;
            }

            if (const auto parsedCommand = _buildCommandLineCommand(c))
            {
                parsedCommands.push_back(*parsedCommand);
            }
        }
        return single_threaded_vector(std::move(parsedCommands));
    }

    // Method Description:
    // - Update recent commands by putting the provided command as most recent.
    // Upon race condition might override an update made by another window.
    // Return Value:
    // - <none>
    void CommandPalette::_updateRecentCommands(const winrt::hstring& command)
    {
        const auto recentCommands = ApplicationState::SharedInstance().RecentCommands();
        std::vector<hstring> newRecentCommands{ std::min(recentCommands.Size(), CommandLineHistoryLength - 1) };
        recentCommands.GetMany(0, newRecentCommands);
        newRecentCommands.insert(newRecentCommands.begin(), command);
        ApplicationState::SharedInstance().RecentCommands(single_threaded_vector(std::move(newRecentCommands)));
    }
}


Review comment (bug):
Neat! This is really nice. I've came back from dinner a while ago, but haven't been able to get my changes to compile under the "AuditMode" (which is basically our linter). Something isn't working right and I get a weird compilation error, unrelated to my changes.

Just for reference - this is my variant, which should technically work (I mean it does compile in Debug/Release mode):
```cpp
void CommandPalette::_updateRecentCommands(const winrt::hstring& command)
{
    const auto recentCommands = ApplicationState::SharedInstance().RecentCommands();
    const auto copyCount = std::min(recentCommands.Size(), CommandLineHistoryLength - 1);

    std::vector<winrt::hstring> newCommands(copyCount + 1);
    til::at(newCommands, 0) = command;

    if (copyCount)
    {
#pragma warning(suppress : 26481) // Don't use pointer arithmetic. Use span instead
        recentCommands.GetMany(0, { newCommands.data() + 1, copyCount });
    }

    ApplicationState::SharedInstance().RecentCommands(std::move(recentCommands));
}
```

Code after:
            if (parsedCommands.size() >= CommandLineHistoryLength)
            {
                // Don't load more than CommandLineHistoryLength commands
                break;
            }

            if (const auto parsedCommand = _buildCommandLineCommand(c))
            {
                parsedCommands.push_back(*parsedCommand);
            }
        }
        return single_threaded_vector(std::move(parsedCommands));
    }

    // Method Description:
    // - Update recent commands by putting the provided command as most recent.
    // Upon race condition might override an update made by another window.
    // Return Value:
    // - <none>
    void CommandPalette::_updateRecentCommands(const hstring& command)
    {
        const auto recentCommands = ApplicationState::SharedInstance().RecentCommands();
        const auto countToCopy = std::min(recentCommands.Size(), CommandLineHistoryLength - 1);
        std::vector<hstring> newRecentCommands{ countToCopy + 1 };
        til::at(newRecentCommands, 0) = command;
        if (countToCopy)
        {
            recentCommands.GetMany(0, { newRecentCommands.data() + 1, countToCopy });
        }
        ApplicationState::SharedInstance().RecentCommands(single_threaded_vector(std::move(newRecentCommands)));
    }
}