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

Code before:
        pkgList = co_await _FindPackagesInCatalogAsync(catalog, PackageMatchField::Name, PackageFieldMatchOption::ContainsCaseInsensitive, query);
        if (pkgList.Size() > 0)
        {
            co_return pkgList;
        }
        pkgList = co_await _FindPackagesInCatalogAsync(catalog, PackageMatchField::Moniker, PackageFieldMatchOption::ContainsCaseInsensitive, query);
        co_return pkgList;
    }

    Windows::Foundation::IAsyncAction TerminalPage::_SearchMissingCommandHandler(const IInspectable /*sender*/, const Microsoft::Terminal::Control::SearchMissingCommandEventArgs args)
    {
        assert(!Dispatcher().HasThreadAccess());

        if (!Feature_QuickFix::IsEnabled())
        {
            co_return;
        }

        auto pkgList = co_await _FindPackageAsync(args.MissingCommand());
        for (int retries = 0; !pkgList && retries < 3; ++retries)
        {
            pkgList = co_await _FindPackageAsync(args.MissingCommand());
        }

        // no packages were found, nothing to suggest
        if (pkgList.Size() == 0)
        {
            co_return;
        }

        std::vector<hstring> suggestions;
        suggestions.reserve(pkgList.Size());
        for (auto pkg : pkgList)
        {
            suggestions.emplace_back(fmt::format(FMT_COMPILE(L"winget install --id {}"), pkg.CatalogPackage().Id()));
        }

        co_await wil::resume_foreground(Dispatcher());

        auto term = _GetActiveControl();
        if (!term)
        {
            co_return;
        }
        term.UpdateWinGetSuggestions(single_threaded_vector<hstring>(std::move(suggestions)));
        term.RefreshQuickFixMenu();
    }

    // Method Description:
    // - Paste text from the Windows Clipboard to the focused terminal
    void TerminalPage::_PasteText()

Review comment (bug):
```suggestion
        if (!pkgList || pkgList.Size() == 0)
```
In case you hit the retry limit and still have a nullptr.

Code after:
        filter.Value(query);

        FindPackagesOptions options = WindowsPackageManagerFactory::CreateFindPackagesOptions();
        options.Filters().Append(filter);
        options.ResultLimit(20);

        IVectorView<MatchResult> pkgList;
        for (const auto& search : searches)
        {
            filter.Field(search.Field);
            filter.Option(search.MatchOption);

            const auto result = co_await catalog.FindPackagesAsync(options);
            pkgList = result.Matches();
            if (pkgList.Size() > 0)
            {
                break;
            }
        }
        co_return pkgList;
    }

    Windows::Foundation::IAsyncAction TerminalPage::_SearchMissingCommandHandler(const IInspectable /*sender*/, const Microsoft::Terminal::Control::SearchMissingCommandEventArgs args)
    {
        if (!Feature_QuickFix::IsEnabled())
        {
            co_return;
        }
        co_await winrt::resume_background();

        // no packages were found, nothing to suggest
        const auto pkgList = co_await _FindPackageAsync(args.MissingCommand());
        if (!pkgList || pkgList.Size() == 0)
        {
            co_return;
        }

        std::vector<hstring> suggestions;
        suggestions.reserve(pkgList.Size());
        for (const auto& pkg : pkgList)
        {
            // --id and --source ensure we don't collide with another package catalog
            suggestions.emplace_back(fmt::format(FMT_COMPILE(L"winget install --id {} -s winget"), pkg.CatalogPackage().Id()));
        }

        co_await wil::resume_foreground(Dispatcher());

        auto term = _GetActiveControl();
        if (!term)
        {
            co_return;