ronantakizawa/codereview-bench huggingface dataset:
Repository: microsoft/terminal
File: src/host/srvinit.cpp (C++)

Code before:
    {
        settings.SetLaunchFaceName(settings.GetFaceName());
    }

    // Allocate console will read the global ServiceLocator::LocateGlobals().getConsoleInformation
    // for the settings we just set.
    auto Status = CONSOLE_INFORMATION::AllocateConsole({ Title, TitleLength / sizeof(wchar_t) });
    if (!NT_SUCCESS(Status))
    {
        return Status;
    }

    return STATUS_SUCCESS;
}

[[nodiscard]] NTSTATUS RemoveConsole(_In_ ConsoleProcessHandle* ProcessData)
{
    auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
    LockConsole();
    auto Status = STATUS_SUCCESS;

    CommandHistory::s_Free((HANDLE)ProcessData);

    const auto fRecomputeOwner = ProcessData->fRootProcess;
    gci.ProcessHandleList.FreeProcessData(ProcessData);

    if (fRecomputeOwner)
    {
        if (const auto pWindow = ServiceLocator::LocateConsoleWindow())
        {
            pWindow->SetOwner();
        }
        if (gci.IsInVtIoMode())
        {
            gci.GetVtIo()->HandleRootClientRemoved();
        }
    }

    UnlockConsole();

    return Status;
}

DWORD WINAPI ConsoleIoThread(LPVOID lpParameter);

void ConsoleCheckDebug()
{
#ifdef DBG
    wil::unique_hkey hCurrentUser;
    wil::unique_hkey hConsole;
    auto status = RegistrySerialization::s_OpenConsoleKey(&hCurrentUser, &hConsole);

Review comment (suggestion):
Just for my own enlightenment: when there are two processes . . .

```
A (root)
B
```

and one exits (`A`):

```
B
```

do we reassign `B` to be the root?

```
B (root)
```


Code after:
        settings.SetLaunchFaceName(settings.GetFaceName());
    }

    // Allocate console will read the global ServiceLocator::LocateGlobals().getConsoleInformation
    // for the settings we just set.
    auto Status = CONSOLE_INFORMATION::AllocateConsole({ Title, TitleLength / sizeof(wchar_t) });
    if (!NT_SUCCESS(Status))
    {
        return Status;
    }

    return STATUS_SUCCESS;
}

[[nodiscard]] NTSTATUS RemoveConsole(_In_ ConsoleProcessHandle* ProcessData)
{
    auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
    LockConsole();
    auto Status = STATUS_SUCCESS;

    CommandHistory::s_Free((HANDLE)ProcessData);

    const auto fRecomputeOwner = ProcessData->fRootProcess;
    gci.ProcessHandleList.FreeProcessData(ProcessData);

    if (fRecomputeOwner)
    {
        auto pWindow = ServiceLocator::LocateConsoleWindow();
        if (pWindow != nullptr)
        {
            pWindow->SetOwner();
        }
    }

    UnlockConsole();

    return Status;
}

DWORD WINAPI ConsoleIoThread(LPVOID lpParameter);

