|
1 | 1 | package com.cedricziel.idea.typo3.util; |
2 | 2 |
|
| 3 | +import com.intellij.openapi.project.Project; |
| 4 | +import com.intellij.patterns.PlatformPatterns; |
3 | 5 | 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; |
4 | 14 |
|
5 | | -import java.util.Arrays; |
6 | | -import java.util.HashSet; |
7 | | -import java.util.Set; |
| 15 | +import java.util.*; |
8 | 16 |
|
9 | 17 | import static com.cedricziel.idea.typo3.psi.PhpElementsUtil.extractArrayIndexFromValue; |
10 | 18 |
|
@@ -44,19 +52,83 @@ public class TCAUtil { |
44 | 52 | "textTable", |
45 | 53 | }; |
46 | 54 |
|
| 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 | + |
47 | 59 | public static boolean arrayIndexIsTCATableNameField(PsiElement element) { |
48 | 60 | String arrayIndex = extractArrayIndexFromValue(element); |
49 | 61 |
|
50 | 62 | return Arrays.asList(TCA_TABLE_FIELDS).contains(arrayIndex); |
51 | 63 | } |
52 | 64 |
|
53 | 65 | 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)); |
54 | 71 |
|
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; |
56 | 80 | } |
57 | 81 |
|
58 | 82 | public static Set<String> getAvailableColumnTypes(PsiElement element) { |
59 | 83 |
|
60 | 84 | return new HashSet<>(Arrays.asList(TCA_CORE_TYPES)); |
61 | 85 | } |
| 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 | + } |
62 | 134 | } |
0 commit comments