Skip to content

Commit 7c82420

Browse files
authored
Merge pull request #63 from cedricziel/dynamic-render-types
Scrape available renderTypes from NodeRegistry
2 parents d85cd04 + d6ba33e commit 7c82420

File tree

1 file changed

+76
-4
lines changed

1 file changed

+76
-4
lines changed

src/main/java/com/cedricziel/idea/typo3/util/TCAUtil.java

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
package com.cedricziel.idea.typo3.util;
22

3+
import com.intellij.openapi.project.Project;
4+
import com.intellij.patterns.PlatformPatterns;
35
import com.intellij.psi.PsiElement;
6+
import com.intellij.psi.PsiFile;
7+
import com.intellij.psi.search.FilenameIndex;
8+
import com.intellij.psi.search.GlobalSearchScope;
9+
import com.intellij.psi.util.PsiTreeUtil;
10+
import com.jetbrains.php.PhpIndex;
11+
import com.jetbrains.php.lang.parser.PhpElementTypes;
12+
import com.jetbrains.php.lang.psi.elements.PhpClass;
13+
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
414

5-
import java.util.Arrays;
6-
import java.util.HashSet;
7-
import java.util.Set;
15+
import java.util.*;
816

917
import static com.cedricziel.idea.typo3.psi.PhpElementsUtil.extractArrayIndexFromValue;
1018

@@ -44,19 +52,83 @@ public class TCAUtil {
4452
"textTable",
4553
};
4654

55+
private static final String EXT_LOCALCONF_FILENAME = "ext_localconf.php";
56+
57+
private static final String NODE_FACTORY_CLASS = "TYPO3\\CMS\\Backend\\Form\\NodeFactory";
58+
4759
public static boolean arrayIndexIsTCATableNameField(PsiElement element) {
4860
String arrayIndex = extractArrayIndexFromValue(element);
4961

5062
return Arrays.asList(TCA_TABLE_FIELDS).contains(arrayIndex);
5163
}
5264

5365
public static Set<String> getAvailableRenderTypes(PsiElement element) {
66+
Set<String> renderTypes = new HashSet<>();
67+
Set<PsiElement> elementsFromExtLocalConf = findAvailableRenderTypes(element.getProject());
68+
69+
// add static list of render types
70+
renderTypes.addAll(Arrays.asList(TCA_CORE_RENDER_TYPES));
5471

55-
return new HashSet<>(Arrays.asList(TCA_CORE_RENDER_TYPES));
72+
// add dynamic list of render types from nodeRegistry
73+
for (PsiElement el : elementsFromExtLocalConf) {
74+
if (el instanceof StringLiteralExpression) {
75+
renderTypes.add(((StringLiteralExpression) el).getContents());
76+
}
77+
}
78+
79+
return renderTypes;
5680
}
5781

5882
public static Set<String> getAvailableColumnTypes(PsiElement element) {
5983

6084
return new HashSet<>(Arrays.asList(TCA_CORE_TYPES));
6185
}
86+
87+
private static Set<PsiElement> findAvailableRenderTypes(Project project) {
88+
PhpIndex phpIndex = PhpIndex.getInstance(project);
89+
PsiFile[] extLocalConfFiles = FilenameIndex.getFilesByName(project, EXT_LOCALCONF_FILENAME, GlobalSearchScope.allScope(project));
90+
Collection<PhpClass> nodeRegistries = phpIndex.getClassesByFQN(NODE_FACTORY_CLASS);
91+
92+
Set<PsiElement> elements = new HashSet<>();
93+
94+
for (PhpClass registry : nodeRegistries) {
95+
Collections.addAll(
96+
elements,
97+
PsiTreeUtil.collectElements(registry, element -> PlatformPatterns
98+
.psiElement(StringLiteralExpression.class)
99+
.withParent(
100+
PlatformPatterns.psiElement(PhpElementTypes.ARRAY_KEY)
101+
.withAncestor(
102+
3,
103+
PlatformPatterns.psiElement(PhpElementTypes.CLASS_FIELD).withName("nodeTypes")
104+
)
105+
)
106+
.accepts(element)
107+
)
108+
);
109+
}
110+
111+
for (PsiFile file : extLocalConfFiles) {
112+
Collections.addAll(
113+
elements,
114+
PsiTreeUtil.collectElements(file, element -> PlatformPatterns
115+
.psiElement(StringLiteralExpression.class)
116+
.withParent(
117+
PlatformPatterns
118+
.psiElement(PhpElementTypes.ARRAY_VALUE)
119+
.withParent(
120+
PlatformPatterns
121+
.psiElement(PhpElementTypes.HASH_ARRAY_ELEMENT)
122+
.withChild(
123+
PlatformPatterns
124+
.psiElement(PhpElementTypes.ARRAY_KEY)
125+
.withText("'nodeName'")
126+
)
127+
)
128+
).accepts(element))
129+
);
130+
}
131+
132+
return elements;
133+
}
62134
}

0 commit comments

Comments
 (0)