void ConsoleCheckDebug()
{
#ifdef DBG
    wil::unique_hkey hCurrentUser;
    wil::unique_hkey hConsole;
    auto status = RegistrySerialization::s_OpenConsoleKey(&hCurrentUser, &hConsole);

    if (NT_SUCCESS(status))
    {
        DWORD dwData = 0;
Repository: microsoft/terminal
File: src/cascadia/TerminalSettingsEditor/MainPage.cpp (C++)

Code before:
        const auto newSelectedItem{ menuItems.GetAt(index < menuItems.Size() - 1 ? index : index - 1) };
        SettingsNav().SelectedItem(newSelectedItem);
        const auto newTag = newSelectedItem.as<MUX::Controls::NavigationViewItem>().Tag();
        if (const auto profileViewModel = newTag.try_as<ProfileViewModel>())
        {
            _Navigate(*profileViewModel, BreadcrumbSubPage::None);
        }
        else
        {
            _Navigate(newTag.as<hstring>(), BreadcrumbSubPage::None);
        }
    }

    IObservableVector<IInspectable> MainPage::Breadcrumbs() noexcept
    {
        return _breadcrumbs;
    }

    winrt::Windows::UI::Xaml::Media::Brush MainPage::BackgroundBrush()
    {
        return SettingsNav().Background();
    }

    // If the theme asks for Mica, then drop out our background, so that we
    // can have mica too.
    void MainPage::_updateBackgroundForMica()

    {
        const auto& theme = _settingsSource.GlobalSettings().CurrentTheme();

        auto bgKey = (theme.Window() != nullptr && theme.Window().UseMica()) ?
                         L"SettingsPageMicaBackground" :
                         L"SettingsPageBackground";

        if (auto bgColor = Resources().TryLookup(winrt::box_value(bgKey)))
        {
            SettingsNav().Background(winrt::WUX::Media::SolidColorBrush(winrt::unbox_value<Windows::UI::Color>(bgColor)));
        }
    }

}


Review comment (suggestion):
```suggestion
    void MainPage::_UpdateBackgroundForMica()
```
nit: all the other ones start capitalized

Code after:
        const auto newSelectedItem{ menuItems.GetAt(index < menuItems.Size() - 1 ? index : index - 1) };
        SettingsNav().SelectedItem(newSelectedItem);
        const auto newTag = newSelectedItem.as<MUX::Controls::NavigationViewItem>().Tag();
        if (const auto profileViewModel = newTag.try_as<ProfileViewModel>())
        {
            _Navigate(*profileViewModel, BreadcrumbSubPage::None);
        }
        else
        {
            _Navigate(newTag.as<hstring>(), BreadcrumbSubPage::None);
        }
    }

    IObservableVector<IInspectable> MainPage::Breadcrumbs() noexcept
    {
        return _breadcrumbs;
    }

    winrt::Windows::UI::Xaml::Media::Brush MainPage::BackgroundBrush()
    {
        return SettingsNav().Background();
    }

    // If the theme asks for Mica, then drop out our background, so that we
    // can have mica too.
    void MainPage::_UpdateBackgroundForMica()

    {
        const auto& theme = _settingsSource.GlobalSettings().CurrentTheme();

        const auto bgKey = (theme.Window() != nullptr && theme.Window().UseMica()) ?
                               L"SettingsPageMicaBackground" :
                               L"SettingsPageBackground";

        if (const auto bgColor = Resources().TryLookup(winrt::box_value(bgKey)))
        {
            SettingsNav().Background(winrt::WUX::Media::SolidColorBrush(winrt::unbox_value<Windows::UI::Color>(bgColor)));
        }
    }

}
Repository: microsoft/terminal
File: src/cascadia/TerminalSettingsEditor/MainPage.cpp (C++)

Code before:
            _Navigate(*profileViewModel, BreadcrumbSubPage::None);
        }
        else
        {
            _Navigate(newTag.as<hstring>(), BreadcrumbSubPage::None);
        }
    }

    IObservableVector<IInspectable> MainPage::Breadcrumbs() noexcept
    {
        return _breadcrumbs;
    }

    winrt::Windows::UI::Xaml::Media::Brush MainPage::BackgroundBrush()
    {
        return SettingsNav().Background();
    }

    // If the theme asks for Mica, then drop out our background, so that we
    // can have mica too.
    void MainPage::_updateBackgroundForMica()

    {
        const auto& theme = _settingsSource.GlobalSettings().CurrentTheme();

        auto bgKey = (theme.Window() != nullptr && theme.Window().UseMica()) ?
                         L"SettingsPageMicaBackground" :
                         L"SettingsPageBackground";

        if (auto bgColor = Resources().TryLookup(winrt::box_value(bgKey)))
        {
            SettingsNav().Background(winrt::WUX::Media::SolidColorBrush(winrt::unbox_value<Windows::UI::Color>(bgColor)));
        }
    }

}


