<commit_before>package fr.adrienbrault.idea.symfony2plugin;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.patterns.ElementPattern;
import com.intellij.patterns.PlatformPatterns;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiWhiteSpace;
import com.jetbrains.php.PhpIndex;
import com.jetbrains.twig.*;
import fr.adrienbrault.idea.symfony2plugin.templating.path.TwigPath;
import fr.adrienbrault.idea.symfony2plugin.templating.path.TwigPathContentIterator;
import fr.adrienbrault.idea.symfony2plugin.templating.path.TwigPathIndex;
import fr.adrienbrault.idea.symfony2plugin.templating.path.TwigPathServiceParser;
import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils;
import fr.adrienbrault.idea.symfony2plugin.util.SymfonyBundleUtil;
import fr.adrienbrault.idea.symfony2plugin.util.dict.SymfonyBundle;
import fr.adrienbrault.idea.symfony2plugin.util.service.ServiceXmlParserFactory;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @author <NAME> <<EMAIL>>
 */
public class TwigHelper {

    public static Map<String, TwigFile> getTwigFilesByName(Project project) {

        PhpIndex phpIndex = PhpIndex.getInstance(project);
        final Map<String, TwigFile> results = new HashMap<String, TwigFile>();

        ProjectFileIndex fileIndex = ProjectFileIndex.SERVICE.getInstance(project);

        ArrayList<String> uniqueNamespaceList = new ArrayList<String>();
        ArrayList<TwigPath> twigPaths = new ArrayList<TwigPath>();

        ServiceXmlParserFactory xmlParser = ServiceXmlParserFactory.getInstance(project, TwigPathServiceParser.class);
        Object twigPathIndex = xmlParser.parser();

        if(twigPathIndex instanceof TwigPathIndex) {
            for (TwigPath twigPath : ((TwigPathIndex) twigPathIndex).getTwigPaths()) {
                uniqueNamespaceList.add(twigPath.getNamespace());
            }

            twigPaths.addAll(((TwigPathIndex) twigPathIndex).getTwigPaths());
        }

        // provide bundle callback for symfony < 2.2, which dont have addPath in container file
        if(twigPaths.size() == 0) {
            getTwigFilesByNameBundleFallback(project, phpIndex, uniqueNamespaceList, twigPaths);
        }

        for (TwigPath twigPath : twigPaths) {
            VirtualFile virtualDirectoryFile = twigPath.getDirectory();
            if(virtualDirectoryFile != null) {
                TwigPathContentIterator twigPathContentIterator = new TwigPathContentIterator(project, twigPath);
                fileIndex.iterateContentUnderDirectory(virtualDirectoryFile, twigPathContentIterator);<commit_msg>not all bundle gets registered on container addPath, so use fallback every call<commit_after>package fr.adrienbrault.idea.symfony2plugin;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.patterns.ElementPattern;
import com.intellij.patterns.PlatformPatterns;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiWhiteSpace;
import com.jetbrains.php.PhpIndex;
import com.jetbrains.twig.*;
import fr.adrienbrault.idea.symfony2plugin.templating.path.TwigPath;
import fr.adrienbrault.idea.symfony2plugin.templating.path.TwigPathContentIterator;
import fr.adrienbrault.idea.symfony2plugin.templating.path.TwigPathIndex;
import fr.adrienbrault.idea.symfony2plugin.templating.path.TwigPathServiceParser;
import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils;
import fr.adrienbrault.idea.symfony2plugin.util.SymfonyBundleUtil;
import fr.adrienbrault.idea.symfony2plugin.util.dict.SymfonyBundle;
import fr.adrienbrault.idea.symfony2plugin.util.service.ServiceXmlParserFactory;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @author <NAME> <<EMAIL>>
 */
public class TwigHelper {

    public static Map<String, TwigFile> getTwigFilesByName(Project project) {

        PhpIndex phpIndex = PhpIndex.getInstance(project);
        final Map<String, TwigFile> results = new HashMap<String, TwigFile>();

        ProjectFileIndex fileIndex = ProjectFileIndex.SERVICE.getInstance(project);

        ArrayList<String> uniqueNamespaceList = new ArrayList<String>();
        ArrayList<TwigPath> twigPaths = new ArrayList<TwigPath>();

        ServiceXmlParserFactory xmlParser = ServiceXmlParserFactory.getInstance(project, TwigPathServiceParser.class);
        Object twigPathIndex = xmlParser.parser();

        if(twigPathIndex instanceof TwigPathIndex) {
            for (TwigPath twigPath : ((TwigPathIndex) twigPathIndex).getTwigPaths()) {
                uniqueNamespaceList.add(twigPath.getNamespace());
            }

            twigPaths.addAll(((TwigPathIndex) twigPathIndex).getTwigPaths());
        }

        // provide bundle callback for symfony < 2.2, which dont have addPath in container file
        // it looks like not all bundle namespaces get registered in addPath, so add them every time
        getTwigFilesByNameBundleFallback(project, phpIndex, uniqueNamespaceList, twigPaths);

        for (TwigPath twigPath : twigPaths) {
            VirtualFile virtualDirectoryFile = twigPath.getDirectory();
            if(virtualDirectoryFile != null) {
                TwigPathContentIterator twigPathContentIterator = new TwigPathContentIterator(project, twigPath);
                fileIndex.iterateContentUnderDirectory(virtualDirectoryFile, twigPathContentIterator);
{'fraction_non_alphanumeric': 0.09224211423699914, 'fraction_numerical': 0.00392156862745098, 'mean_word_length': 4.554924242424242, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5008543', 'n_tokens_mistral': 1748, 'n_tokens_neox': 1714, 'n_words': 297}