Repository: microsoft/terminal
File: src/cascadia/TerminalSettingsModel/ActionMap.cpp (C++)

Code before:
            // In the real settings parser, we'd throw here:
            // throw winrt::hresult_error(WEB_E_INVALID_JSON_STRING, winrt::to_hstring(errs));
            //
            // That seems overly aggressive for something that we don't
            // really own. Instead, just bail out.
            return {};
        }

        std::unordered_map<hstring, Model::Command> result;
        if (auto actions{ root[JsonKey("snippets")] })
        {
            for (const auto& json : actions)
            {
                const auto snippet = Command::FromSnippetJson(json);
                result.insert_or_assign(snippet->Name(), *snippet);
            }
        }
        return result;
    }

    winrt::Windows::Foundation::IAsyncOperation<IVector<Model::Command>> ActionMap::FilterToSnippets(
        winrt::hstring currentCommandline,
        winrt::hstring currentWorkingDirectory)
    {
        // enumerate all the parent directories we want to import snippets from
        std::filesystem::path directory{ std::wstring_view{ currentWorkingDirectory } };
        std::vector<std::filesystem::path> directories;
        while (!directory.empty())
        {
            directories.push_back(directory);
            const auto parentPath = directory.parent_path();
            if (directory == parentPath)
            {
                break;
            }
            directory = parentPath;
        }

        {
            // Check if all the directories are already in the cache
            const auto& cache{ _cwdLocalSnippetsCache.lock_shared() };

            bool allSnippetsCached = true;
            for (const auto& dir : directories)
            {
                if (!cache->contains(dir))
                {
                    allSnippetsCached = false;
                    break;
                }
            }

Review comment (suggestion):
`std::wstring_view` cast can probably be safely removed.

Code after:
            // In the real settings parser, we'd throw here:
            // throw winrt::hresult_error(WEB_E_INVALID_JSON_STRING, winrt::to_hstring(errs));
            //
            // That seems overly aggressive for something that we don't
            // really own. Instead, just bail out.
            return {};
        }

        std::unordered_map<hstring, Model::Command> result;
        if (auto actions{ root[JsonKey("snippets")] })
        {
            for (const auto& json : actions)
            {
                const auto snippet = Command::FromSnippetJson(json);
                result.insert_or_assign(snippet->Name(), *snippet);
            }
        }
        return result;
    }

    winrt::Windows::Foundation::IAsyncOperation<IVector<Model::Command>> ActionMap::FilterToSnippets(
        winrt::hstring currentCommandline,
        winrt::hstring currentWorkingDirectory)
    {
        // enumerate all the parent directories we want to import snippets from
        std::filesystem::path directory{ std::wstring_view{ currentWorkingDirectory } };
        std::vector<std::filesystem::path> directories;
        while (!directory.empty())
        {
            directories.push_back(directory);
            auto parentPath = directory.parent_path();
            if (directory == parentPath)
            {
                break;
            }
            directory = std::move(parentPath);
        }

        {
            // Check if all the directories are already in the cache
            const auto& cache{ _cwdLocalSnippetsCache.lock_shared() };
            if (std::ranges::all_of(directories, [&](auto&& dir) { return cache->contains(dir); }))
            {
                // Load snippets from directories in reverse order.
                // This ensures that we prioritize snippets closer to the cwd.
                // The map makes it easy to avoid duplicates.
                std::unordered_map<hstring, Model::Command> localSnippetsMap;
                for (auto rit = directories.rbegin(); rit != directories.rend(); ++rit)
                {
                    // register snippets from cache
                    for (const auto& [name, snippet] : cache->at(*rit))