Review comment (bug):
```suggestion
        const auto bgKey = (theme.Window() != nullptr && theme.Window().UseMica()) ?
```

Code after:
            _Navigate(*profileViewModel, BreadcrumbSubPage::None);
        }
        else
        {
            _Navigate(newTag.as<hstring>(), BreadcrumbSubPage::None);
        }
    }

    IObservableVector<IInspectable> MainPage::Breadcrumbs() noexcept
    {
        return _breadcrumbs;
    }

    winrt::Windows::UI::Xaml::Media::Brush MainPage::BackgroundBrush()
    {
        return SettingsNav().Background();
    }

    // If the theme asks for Mica, then drop out our background, so that we
    // can have mica too.
    void MainPage::_UpdateBackgroundForMica()

    {
        const auto& theme = _settingsSource.GlobalSettings().CurrentTheme();

        const auto bgKey = (theme.Window() != nullptr && theme.Window().UseMica()) ?
                               L"SettingsPageMicaBackground" :
                               L"SettingsPageBackground";

        if (const auto bgColor = Resources().TryLookup(winrt::box_value(bgKey)))
        {
            SettingsNav().Background(winrt::WUX::Media::SolidColorBrush(winrt::unbox_value<Windows::UI::Color>(bgColor)));
        }
    }

}
Repository: microsoft/terminal
File: src/cascadia/TerminalSettingsEditor/MainPage.cpp (C++)

Code before:
            _Navigate(newTag.as<hstring>(), BreadcrumbSubPage::None);
        }
    }

    IObservableVector<IInspectable> MainPage::Breadcrumbs() noexcept
    {
        return _breadcrumbs;
    }

    winrt::Windows::UI::Xaml::Media::Brush MainPage::BackgroundBrush()
    {
        return SettingsNav().Background();
    }

    // If the theme asks for Mica, then drop out our background, so that we
    // can have mica too.
    void MainPage::_updateBackgroundForMica()

    {
        const auto& theme = _settingsSource.GlobalSettings().CurrentTheme();

        auto bgKey = (theme.Window() != nullptr && theme.Window().UseMica()) ?
                         L"SettingsPageMicaBackground" :
                         L"SettingsPageBackground";

        if (auto bgColor = Resources().TryLookup(winrt::box_value(bgKey)))
        {
            SettingsNav().Background(winrt::WUX::Media::SolidColorBrush(winrt::unbox_value<Windows::UI::Color>(bgColor)));
        }
    }

}


Review comment (suggestion):
```suggestion
        if (const auto bgColor = Resources().TryLookup(winrt::box_value(bgKey)))
```

Code after:
            _Navigate(newTag.as<hstring>(), BreadcrumbSubPage::None);
        }
    }

    IObservableVector<IInspectable> MainPage::Breadcrumbs() noexcept
    {
        return _breadcrumbs;
    }

    winrt::Windows::UI::Xaml::Media::Brush MainPage::BackgroundBrush()
    {
        return SettingsNav().Background();
    }

    // If the theme asks for Mica, then drop out our background, so that we
    // can have mica too.
    void MainPage::_UpdateBackgroundForMica()

    {
        const auto& theme = _settingsSource.GlobalSettings().CurrentTheme();

        const auto bgKey = (theme.Window() != nullptr && theme.Window().UseMica()) ?
                               L"SettingsPageMicaBackground" :
                               L"SettingsPageBackground";

        if (const auto bgColor = Resources().TryLookup(winrt::box_value(bgKey)))
        {
            SettingsNav().Background(winrt::WUX::Media::SolidColorBrush(winrt::unbox_value<Windows::UI::Color>(bgColor)));
        }
    }

}
Repository: microsoft/terminal
File: src/cascadia/TerminalControl/TermControl.cpp (C++)

