@@ -2288,103 +2288,109 @@ public static Map<VirtualFile, Collection<String>> getBlockNamesForFiles(@NotNul
22882288 /**
22892289 * Visit all possible Twig include file pattern
22902290 */
2291- public static void visitTemplateIncludes (@ NotNull TwigFile twigFile , @ NotNull Consumer <TemplateInclude > consumer ) {
2292- PsiTreeUtil .collectElements (twigFile , psiElement -> {
2293- if (psiElement instanceof TwigTagWithFileReference ) {
2294- // {% include %}
2295- if (psiElement .getNode ().getElementType () == TwigElementTypes .INCLUDE_TAG ) {
2296- for (String templateName : getIncludeTagStrings ((TwigTagWithFileReference ) psiElement )) {
2297- if (StringUtils .isNotBlank (templateName )) {
2298- consumer .consume (new TemplateInclude (psiElement , templateName , TemplateInclude .TYPE .INCLUDE ));
2299- }
2291+ public static void visitTemplateIncludes (@ NotNull PsiElement psiElement , @ NotNull Consumer <TemplateInclude > consumer ) {
2292+ if (psiElement instanceof TwigTagWithFileReference ) {
2293+ // {% include %}
2294+ if (psiElement .getNode ().getElementType () == TwigElementTypes .INCLUDE_TAG ) {
2295+ for (String templateName : getIncludeTagStrings ((TwigTagWithFileReference ) psiElement )) {
2296+ if (StringUtils .isNotBlank (templateName )) {
2297+ consumer .consume (new TemplateInclude (psiElement , templateName , TemplateInclude .TYPE .INCLUDE ));
23002298 }
23012299 }
2300+ }
23022301
2303- // {% import "foo.html.twig"
2304- PsiElement importTag = PsiElementUtils .getChildrenOfType (psiElement , TwigPattern .getTagNameParameterPattern (TwigElementTypes .IMPORT_TAG , "import" ));
2305- if (importTag != null ) {
2306- String templateName = importTag .getText ();
2307- if (StringUtils .isNotBlank (templateName )) {
2308- consumer .consume (new TemplateInclude (psiElement , templateName , TemplateInclude .TYPE .IMPORT ));
2309- }
2302+ // {% import "foo.html.twig"
2303+ PsiElement importTag = PsiElementUtils .getChildrenOfType (psiElement , TwigPattern .getTagNameParameterPattern (TwigElementTypes .IMPORT_TAG , "import" ));
2304+ if (importTag != null ) {
2305+ String templateName = importTag .getText ();
2306+ if (StringUtils .isNotBlank (templateName )) {
2307+ consumer .consume (new TemplateInclude (psiElement , templateName , TemplateInclude .TYPE .IMPORT ));
23102308 }
2309+ }
23112310
2312- // {% from 'forms.html' import ... %}
2313- PsiElement fromTag = PsiElementUtils .getChildrenOfType (psiElement , TwigPattern .getTagNameParameterPattern (TwigElementTypes .IMPORT_TAG , "from" ));
2314- if (fromTag != null ) {
2315- String templateName = fromTag .getText ();
2316- if (StringUtils .isNotBlank (templateName )) {
2317- consumer .consume (new TemplateInclude (psiElement , templateName , TemplateInclude .TYPE .IMPORT ));
2318- }
2311+ // {% from 'forms.html' import ... %}
2312+ PsiElement fromTag = PsiElementUtils .getChildrenOfType (psiElement , TwigPattern .getTagNameParameterPattern (TwigElementTypes .IMPORT_TAG , "from" ));
2313+ if (fromTag != null ) {
2314+ String templateName = fromTag .getText ();
2315+ if (StringUtils .isNotBlank (templateName )) {
2316+ consumer .consume (new TemplateInclude (psiElement , templateName , TemplateInclude .TYPE .IMPORT ));
23192317 }
2320- } else if ( psiElement instanceof TwigCompositeElement ) {
2321- // {{ include() }}
2322- // {{ source () }}
2323- PsiElement includeTag = PsiElementUtils . getChildrenOfType ( psiElement , TwigPattern . getPrintBlockOrTagFunctionPattern ( "include" , " source" ));
2324- if ( includeTag != null ) {
2325- String templateName = includeTag . getText ();
2326- if ( StringUtils . isNotBlank ( templateName )) {
2327- consumer . consume ( new TemplateInclude ( psiElement , templateName , TemplateInclude . TYPE . INCLUDE_FUNCTION ));
2328- }
2318+ }
2319+ } else if ( psiElement instanceof TwigCompositeElement ) {
2320+ // {{ include () }}
2321+ // {{ source() }}
2322+ PsiElement includeTag = PsiElementUtils . getChildrenOfType ( psiElement , TwigPattern . getPrintBlockOrTagFunctionPattern ( "include" , "source" ));
2323+ if ( includeTag != null ) {
2324+ String templateName = includeTag . getText ();
2325+ if ( StringUtils . isNotBlank ( templateName )) {
2326+ consumer . consume ( new TemplateInclude ( psiElement , templateName , TemplateInclude . TYPE . INCLUDE_FUNCTION ));
23292327 }
2328+ }
23302329
2331- // {% embed "foo.html.twig"
2332- PsiElement embedTag = PsiElementUtils .getChildrenOfType (psiElement , TwigPattern .getEmbedPattern ());
2333- if (embedTag != null ) {
2334- String templateName = embedTag .getText ();
2335- if (StringUtils .isNotBlank (templateName )) {
2336- consumer .consume (new TemplateInclude (psiElement , templateName , TemplateInclude .TYPE .EMBED ));
2337- }
2330+ // {% embed "foo.html.twig"
2331+ PsiElement embedTag = PsiElementUtils .getChildrenOfType (psiElement , TwigPattern .getEmbedPattern ());
2332+ if (embedTag != null ) {
2333+ String templateName = embedTag .getText ();
2334+ if (StringUtils .isNotBlank (templateName )) {
2335+ consumer .consume (new TemplateInclude (psiElement , templateName , TemplateInclude .TYPE .EMBED ));
23382336 }
2337+ }
23392338
2340- if (psiElement .getNode ().getElementType () == TwigElementTypes .TAG ) {
2341- PsiElement tagElement = PsiElementUtils .getChildrenOfType (psiElement , PlatformPatterns .psiElement ().withElementType (TwigTokenTypes .TAG_NAME ));
2342- if (tagElement != null ) {
2343- String text = tagElement .getText ();
2344- if ("form_theme" .equals (text )) {
2345- // {% form_theme form.child 'form/fields_child.html.twig' %}
2346- PsiElement childrenOfType = PsiElementUtils .getNextSiblingAndSkip (tagElement , TwigTokenTypes .STRING_TEXT ,
2347- TwigTokenTypes .IDENTIFIER , TwigTokenTypes .SINGLE_QUOTE , TwigTokenTypes .DOUBLE_QUOTE , TwigTokenTypes .DOT
2348- );
2339+ if (psiElement .getNode ().getElementType () == TwigElementTypes .TAG ) {
2340+ PsiElement tagElement = PsiElementUtils .getChildrenOfType (psiElement , PlatformPatterns .psiElement ().withElementType (TwigTokenTypes .TAG_NAME ));
2341+ if (tagElement != null ) {
2342+ String text = tagElement .getText ();
2343+ if ("form_theme" .equals (text )) {
2344+ // {% form_theme form.child 'form/fields_child.html.twig' %}
2345+ PsiElement childrenOfType = PsiElementUtils .getNextSiblingAndSkip (tagElement , TwigTokenTypes .STRING_TEXT ,
2346+ TwigTokenTypes .IDENTIFIER , TwigTokenTypes .SINGLE_QUOTE , TwigTokenTypes .DOUBLE_QUOTE , TwigTokenTypes .DOT
2347+ );
23492348
2350- if (childrenOfType != null ) {
2351- String templateName = childrenOfType .getText ();
2352- if (StringUtils .isNotBlank (templateName )) {
2353- consumer .consume (new TemplateInclude (psiElement , templateName , TemplateInclude .TYPE .FORM_THEME ));
2354- }
2349+ if (childrenOfType != null ) {
2350+ String templateName = childrenOfType .getText ();
2351+ if (StringUtils .isNotBlank (templateName )) {
2352+ consumer .consume (new TemplateInclude (psiElement , templateName , TemplateInclude .TYPE .FORM_THEME ));
23552353 }
2354+ }
23562355
2357- // {% form_theme form.child with ['form/fields_child.html.twig'] %}
2358- PsiElement withElement = PsiElementUtils .getNextSiblingOfType (tagElement , PlatformPatterns .psiElement ().withElementType (TwigTokenTypes .IDENTIFIER ).withText ("with" ));
2359- if (withElement != null ) {
2360- // find LITERAL "[", "{"
2361- PsiElement arrayStart = PsiElementUtils .getNextSiblingAndSkip (tagElement , TwigElementTypes .LITERAL ,
2362- TwigTokenTypes .IDENTIFIER , TwigTokenTypes .SINGLE_QUOTE , TwigTokenTypes .DOUBLE_QUOTE , TwigTokenTypes .DOT
2363- );
2364-
2365- if (arrayStart != null ) {
2366- PsiElement firstChild = arrayStart .getFirstChild ();
2367- if (firstChild != null ) {
2368- visitStringInArray (firstChild , pair ->
2369- consumer .consume (new TemplateInclude (psiElement , pair .getFirst (), TemplateInclude .TYPE .FORM_THEME ))
2370- );
2371- }
2356+ // {% form_theme form.child with ['form/fields_child.html.twig'] %}
2357+ PsiElement withElement = PsiElementUtils .getNextSiblingOfType (tagElement , PlatformPatterns .psiElement ().withElementType (TwigTokenTypes .IDENTIFIER ).withText ("with" ));
2358+ if (withElement != null ) {
2359+ // find LITERAL "[", "{"
2360+ PsiElement arrayStart = PsiElementUtils .getNextSiblingAndSkip (tagElement , TwigElementTypes .LITERAL ,
2361+ TwigTokenTypes .IDENTIFIER , TwigTokenTypes .SINGLE_QUOTE , TwigTokenTypes .DOUBLE_QUOTE , TwigTokenTypes .DOT
2362+ );
2363+
2364+ if (arrayStart != null ) {
2365+ PsiElement firstChild = arrayStart .getFirstChild ();
2366+ if (firstChild != null ) {
2367+ visitStringInArray (firstChild , pair ->
2368+ consumer .consume (new TemplateInclude (psiElement , pair .getFirst (), TemplateInclude .TYPE .FORM_THEME ))
2369+ );
23722370 }
23732371 }
23742372 }
23752373 }
23762374 }
2375+ }
23772376
2378- for (TwigFileUsage extension : TWIG_FILE_USAGE_EXTENSIONS .getExtensions ()) {
2379- if (extension .isIncludeTemplate (psiElement )) {
2380- for (String template : extension .getIncludeTemplate (psiElement )) {
2381- consumer .consume (new TemplateInclude (psiElement , template , TemplateInclude .TYPE .INCLUDE ));
2382- }
2377+ for (TwigFileUsage extension : TWIG_FILE_USAGE_EXTENSIONS .getExtensions ()) {
2378+ if (extension .isIncludeTemplate (psiElement )) {
2379+ for (String template : extension .getIncludeTemplate (psiElement )) {
2380+ consumer .consume (new TemplateInclude (psiElement , template , TemplateInclude .TYPE .INCLUDE ));
23832381 }
23842382 }
23852383 }
2384+ }
2385+ }
23862386
2387- return false ;
2387+ /**
2388+ * Visit all possible Twig include file pattern
2389+ */
2390+ public static void visitTemplateIncludes (@ NotNull TwigFile twigFile , @ NotNull Consumer <TemplateInclude > consumer ) {
2391+ PsiTreeUtil .collectElements (twigFile , psiElement -> {
2392+ visitTemplateIncludes (psiElement , consumer );
2393+ return true ;
23882394 });
23892395 }
23902396
0 commit comments