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

Code before:
        {
            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;
                }
            }
            if (allSnippetsCached)
            {
                // 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 (decltype(directories)::reverse_iterator rit = directories.rbegin(); rit != directories.rend(); ++rit)
                {
                    // register snippets from cache
                    std::ranges::for_each(cache->at(*rit), [&localSnippetsMap](const auto& kvPair) {

Review comment (suggestion):
No matter what though, I'd remove the const from the the auto above and use `std::move` here. That actually seems useful to me.

Code after:
        {
            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))
                    {
                        localSnippetsMap.insert_or_assign(name, snippet);
                    }
                }

                std::vector<Model::Command> localSnippets;
                localSnippets.reserve(localSnippetsMap.size());
                std::ranges::transform(localSnippetsMap,
                                       std::back_inserter(localSnippets),
                                       [](const auto& kvPair) { return kvPair.second; });