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

Code before:
            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;
                }
            }
            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.

Review comment (suggestion):
You _can_ remove the string copy, but it looks ugly so maybe don't.

```suggestion
            auto parentPath = directory.parent_path();
            const auto done = directory == parentPath;
            directories.push_back(std::move(directory));
            if (done)
            {
                break;
            }
            directory = std::move(parentPath);
```

Code after:
            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))
                    {
                        localSnippetsMap.insert_or_assign(name, snippet);
                    }
                }