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

Code before:
        if (const auto& control{ _senderOrActiveControl(sender) })
        {
            const auto handled = control.SwitchSelectionEndpoint();
            args.Handled(handled);
        }
    }

    void TerminalPage::_HandleSuggestions(const IInspectable& /*sender*/,
                                          const ActionEventArgs& args)
    {
        if (args)
        {
            if (const auto& realArgs = args.ActionArgs().try_as<SuggestionsArgs>())
            {
                _doHandleSuggestions(realArgs);
                args.Handled(true);
            }
        }
    }

    safe_void_coroutine TerminalPage::_doHandleSuggestions(SuggestionsArgs realArgs)
    {
        const auto source = realArgs.Source();
        std::vector<Command> commandsCollection;
        Control::CommandHistoryContext context{ nullptr };
        winrt::hstring currentCommandline;
        winrt::hstring currentWorkingDirectory;

        // If the user wanted to use the current commandline to filter results,
        //    OR they wanted command history (or some other source that
        //       requires context from the control)
        // then get that here.
        const bool shouldGetContext = realArgs.UseCommandline() ||
                                      WI_IsAnyFlagSet(source, SuggestionsSource::CommandHistory | SuggestionsSource::QuickFixes);
        if (const auto& control{ _GetActiveControl() })
        {
            currentWorkingDirectory = control.CurrentWorkingDirectory();

            if (shouldGetContext)
            {
                context = control.CommandHistory();
                if (context)
                {
                    currentCommandline = context.CurrentCommandline();
                }
            }
        }

        // Aggregate all the commands from the different sources that
        // the user selected.


Review comment (suggestion):
Is an alternative to simply `get_strong` before switching to the background? For things like this, I don't see the value in vending a weak reference and then immediately resolving it when we can just `retain` before and `release` after...

Code after:
    void TerminalPage::_HandleSwitchSelectionEndpoint(const IInspectable& sender,
                                                      const ActionEventArgs& args)
    {
        if (const auto& control{ _senderOrActiveControl(sender) })
        {
            const auto handled = control.SwitchSelectionEndpoint();
            args.Handled(handled);
        }
    }

    void TerminalPage::_HandleSuggestions(const IInspectable& /*sender*/,
                                          const ActionEventArgs& args)
    {
        if (args)
        {
            if (const auto& realArgs = args.ActionArgs().try_as<SuggestionsArgs>())
            {
                _doHandleSuggestions(realArgs);
                args.Handled(true);
            }
        }
    }

    safe_void_coroutine TerminalPage::_doHandleSuggestions(SuggestionsArgs realArgs)
    {
        const auto weak = get_weak();
        const auto dispatcher = Dispatcher();
        const auto source = realArgs.Source();
        std::vector<Command> commandsCollection;
        Control::CommandHistoryContext context{ nullptr };
        winrt::hstring currentCommandline;
        winrt::hstring currentWorkingDirectory;

        // If the user wanted to use the current commandline to filter results,
        //    OR they wanted command history (or some other source that
        //       requires context from the control)
        // then get that here.
        const bool shouldGetContext = realArgs.UseCommandline() ||
                                      WI_IsAnyFlagSet(source, SuggestionsSource::CommandHistory | SuggestionsSource::QuickFixes);
        if (const auto& control{ _GetActiveControl() })
        {
            currentWorkingDirectory = control.CurrentWorkingDirectory();

            if (shouldGetContext)
            {
                context = control.CommandHistory();
                if (context)
                {
                    currentCommandline = context.CurrentCommandline();
                }
            }