@@ -102,7 +102,7 @@ public static Map<String, VirtualFile> getTemplateFilesByName(Project project) {
102102 @ Nullable
103103 public static TwigNamespaceSetting findManagedTwigNamespace (Project project , TwigPath twigPath ) {
104104
105- ArrayList <TwigNamespaceSetting > twigNamespaces = ( ArrayList < TwigNamespaceSetting >) Settings .getInstance (project ).twigNamespaces ;
105+ List <TwigNamespaceSetting > twigNamespaces = Settings .getInstance (project ).twigNamespaces ;
106106 if (twigNamespaces == null ) {
107107 return null ;
108108 }
@@ -116,29 +116,12 @@ public static TwigNamespaceSetting findManagedTwigNamespace(Project project, Twi
116116 return null ;
117117 }
118118
119- /**
120- * todo: migrate getTemplatePsiElements to this method. think of support twig and php templates
121- */
122- public static PsiFile [] getTemplateFilesByName (Project project , String templateName ) {
123-
124- ArrayList <PsiFile > psiFiles = new ArrayList <PsiFile >();
125-
126- for (PsiElement templateTarget : TwigHelper .getTemplatePsiElements (project , templateName )) {
127- if (templateTarget instanceof PsiFile ) {
128- psiFiles .add ((TwigFile ) templateTarget );
129- }
130- }
131-
132- return psiFiles .toArray (new PsiFile [psiFiles .size ()]);
133- }
134-
135119 @ Nullable
136120 public static PsiFile getTemplateFileByName (Project project , String templateName ) {
137121
138- for (PsiElement templateTarget : TwigHelper .getTemplatePsiElements (project , templateName )) {
139- if (templateTarget instanceof PsiFile ) {
140- return (PsiFile ) templateTarget ;
141- }
122+ PsiFile [] templatePsiElements = TwigHelper .getTemplatePsiElements (project , templateName );
123+ if (templatePsiElements .length > 0 ) {
124+ return templatePsiElements [0 ];
142125 }
143126
144127 return null ;
@@ -173,23 +156,83 @@ public static String normalizeTemplateName(String templateName) {
173156
174157 }
175158
176- public static PsiElement [] getTemplatePsiElements (Project project , String templateName ) {
159+ /**
160+ * Find file in a twig path collection
161+ *
162+ * @param project current project
163+ * @param templateName path known, should not be normalized
164+ * @return target files
165+ */
166+ public static PsiFile [] getTemplatePsiElements (Project project , String templateName ) {
167+
177168
178169 String normalizedTemplateName = normalizeTemplateName (templateName );
179170
180- Map <String , VirtualFile > twigFiles = TwigHelper .getTemplateFilesByName (project );
181- if (!twigFiles .containsKey (normalizedTemplateName )) {
182- return new PsiElement [0 ];
183- }
171+ Collection <PsiFile > psiFiles = new HashSet <PsiFile >();
184172
185- VirtualFile virtualFile = twigFiles .get (normalizedTemplateName );
173+ for (TwigPath twigPath : getTwigNamespaces (project )) {
174+
175+ if (!twigPath .isEnabled ()) {
176+ continue ;
177+ }
178+
179+ if (normalizedTemplateName .startsWith ("@" )) {
180+ // @Namespace/base.html.twig
181+ // @Namespace/folder/base.html.twig
182+ if (normalizedTemplateName .length () > 1 && twigPath .getNamespaceType () != TwigPathIndex .NamespaceType .BUNDLE ) {
183+ int i = normalizedTemplateName .indexOf ("/" );
184+ if (i > 0 ) {
185+ String templateNs = normalizedTemplateName .substring (1 , i );
186+ if (twigPath .getNamespace ().equals (templateNs )) {
187+ addFileInsideTwigPath (project , normalizedTemplateName .substring (i + 1 ), psiFiles , twigPath );
188+ }
189+ }
190+ }
191+ } else if (normalizedTemplateName .startsWith (":" )) {
192+ // ::base.html.twig
193+ // :Foo:base.html.twig
194+ if (normalizedTemplateName .length () > 1 && twigPath .getNamespaceType () == TwigPathIndex .NamespaceType .ADD_PATH ) {
195+ String templatePath = StringUtils .strip (normalizedTemplateName .replace (":" , "/" ), "/" );
196+ addFileInsideTwigPath (project , templatePath , psiFiles , twigPath );
197+ }
198+ } else {
199+ // FooBundle::base.html.twig
200+ // FooBundle:Bar:base.html.twig
201+ if (twigPath .getNamespaceType () == TwigPathIndex .NamespaceType .BUNDLE ) {
202+ int i = normalizedTemplateName .indexOf (":" );
203+ if (i > 0 ) {
204+ String templateNs = normalizedTemplateName .substring (0 , i );
205+ if (twigPath .getNamespace ().equals (templateNs )) {
206+ String templatePath = StringUtils .strip (normalizedTemplateName .substring (i + 1 ).replace (":" , "/" ).replace ("//" , "/" ), "/" );
207+ addFileInsideTwigPath (project , templatePath , psiFiles , twigPath );
208+ }
209+
210+ }
211+ }
212+
213+ // form_div_layout.html.twig
214+ if (twigPath .isGlobalNamespace () && twigPath .getNamespaceType () == TwigPathIndex .NamespaceType .ADD_PATH ) {
215+ String templatePath = StringUtils .strip (normalizedTemplateName .replace (":" , "/" ), "/" );
216+ addFileInsideTwigPath (project , templatePath , psiFiles , twigPath );
217+ }
218+
219+ }
186220
187- PsiFile psiFile = PsiManager .getInstance (project ).findFile (virtualFile );
188- if (psiFile != null ) {
189- return new PsiElement [] {psiFile };
190221 }
191222
192- return new PsiElement [0 ];
223+ return psiFiles .toArray (new PsiFile [psiFiles .size ()]);
224+
225+ }
226+
227+ private static void addFileInsideTwigPath (Project project , String templatePath , Collection <PsiFile > psiFiles , TwigPath twigPath ) {
228+ String [] split = templatePath .split ("/" );
229+ VirtualFile virtualFile = VfsUtil .findRelativeFile (twigPath .getDirectory (project ), split );
230+ if (virtualFile != null ) {
231+ PsiFile psiFile = PsiManager .getInstance (project ).findFile (virtualFile );
232+ if (psiFile != null ) {
233+ psiFiles .add (psiFile );
234+ }
235+ }
193236 }
194237
195238 public static List <TwigPath > getTwigNamespaces (Project project ) {
0 commit comments