Repository: microsoft/terminal
File: src/cascadia/TerminalControl/ControlCore.cpp (C++)

Code before:
// The minimum delay between updating the TSF input control.
constexpr const auto TsfRedrawInterval = std::chrono::milliseconds(100);

// The minimum delay between updating the locations of regex patterns
constexpr const auto UpdatePatternLocationsInterval = std::chrono::milliseconds(500);

static constexpr std::optional<Terminal::SelectionDirection> ConvertVKeyToSelectionDirection(WORD vkey)
{
    switch (vkey)
    {
    case VK_LEFT:
    case VK_HOME:
        return Terminal::SelectionDirection::Left;
    case VK_RIGHT:
    case VK_END:
        return Terminal::SelectionDirection::Right;
    case VK_UP:
    case VK_PRIOR:
        return Terminal::SelectionDirection::Up;
    case VK_DOWN:
    case VK_NEXT:
        return Terminal::SelectionDirection::Down;
    default:
        return std::nullopt;
    }
}

static constexpr std::optional<std::tuple<Terminal::SelectionDirection, Terminal::SelectionExpansion>> ConvertKeyEventToUpdateSelectionParams(const ControlKeyStates mods, const WORD vkey)
{
    if (mods.IsShiftPressed() && !mods.IsAltPressed())
    {
        if (const auto dir{ ConvertVKeyToSelectionDirection(vkey) })
        {
            if (mods.IsCtrlPressed())
            {
                switch (vkey)
                {
                case VK_LEFT:
                case VK_RIGHT:
                    // Move by word
                    return std::make_tuple(*dir, Terminal::SelectionExpansion::Word);
                case VK_HOME:
                case VK_END:
                    // Move by buffer
                    return std::make_tuple(*dir, Terminal::SelectionExpansion::Buffer);
                default:
                    __fallthrough;
                }
            }
            else
            {

Review comment (refactor):
This abstraction is only called by one other function. If you inline it like so it'll be immediately obvious which value will be returned for any given input and it saves you a third of the code:
```cpp
using UpdateSelectionParams = std::optional<std::pair<Terminal::SelectionDirection, Terminal::SelectionExpansion>>;

static constexpr UpdateSelectionParams ConvertKeyEventToUpdateSelectionParams(const ControlKeyStates mods, const WORD vkey)
{
    if (mods.IsShiftPressed() && !mods.IsAltPressed())
    {
        if (mods.IsCtrlPressed())
        {
            switch (vkey)
            {
            case VK_LEFT:
                return UpdateSelectionParams{ std::in_place, Terminal::SelectionDirection::Left, Terminal::SelectionExpansion::Word };
            case VK_RIGHT:
                return UpdateSelectionParams{ std::in_place, Terminal::SelectionDirection::Right, Terminal::SelectionExpansion::Word };
            case VK_HOME:
                return UpdateSelectionParams{ std::in_place, Terminal::SelectionDirection::Left, Terminal::SelectionExpansion::Buffer };
            case VK_END:
                return UpdateSelectionParams{ std::in_place, Terminal::SelectionDirection::Right, Terminal::SelectionExpansion::Buffer };
            default:
                [[fallthrough]];
            }
        }
        else
        {
            switch (vkey)
            {
            case VK_HOME:
                return UpdateSelectionParams{ std::in_place, Terminal::SelectionDirection::Left, Terminal::SelectionExpansion::Viewport };
            case VK_END:
                return UpdateSelectionParams{ std::in_place, Terminal::SelectionDirection::Right, Terminal::SelectionExpansion::Viewport };
            case VK_PRIOR:
                return UpdateSelectionParams{ std::in_place, Terminal::SelectionDirection::Up, Terminal::SelectionExpansion::Viewport };
            case VK_NEXT:
                return UpdateSelectionParams{ std::in_place, Terminal::SelectionDirection::Down, Terminal::SelectionExpansion::Viewport };
            case VK_LEFT:
                return UpdateSelectionParams{ std::in_place, Terminal::SelectionDirection::Left, Terminal::SelectionExpansion::Char };
            case VK_RIGHT:
                return UpdateSelectionParams{ std::in_place, Terminal::SelectionDirection::Right, Terminal::SelectionExpansion::Char };
            case VK_UP:
                return UpdateSelectionParams{ std::in_place, Terminal::SelectionDirection::Up, Terminal::SelectionExpansion::Char };
            case VK_DOWN:
                return UpdateSelectionParams{ std::in_place, Terminal::SelectionDirection::Down, Terminal::SelectionExpansion::Char };
            default:
                [[fallthrough]];
            }
        }
    }
    return std::nullopt;
}
```

* `[[fallthrough]]`
* `std::pair` if you got exactly 2 items in a tuple (the most common case anyways)
* idk if you like the above more, but I prefer it (it's easier to understand IMO)
* I'd suggest making this a private member function to save yourself from writing `Terminal::` again and again.

Code after:
// The minimum delay between updating the TSF input control.
constexpr const auto TsfRedrawInterval = std::chrono::milliseconds(100);

// The minimum delay between updating the locations of regex patterns
constexpr const auto UpdatePatternLocationsInterval = std::chrono::milliseconds(500);

namespace winrt::Microsoft::Terminal::Control::implementation
{
    // Helper static function to ensure that all ambiguous-width glyphs are reported as narrow.
    // See microsoft/terminal#2066 for more info.
    static bool _IsGlyphWideForceNarrowFallback(const std::wstring_view /* glyph */)
    {
        return false; // glyph is not wide.
    }

    static bool _EnsureStaticInitialization()
    {
        // use C++11 magic statics to make sure we only do this once.
        static bool initialized = []() {
            // *** THIS IS A SINGLETON ***
            SetGlyphWidthFallback(_IsGlyphWideForceNarrowFallback);

            return true;
        }();
        return initialized;
    }

    ControlCore::ControlCore(IControlSettings settings,
                             TerminalConnection::ITerminalConnection connection) :
        _connection{ connection },
        _settings{ settings },
        _desiredFont{ DEFAULT_FONT_FACE, 0, DEFAULT_FONT_WEIGHT, { 0, DEFAULT_FONT_SIZE }, CP_UTF8 },
        _actualFont{ DEFAULT_FONT_FACE, 0, DEFAULT_FONT_WEIGHT, { 0, DEFAULT_FONT_SIZE }, CP_UTF8, false }
    {
        _EnsureStaticInitialization();

        _terminal = std::make_unique<::Microsoft::Terminal::Core::Terminal>();

        // Subscribe to the connection's disconnected event and call our connection closed handlers.
        _connectionStateChangedRevoker = _connection.StateChanged(winrt::auto_revoke, [this](auto&& /*s*/, auto&& /*v*/) {
            _ConnectionStateChangedHandlers(*this, nullptr);
        });

        // This event is explicitly revoked in the destructor: does not need weak_ref
        _connectionOutputEventToken = _connection.TerminalOutput({ this, &ControlCore::_connectionOutputHandler });

        _terminal->SetWriteInputCallback([this](std::wstring& wstr) {
            _sendInputToConnection(wstr);
        });

        // GH#8969: pre-seed working directory to prevent potential races