1
1
package fr .adrienbrault .idea .symfony2plugin ;
2
2
3
+ import com .intellij .codeInsight .lookup .LookupElement ;
3
4
import com .intellij .openapi .project .Project ;
4
5
import com .intellij .openapi .vfs .VfsUtil ;
5
6
import com .intellij .openapi .vfs .VirtualFile ;
25
26
import fr .adrienbrault .idea .symfony2plugin .asset .dic .AssetFile ;
26
27
import fr .adrienbrault .idea .symfony2plugin .stubs .SymfonyProcessors ;
27
28
import fr .adrienbrault .idea .symfony2plugin .stubs .indexes .TwigMacroFunctionStubIndex ;
29
+ import fr .adrienbrault .idea .symfony2plugin .templating .TemplateLookupElement ;
28
30
import fr .adrienbrault .idea .symfony2plugin .templating .path .*;
29
31
import fr .adrienbrault .idea .symfony2plugin .templating .util .TwigTypeResolveUtil ;
30
32
import fr .adrienbrault .idea .symfony2plugin .util .PhpElementsUtil ;
@@ -49,10 +51,11 @@ public class TwigHelper {
49
51
50
52
public static String TEMPLATE_ANNOTATION_CLASS = "\\ Sensio\\ Bundle\\ FrameworkExtraBundle\\ Configuration\\ Template" ;
51
53
52
- synchronized public static Map <String , PsiFile > getTemplateFilesByName (Project project , boolean useTwig , boolean usePhp ) {
53
- Map <String , PsiFile > results = new HashMap <String , PsiFile >();
54
+ public static Map <String , VirtualFile > getTemplateFilesByName (Project project , boolean useTwig , boolean usePhp ) {
54
55
55
- ArrayList <TwigPath > twigPaths = new ArrayList <TwigPath >();
56
+ Map <String , VirtualFile > results = new HashMap <String , VirtualFile >();
57
+
58
+ List <TwigPath > twigPaths = new ArrayList <TwigPath >();
56
59
twigPaths .addAll (getTwigNamespaces (project ));
57
60
58
61
if (twigPaths .size () == 0 ) {
@@ -82,18 +85,11 @@ public boolean visitFile(@NotNull VirtualFile virtualFile) {
82
85
return results ;
83
86
}
84
87
85
- synchronized public static Map <String , TwigFile > getTwigFilesByName (Project project ) {
86
- Map <String , TwigFile > results = new HashMap <String , TwigFile >();
87
- for (Map .Entry <String , PsiFile > entry : getTemplateFilesByName (project , true , true ).entrySet ()) {
88
- if (entry .getValue () instanceof TwigFile ) {
89
- results .put (entry .getKey (), (TwigFile ) entry .getValue ());
90
- }
91
- }
92
-
93
- return results ;
88
+ public static Map <String , VirtualFile > getTwigFilesByName (Project project ) {
89
+ return getTemplateFilesByName (project , true , false );
94
90
}
95
91
96
- synchronized public static Map <String , PsiFile > getTemplateFilesByName (Project project ) {
92
+ public static Map <String , VirtualFile > getTemplateFilesByName (Project project ) {
97
93
return getTemplateFilesByName (project , true , true );
98
94
}
99
95
@@ -175,20 +171,27 @@ public static PsiElement[] getTemplatePsiElements(Project project, String templa
175
171
176
172
String normalizedTemplateName = normalizeTemplateName (templateName );
177
173
178
- Map <String , PsiFile > twigFiles = TwigHelper .getTemplateFilesByName (project );
174
+ Map <String , VirtualFile > twigFiles = TwigHelper .getTemplateFilesByName (project );
179
175
if (!twigFiles .containsKey (normalizedTemplateName )) {
180
176
return new PsiElement [0 ];
181
177
}
182
178
183
- return new PsiElement [] {twigFiles .get (normalizedTemplateName )};
179
+ VirtualFile virtualFile = twigFiles .get (normalizedTemplateName );
180
+
181
+ PsiFile psiFile = PsiManager .getInstance (project ).findFile (virtualFile );
182
+ if (psiFile != null ) {
183
+ return new PsiElement [] {psiFile };
184
+ }
185
+
186
+ return new PsiElement [0 ];
184
187
}
185
188
186
- synchronized public static ArrayList <TwigPath > getTwigNamespaces (Project project ) {
189
+ public static List <TwigPath > getTwigNamespaces (Project project ) {
187
190
return getTwigNamespaces (project , true );
188
191
}
189
192
190
- synchronized public static ArrayList <TwigPath > getTwigNamespaces (Project project , boolean includeSettings ) {
191
- ArrayList <TwigPath > twigPaths = new ArrayList <TwigPath >();
193
+ public static List <TwigPath > getTwigNamespaces (Project project , boolean includeSettings ) {
194
+ List <TwigPath > twigPaths = new ArrayList <TwigPath >();
192
195
PhpIndex phpIndex = PhpIndex .getInstance (project );
193
196
194
197
TwigPathServiceParser twigPathServiceParser = ServiceXmlParserFactory .getInstance (project , TwigPathServiceParser .class );
@@ -219,7 +222,7 @@ synchronized public static ArrayList<TwigPath> getTwigNamespaces(Project project
219
222
return twigPaths ;
220
223
}
221
224
222
- ArrayList <TwigNamespaceSetting > twigNamespaceSettings = ( ArrayList < TwigNamespaceSetting >) Settings .getInstance (project ).twigNamespaces ;
225
+ List <TwigNamespaceSetting > twigNamespaceSettings = Settings .getInstance (project ).twigNamespaces ;
223
226
if (twigNamespaceSettings != null ) {
224
227
for (TwigNamespaceSetting twigNamespaceSetting : twigNamespaceSettings ) {
225
228
if (twigNamespaceSetting .isCustom ()) {
@@ -835,4 +838,31 @@ public boolean execute(@NotNull PsiElement psiElement) {
835
838
return targets ;
836
839
}
837
840
841
+ public static Collection <LookupElement > getTwigLookupElements (Project project ) {
842
+ VirtualFile baseDir = project .getBaseDir ();
843
+
844
+ Collection <LookupElement > lookupElements = new ArrayList <LookupElement >();
845
+
846
+ for (Map .Entry <String , VirtualFile > entry : TwigHelper .getTwigFilesByName (project ).entrySet ()) {
847
+ lookupElements .add (
848
+ new TemplateLookupElement (entry .getKey (), entry .getValue (), baseDir )
849
+ );
850
+ }
851
+
852
+ return lookupElements ;
853
+ }
854
+
855
+ public static Collection <LookupElement > getAllTemplateLookupElements (Project project ) {
856
+ VirtualFile baseDir = project .getBaseDir ();
857
+
858
+ Collection <LookupElement > lookupElements = new ArrayList <LookupElement >();
859
+
860
+ for (Map .Entry <String , VirtualFile > entry : TwigHelper .getTemplateFilesByName (project ).entrySet ()) {
861
+ lookupElements .add (
862
+ new TemplateLookupElement (entry .getKey (), entry .getValue (), baseDir )
863
+ );
864
+ }
865
+
866
+ return lookupElements ;
867
+ }
838
868
}
0 commit comments