Code before:

    void TermControl::ToggleMarkMode()
    {
        _core.ToggleMarkMode();
    }

    bool TermControl::SwitchSelectionEndpoint()
    {
        return _core.SwitchSelectionEndpoint();
    }

    bool TermControl::ExpandSelectionToWord()
    {
        return _core.ExpandSelectionToWord();
    }

    void TermControl::Close()
    {
        if (!_IsClosing())
        {
            _closing = true;
            if (_automationPeer)
            {
                auto autoPeerImpl{ winrt::get_self<implementation::TermControlAutomationPeer>(_automationPeer) };
                autoPeerImpl->IsClosing(true);
            }

            _RestorePointerCursorHandlers(*this, nullptr);

            // Disconnect the TSF input control so it doesn't receive EditContext events.
            TSFInputControl().Close();
            _autoScrollTimer.Stop();

            _core.Close();
        }
    }

    // Method Description:
    // - Scrolls the viewport of the terminal and updates the scroll bar accordingly
    // Arguments:
    // - viewTop: the viewTop to scroll to
    void TermControl::ScrollViewport(int viewTop)
    {
        ScrollBar().Value(viewTop);
    }

    int TermControl::ScrollOffset() const
    {
        return _core.ScrollOffset();
    }


Review comment (suggestion):
I don't like this too too much, but I don't know if there's a better way to do this. `IsClosing()` is a public function off the impl type, so that helps ensure it isn't abused.

I'm mildly annoyed that we only use it _once_, but `_closing` is only set in one spot so I'll take what I can get haha.

Code after:

    void TermControl::ToggleMarkMode()
    {
        _core.ToggleMarkMode();
    }

    bool TermControl::SwitchSelectionEndpoint()
    {
        return _core.SwitchSelectionEndpoint();
    }

    bool TermControl::ExpandSelectionToWord()
    {
        return _core.ExpandSelectionToWord();
    }

    void TermControl::Close()
    {
        if (!_IsClosing())
        {
            _closing = true;
            if (_automationPeer)
            {
                auto autoPeerImpl{ winrt::get_self<implementation::TermControlAutomationPeer>(_automationPeer) };
                autoPeerImpl->Close();
            }

            _RestorePointerCursorHandlers(*this, nullptr);

            // Disconnect the TSF input control so it doesn't receive EditContext events.
            TSFInputControl().Close();
            _autoScrollTimer.Stop();

            _core.Close();
        }
    }

    // Method Description:
    // - Scrolls the viewport of the terminal and updates the scroll bar accordingly
    // Arguments:
    // - viewTop: the viewTop to scroll to
    void TermControl::ScrollViewport(int viewTop)
    {
        ScrollBar().Value(viewTop);
    }

    int TermControl::ScrollOffset() const
    {
        return _core.ScrollOffset();
    }
Repository: microsoft/terminal
File: tools/ReleaseEngineering/New-TerminalStackedChangelog.ps1 (PowerShell)

Code before:
ForEach ($RevisionRange in $RevisionRanges) {
    # --pretty=format notes:
    #   - %an: author name
    #   - %x1C: print a literal FS (0x1C), which will be used as a delimiter
    #   - %ae: author email
    #   - %x1C: another FS
    #   - %s: subject, the title of the commit
    $NewEntries = & git log $RevisionRange "--pretty=format:%an%x1C%ae%x1C%s" |
        ConvertFrom-CSV -Delimiter "`u{001C}" -Header Author,Email,Subject

    $Entries += $NewEntries | % { [PSCustomObject]@{
        Author = $_.Author;
        Email = $_.Email;
        Subject = $_.Subject;
        Microsoft = (Test-MicrosoftPerson $_.Email);
        PossibleUsername = (Get-PossibleUserName $_.Email);
        RevRangeID = $i;
    } }
    $i++
}

For($c = 0; $c -Lt $i; $c++) {
    "   " + ("|" * $c) + "/ " + $RevisionRanges[$c]
}

$Unique = $Entries | Group-Object Subject #| %{ $_.Group[0] | Add-Member Count $_.Count -Force -PassThru }

