@@ -864,42 +864,49 @@ public static String getPresentableTemplateName(@NotNull PsiElement psiElement,
864
864
* foo.html.twig => ["views/foo.html.twig", "templates/foo.html.twig"]
865
865
*/
866
866
@ 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 ;
869
879
870
880
// cache twig and all files,
871
881
// only PHP files we dont need to cache
872
- if (useTwig && !usePhp ) {
882
+ if (!usePhp ) {
873
883
// cache twig files only, most use case
874
884
CachedValue <Map <String , Set <VirtualFile >>> cache = project .getUserData (TEMPLATE_CACHE_TWIG );
875
885
if (cache == null ) {
876
- cache = CachedValuesManager .getManager (project ).createCachedValue (new MyTwigOnlyTemplateFileMapCachedValueProvider (project ), false );
886
+ cache = CachedValuesManager .getManager (project ).createCachedValue (new MyAllTemplateFileMapCachedValueProvider (project ), false );
877
887
project .putUserData (TEMPLATE_CACHE_TWIG , cache );
878
888
}
879
889
880
890
templateMapProxy = cache .getValue ();
881
-
882
- } else if (useTwig && usePhp ) {
891
+ } else {
883
892
// cache all files
884
893
CachedValue <Map <String , Set <VirtualFile >>> cache = project .getUserData (TEMPLATE_CACHE_ALL );
885
894
if (cache == null ) {
886
- cache = CachedValuesManager .getManager (project ).createCachedValue (new MyAllTemplateFileMapCachedValueProvider (project ), false );
895
+ cache = CachedValuesManager .getManager (project ).createCachedValue (new MyAllTemplateFileMapCachedValueProvider (project , true ), false );
887
896
project .putUserData (TEMPLATE_CACHE_ALL , cache );
888
897
}
889
898
890
899
templateMapProxy = cache .getValue ();
891
900
}
892
901
893
- // cache-less calls
894
- if (templateMapProxy == null ) {
895
- templateMapProxy = getTemplateMapProxy (project , useTwig , usePhp );
896
- }
897
-
898
902
return templateMapProxy ;
899
903
}
900
904
905
+ /**
906
+ * Cache less visiting template files
907
+ */
901
908
@ 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 ) {
903
910
List <TwigPath > twigPaths = new ArrayList <>(getTwigNamespaces (project ));
904
911
if (twigPaths .size () == 0 ) {
905
912
return Collections .emptyMap ();
@@ -913,39 +920,29 @@ private static Map<String, Set<VirtualFile>> getTemplateMapProxy(@NotNull Projec
913
920
}
914
921
915
922
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
+ }
920
926
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
+ );
925
933
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 <>());
927
937
}
938
+
939
+ templateNames .get (entry .getKey ()).add (entry .getValue ());
928
940
}
929
941
}
930
942
931
943
return templateNames ;
932
944
}
933
945
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
-
949
946
@ Nullable
950
947
private static TwigNamespaceSetting findManagedTwigNamespace (@ NotNull Project project , @ NotNull TwigPath twigPath ) {
951
948
List <TwigNamespaceSetting > twigNamespaces = Settings .getInstance (project ).twigNamespaces ;
@@ -1481,7 +1478,8 @@ public static Collection<PsiElement> getTwigMacroTargets(final Project project,
1481
1478
public static Collection <LookupElement > getTwigLookupElements (@ NotNull Project project ) {
1482
1479
VirtualFile baseDir = project .getBaseDir ();
1483
1480
1484
- return getTwigTemplateFiles (project ).entrySet ().stream ()
1481
+ return getTemplateMap (project ).entrySet ()
1482
+ .stream ()
1485
1483
.filter (entry -> entry .getValue ().size () > 0 )
1486
1484
.map ((java .util .function .Function <Map .Entry <String , Set <VirtualFile >>, LookupElement >) entry ->
1487
1485
new TemplateLookupElement (entry .getKey (), entry .getValue ().iterator ().next (), baseDir )
@@ -1496,7 +1494,8 @@ public static Collection<LookupElement> getTwigLookupElements(@NotNull Project p
1496
1494
public static Collection <LookupElement > getAllTemplateLookupElements (@ NotNull Project project ) {
1497
1495
VirtualFile baseDir = project .getBaseDir ();
1498
1496
1499
- return getTwigAndPhpTemplateFiles (project ).entrySet ().stream ()
1497
+ return getTemplateMap (project , true ).entrySet ()
1498
+ .stream ()
1500
1499
.filter (entry -> entry .getValue ().size () > 0 )
1501
1500
.map ((java .util .function .Function <Map .Entry <String , Set <VirtualFile >>, LookupElement >) entry ->
1502
1501
new TemplateLookupElement (entry .getKey (), entry .getValue ().iterator ().next (), baseDir )
@@ -2751,33 +2750,25 @@ public String getDomain() {
2751
2750
}
2752
2751
}
2753
2752
2754
- private static class MyTwigOnlyTemplateFileMapCachedValueProvider implements CachedValueProvider <Map <String , Set <VirtualFile >>> {
2753
+ private static class MyAllTemplateFileMapCachedValueProvider implements CachedValueProvider <Map <String , Set <VirtualFile >>> {
2755
2754
@ NotNull
2756
2755
private final Project project ;
2757
2756
2758
- MyTwigOnlyTemplateFileMapCachedValueProvider (@ NotNull Project project ) {
2759
- this .project = project ;
2760
- }
2757
+ private final boolean includePhpFiles ;
2761
2758
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 );
2766
2761
}
2767
- }
2768
-
2769
- private static class MyAllTemplateFileMapCachedValueProvider implements CachedValueProvider <Map <String , Set <VirtualFile >>> {
2770
- @ NotNull
2771
- private final Project project ;
2772
2762
2773
- MyAllTemplateFileMapCachedValueProvider (@ NotNull Project project ) {
2763
+ MyAllTemplateFileMapCachedValueProvider (@ NotNull Project project , boolean includePhpFiles ) {
2774
2764
this .project = project ;
2765
+ this .includePhpFiles = includePhpFiles ;
2775
2766
}
2776
2767
2777
2768
@ Nullable
2778
2769
@ Override
2779
2770
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 );
2781
2772
}
2782
2773
}
2783
2774
@@ -2798,20 +2789,19 @@ private static class MyLimitedVirtualFileVisitor extends VirtualFileVisitor {
2798
2789
@ NotNull
2799
2790
private Map <String , VirtualFile > results = new HashMap <>();
2800
2791
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 ;
2804
2795
2805
2796
@ NotNull
2806
2797
private Set <String > workedOn = new HashSet <>();
2807
2798
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 ) {
2809
2800
super (VirtualFileVisitor .limit (maxDepth ));
2810
2801
2811
2802
this .project = project ;
2812
2803
this .twigPath = twigPath ;
2813
2804
this .withPhp = withPhp ;
2814
- this .withTwig = withTwig ;
2815
2805
this .childrenAllowToVisit = maxDirs ;
2816
2806
}
2817
2807
@@ -2852,7 +2842,7 @@ private boolean isProcessable(VirtualFile virtualFile) {
2852
2842
return false ;
2853
2843
}
2854
2844
2855
- if (withTwig && virtualFile .getFileType () instanceof TwigFileType ) {
2845
+ if (virtualFile .getFileType () instanceof TwigFileType ) {
2856
2846
return true ;
2857
2847
}
2858
2848
@@ -2864,8 +2854,15 @@ private boolean isProcessable(VirtualFile virtualFile) {
2864
2854
}
2865
2855
2866
2856
@ NotNull
2867
- public Map <String , VirtualFile > getResults () {
2857
+ private Map <String , VirtualFile > getResults () {
2868
2858
return results ;
2869
2859
}
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
+ }
2870
2867
}
2871
2868
}
0 commit comments