@@ -864,42 +864,49 @@ public static String getPresentableTemplateName(@NotNull PsiElement psiElement,
864864 * foo.html.twig => ["views/foo.html.twig", "templates/foo.html.twig"]
865865 */
866866 @ NotNull
867- private static synchronized Map <String , Set <VirtualFile >> getTemplateMap (@ NotNull Project project , boolean useTwig , final boolean usePhp ) {
868- Map <String , Set <VirtualFile >> templateMapProxy = null ;
867+ public static Map <String , Set <VirtualFile >> getTemplateMap (@ NotNull Project project ) {
868+ return getTemplateMap (project , false );
869+ }
870+
871+ /**
872+ * Generate a mapped template name file multiple relation:
873+ *
874+ * foo.html.twig => ["views/foo.html.twig", "templates/foo.html.twig"]
875+ */
876+ @ NotNull
877+ public static synchronized Map <String , Set <VirtualFile >> getTemplateMap (@ NotNull Project project , boolean usePhp ) {
878+ Map <String , Set <VirtualFile >> templateMapProxy ;
869879
870880 // cache twig and all files,
871881 // only PHP files we dont need to cache
872- if (useTwig && !usePhp ) {
882+ if (!usePhp ) {
873883 // cache twig files only, most use case
874884 CachedValue <Map <String , Set <VirtualFile >>> cache = project .getUserData (TEMPLATE_CACHE_TWIG );
875885 if (cache == null ) {
876- cache = CachedValuesManager .getManager (project ).createCachedValue (new MyTwigOnlyTemplateFileMapCachedValueProvider (project ), false );
886+ cache = CachedValuesManager .getManager (project ).createCachedValue (new MyAllTemplateFileMapCachedValueProvider (project ), false );
877887 project .putUserData (TEMPLATE_CACHE_TWIG , cache );
878888 }
879889
880890 templateMapProxy = cache .getValue ();
881-
882- } else if (useTwig && usePhp ) {
891+ } else {
883892 // cache all files
884893 CachedValue <Map <String , Set <VirtualFile >>> cache = project .getUserData (TEMPLATE_CACHE_ALL );
885894 if (cache == null ) {
886- cache = CachedValuesManager .getManager (project ).createCachedValue (new MyAllTemplateFileMapCachedValueProvider (project ), false );
895+ cache = CachedValuesManager .getManager (project ).createCachedValue (new MyAllTemplateFileMapCachedValueProvider (project , true ), false );
887896 project .putUserData (TEMPLATE_CACHE_ALL , cache );
888897 }
889898
890899 templateMapProxy = cache .getValue ();
891900 }
892901
893- // cache-less calls
894- if (templateMapProxy == null ) {
895- templateMapProxy = getTemplateMapProxy (project , useTwig , usePhp );
896- }
897-
898902 return templateMapProxy ;
899903 }
900904
905+ /**
906+ * Cache less visiting template files
907+ */
901908 @ NotNull
902- private static Map <String , Set <VirtualFile >> getTemplateMapProxy (@ NotNull Project project , boolean useTwig , boolean usePhp ) {
909+ private static Map <String , Set <VirtualFile >> getTemplateMapProxy (@ NotNull Project project , boolean usePhp ) {
903910 List <TwigPath > twigPaths = new ArrayList <>(getTwigNamespaces (project ));
904911 if (twigPaths .size () == 0 ) {
905912 return Collections .emptyMap ();
@@ -913,39 +920,29 @@ private static Map<String, Set<VirtualFile>> getTemplateMapProxy(@NotNull Projec
913920 }
914921
915922 VirtualFile virtualDirectoryFile = twigPath .getDirectory (project );
916- if (virtualDirectoryFile != null ) {
917- MyLimitedVirtualFileVisitor visitor = new MyLimitedVirtualFileVisitor (project , twigPath , usePhp , useTwig , 5 , 150 );
918-
919- VfsUtil .visitChildrenRecursively (virtualDirectoryFile , visitor );
923+ if (virtualDirectoryFile == null ) {
924+ continue ;
925+ }
920926
921- for (Map .Entry <String , VirtualFile > entry : visitor .getResults ().entrySet ()) {
922- if (!templateNames .containsKey (entry .getKey ())) {
923- templateNames .put (entry .getKey (), new HashSet <>());
924- }
927+ Map <String , VirtualFile > visitor = MyLimitedVirtualFileVisitor .createResult (
928+ virtualDirectoryFile ,
929+ project ,
930+ twigPath ,
931+ usePhp
932+ );
925933
926- templateNames .get (entry .getKey ()).add (entry .getValue ());
934+ for (Map .Entry <String , VirtualFile > entry : visitor .entrySet ()) {
935+ if (!templateNames .containsKey (entry .getKey ())) {
936+ templateNames .put (entry .getKey (), new HashSet <>());
927937 }
938+
939+ templateNames .get (entry .getKey ()).add (entry .getValue ());
928940 }
929941 }
930942
931943 return templateNames ;
932944 }
933945
934- @ NotNull
935- static Map <String , Set <VirtualFile >> getTwigTemplateFiles (@ NotNull Project project ) {
936- return getTemplateMap (project , true , false );
937- }
938-
939- @ NotNull
940- public static Collection <String > getTwigFileNames (@ NotNull Project project ) {
941- return getTemplateMap (project , true , false ).keySet ();
942- }
943-
944- @ NotNull
945- public static Map <String , Set <VirtualFile >> getTwigAndPhpTemplateFiles (@ NotNull Project project ) {
946- return getTemplateMap (project , true , true );
947- }
948-
949946 @ Nullable
950947 private static TwigNamespaceSetting findManagedTwigNamespace (@ NotNull Project project , @ NotNull TwigPath twigPath ) {
951948 List <TwigNamespaceSetting > twigNamespaces = Settings .getInstance (project ).twigNamespaces ;
@@ -1481,7 +1478,8 @@ public static Collection<PsiElement> getTwigMacroTargets(final Project project,
14811478 public static Collection <LookupElement > getTwigLookupElements (@ NotNull Project project ) {
14821479 VirtualFile baseDir = project .getBaseDir ();
14831480
1484- return getTwigTemplateFiles (project ).entrySet ().stream ()
1481+ return getTemplateMap (project ).entrySet ()
1482+ .stream ()
14851483 .filter (entry -> entry .getValue ().size () > 0 )
14861484 .map ((java .util .function .Function <Map .Entry <String , Set <VirtualFile >>, LookupElement >) entry ->
14871485 new TemplateLookupElement (entry .getKey (), entry .getValue ().iterator ().next (), baseDir )
@@ -1496,7 +1494,8 @@ public static Collection<LookupElement> getTwigLookupElements(@NotNull Project p
14961494 public static Collection <LookupElement > getAllTemplateLookupElements (@ NotNull Project project ) {
14971495 VirtualFile baseDir = project .getBaseDir ();
14981496
1499- return getTwigAndPhpTemplateFiles (project ).entrySet ().stream ()
1497+ return getTemplateMap (project , true ).entrySet ()
1498+ .stream ()
15001499 .filter (entry -> entry .getValue ().size () > 0 )
15011500 .map ((java .util .function .Function <Map .Entry <String , Set <VirtualFile >>, LookupElement >) entry ->
15021501 new TemplateLookupElement (entry .getKey (), entry .getValue ().iterator ().next (), baseDir )
@@ -2751,33 +2750,25 @@ public String getDomain() {
27512750 }
27522751 }
27532752
2754- private static class MyTwigOnlyTemplateFileMapCachedValueProvider implements CachedValueProvider <Map <String , Set <VirtualFile >>> {
2753+ private static class MyAllTemplateFileMapCachedValueProvider implements CachedValueProvider <Map <String , Set <VirtualFile >>> {
27552754 @ NotNull
27562755 private final Project project ;
27572756
2758- MyTwigOnlyTemplateFileMapCachedValueProvider (@ NotNull Project project ) {
2759- this .project = project ;
2760- }
2757+ private final boolean includePhpFiles ;
27612758
2762- @ Nullable
2763- @ Override
2764- public Result <Map <String , Set <VirtualFile >>> compute () {
2765- return Result .create (getTemplateMapProxy (project , true , false ), PsiModificationTracker .MODIFICATION_COUNT );
2759+ MyAllTemplateFileMapCachedValueProvider (@ NotNull Project project ) {
2760+ this (project , false );
27662761 }
2767- }
2768-
2769- private static class MyAllTemplateFileMapCachedValueProvider implements CachedValueProvider <Map <String , Set <VirtualFile >>> {
2770- @ NotNull
2771- private final Project project ;
27722762
2773- MyAllTemplateFileMapCachedValueProvider (@ NotNull Project project ) {
2763+ MyAllTemplateFileMapCachedValueProvider (@ NotNull Project project , boolean includePhpFiles ) {
27742764 this .project = project ;
2765+ this .includePhpFiles = includePhpFiles ;
27752766 }
27762767
27772768 @ Nullable
27782769 @ Override
27792770 public Result <Map <String , Set <VirtualFile >>> compute () {
2780- return Result .create (getTemplateMapProxy (project , true , true ), PsiModificationTracker .MODIFICATION_COUNT );
2771+ return Result .create (getTemplateMapProxy (project , includePhpFiles ), PsiModificationTracker .MODIFICATION_COUNT );
27812772 }
27822773 }
27832774
@@ -2798,20 +2789,19 @@ private static class MyLimitedVirtualFileVisitor extends VirtualFileVisitor {
27982789 @ NotNull
27992790 private Map <String , VirtualFile > results = new HashMap <>();
28002791
2801- private boolean withPhp = false ;
2802- private boolean withTwig = true ;
2803- private int childrenAllowToVisit = 1000 ;
2792+ final private boolean withPhp ;
2793+
2794+ private int childrenAllowToVisit ;
28042795
28052796 @ NotNull
28062797 private Set <String > workedOn = new HashSet <>();
28072798
2808- MyLimitedVirtualFileVisitor (@ NotNull Project project , @ NotNull TwigPath twigPath , boolean withPhp , boolean withTwig , int maxDepth , int maxDirs ) {
2799+ private MyLimitedVirtualFileVisitor (@ NotNull Project project , @ NotNull TwigPath twigPath , boolean withPhp , int maxDepth , int maxDirs ) {
28092800 super (VirtualFileVisitor .limit (maxDepth ));
28102801
28112802 this .project = project ;
28122803 this .twigPath = twigPath ;
28132804 this .withPhp = withPhp ;
2814- this .withTwig = withTwig ;
28152805 this .childrenAllowToVisit = maxDirs ;
28162806 }
28172807
@@ -2852,7 +2842,7 @@ private boolean isProcessable(VirtualFile virtualFile) {
28522842 return false ;
28532843 }
28542844
2855- if (withTwig && virtualFile .getFileType () instanceof TwigFileType ) {
2845+ if (virtualFile .getFileType () instanceof TwigFileType ) {
28562846 return true ;
28572847 }
28582848
@@ -2864,8 +2854,15 @@ private boolean isProcessable(VirtualFile virtualFile) {
28642854 }
28652855
28662856 @ NotNull
2867- public Map <String , VirtualFile > getResults () {
2857+ private Map <String , VirtualFile > getResults () {
28682858 return results ;
28692859 }
2860+
2861+ @ NotNull
2862+ static Map <String , VirtualFile > createResult (@ NotNull VirtualFile virtualFile , @ NotNull Project project , @ NotNull TwigPath twigPath , boolean withPhp ) {
2863+ MyLimitedVirtualFileVisitor visitor = new MyLimitedVirtualFileVisitor (project , twigPath , withPhp , 5 , 150 );
2864+ VfsUtil .visitChildrenRecursively (virtualFile , visitor );
2865+ return visitor .getResults ();
2866+ }
28702867 }
28712868}
0 commit comments