$Unique | % {
    $en = $_.Group[0]
    $revSpec = (" " * $i)
    $_.Group | % {
        $revSpec = $revSpec.remove($_.RevRangeID, 1).insert($_.RevRangeID, "X")
    }
    $c = "[{0}] " -f $revSpec
    "* {0}{1}{2}" -f ($c, $en.Subject, (Generate-Thanks $en))
}


Review comment (suggestion):
```suggestion
$Unique = $Entries | Group-Object Subject
```

Code after:
ForEach ($RevisionRange in $RevisionRanges) {
    # --pretty=format notes:
    #   - %an: author name
    #   - %x1C: print a literal FS (0x1C), which will be used as a delimiter
    #   - %ae: author email
    #   - %x1C: another FS
    #   - %s: subject, the title of the commit
    $NewEntries = & git log $RevisionRange "--pretty=format:%an%x1C%ae%x1C%s" |
        ConvertFrom-CSV -Delimiter "`u{001C}" -Header Author,Email,Subject

    $Entries += $NewEntries | % { [PSCustomObject]@{
        Author = $_.Author;
        Email = $_.Email;
        Subject = $_.Subject;
        Microsoft = (Test-MicrosoftPerson $_.Email);
        PossibleUsername = (Get-PossibleUserName $_.Email);
        RevRangeID = $i;
    } }
    $i++
}

For($c = 0; $c -Lt $i; $c++) {
    "   " + ("|" * $c) + "/ " + $RevisionRanges[$c]
}

$Unique = $Entries | Group-Object Subject

$Unique | % {
    $en = $_.Group[0]
    $revSpec = (" " * $i)
    $_.Group | % {
        $revSpec = $revSpec.remove($_.RevRangeID, 1).insert($_.RevRangeID, "X")
    }
    $c = "[{0}] " -f $revSpec
    "* {0}{1}{2}" -f ($c, $en.Subject, (Generate-Thanks $en))
}
Repository: microsoft/terminal
File: src/cascadia/TerminalSettingsEditor/ProfileViewModel.cpp (C++)

Code before:
        auto currentStyle = BellStyle();
        WI_UpdateFlag(currentStyle, Model::BellStyle::Audible, winrt::unbox_value<bool>(on));
        BellStyle(currentStyle);
    }

    void ProfileViewModel::SetBellStyleWindow(winrt::Windows::Foundation::IReference<bool> on)
    {
        auto currentStyle = BellStyle();
        WI_UpdateFlag(currentStyle, Model::BellStyle::Window, winrt::unbox_value<bool>(on));
        BellStyle(currentStyle);
    }

    void ProfileViewModel::SetBellStyleTaskbar(winrt::Windows::Foundation::IReference<bool> on)
    {
        auto currentStyle = BellStyle();
        WI_UpdateFlag(currentStyle, Model::BellStyle::Taskbar, winrt::unbox_value<bool>(on));
        BellStyle(currentStyle);
    }

    void ProfileViewModel::DeleteProfile()
    {
        auto deleteProfileArgs{ winrt::make_self<DeleteProfileEventArgs>(Guid()) };
        _DeleteProfileHandlers(*this, *deleteProfileArgs);
    }

    void ProfileViewModel::SetupAppearances(Windows::Foundation::Collections::IObservableVector<Editor::ColorSchemeViewModel> schemesList, Editor::IHostedInWindow windowRoot)
    {
        WindowRoot(windowRoot);
        DefaultAppearance().SchemesList(schemesList);
        DefaultAppearance().WindowRoot(windowRoot);
        if (UnfocusedAppearance())
        {
            UnfocusedAppearance().SchemesList(schemesList);
            UnfocusedAppearance().WindowRoot(windowRoot);
        }
    }
}


Review comment (refactor):
Do we want to rename this to something like `SetupHelper` or something since it technically isn't _just_ setting up the appearances anymore? I don't feel too strongly about it 

Code after:
        WI_UpdateFlag(currentStyle, Model::BellStyle::Audible, winrt::unbox_value<bool>(on));
        BellStyle(currentStyle);
    }

    void ProfileViewModel::SetBellStyleWindow(winrt::Windows::Foundation::IReference<bool> on)
    {
        auto currentStyle = BellStyle();
        WI_UpdateFlag(currentStyle, Model::BellStyle::Window, winrt::unbox_value<bool>(on));
        BellStyle(currentStyle);
    }

    void ProfileViewModel::SetBellStyleTaskbar(winrt::Windows::Foundation::IReference<bool> on)
    {
        auto currentStyle = BellStyle();
        WI_UpdateFlag(currentStyle, Model::BellStyle::Taskbar, winrt::unbox_value<bool>(on));
        BellStyle(currentStyle);
    }

    void ProfileViewModel::DeleteProfile()
    {
        auto deleteProfileArgs{ winrt::make_self<DeleteProfileEventArgs>(Guid()) };
        _DeleteProfileHandlers(*this, *deleteProfileArgs);
    }

    void ProfileViewModel::SetupAppearances(Windows::Foundation::Collections::IObservableVector<Editor::ColorSchemeViewModel> schemesList)
    {
        DefaultAppearance().SchemesList(schemesList);
        if (UnfocusedAppearance())
        {
            UnfocusedAppearance().SchemesList(schemesList);
        }
    }
}
Repository: microsoft/terminal
File: src/cascadia/TerminalControl/ControlCore.cpp (C++)

Code before:
            {
                return true;
            }
        }

        // Didn't click on a mark with a command - don't show.
        return false;
    }

    // Method Description:
    // * Same as ShouldShowSelectCommand, but with the mark needing output
    bool ControlCore::ShouldShowSelectOutput()
    {
        // Relies on the anchor set in AnchorContextMenu

        // Don't show this if the click was on the selection
        if (_terminal->IsSelectionActive() &&
            _terminal->GetSelectionAnchor() <= _contextMenuBufferPosition &&
            _terminal->GetSelectionEnd() >= _contextMenuBufferPosition)
        {
            return false;
        }

        // DO show this if the click was on a mark with output
        const auto& marks{ _terminal->GetScrollMarks() };
        for (auto&& m : marks)
        {
            if (!m.HasOutput())
            {
                continue;
            }
            const auto [start, end] = m.GetExtent();
            if (start <= _contextMenuBufferPosition &&
                end >= _contextMenuBufferPosition)
            {
                return true;
            }
        }
        // Didn't click on a mark with output - don't show.
        return false;
    }
}


Review comment (suggestion):
```suggestion
        for (const auto&& m : marks)
```

Code after:
        }
    }

    void ControlCore::ContextMenuSelectCommand()
    {
        _contextMenuSelectMark(
            _contextMenuBufferPosition,
            [](const DispatchTypes::ScrollMark& m) -> bool { return !m.HasCommand(); },
            [](const DispatchTypes::ScrollMark& m) { return til::point_span{ m.end, *m.commandEnd }; });
    }
    void ControlCore::ContextMenuSelectOutput()
    {
        _contextMenuSelectMark(
            _contextMenuBufferPosition,
            [](const DispatchTypes::ScrollMark& m) -> bool { return !m.HasOutput(); },
            [](const DispatchTypes::ScrollMark& m) { return til::point_span{ *m.commandEnd, *m.outputEnd }; });
    }

    bool ControlCore::_clickedOnMark(
        const til::point& pos,
        const std::function<bool(const DispatchTypes::ScrollMark&)>& filter)
    {
        // Don't show this if the click was on the selection
        if (_terminal->IsSelectionActive() &&
            _terminal->GetSelectionAnchor() <= pos &&
            _terminal->GetSelectionEnd() >= pos)
        {
            return false;
        }

        // DO show this if the click was on a mark with a command
        const auto& marks{ _terminal->GetScrollMarks() };
        for (auto&& m : marks)
        {
            if (filter && filter(m))
            {
                continue;
            }
            const auto [start, end] = m.GetExtent();
            if (start <= pos &&
                end >= pos)
            {
                return true;
            }
        }

        // Didn't click on a mark with a command - don't show.
        return false;
    }

    // Method Description: