|
| 1 | +package fr.adrienbrault.idea.symfony2plugin.templating.util; |
| 2 | + |
| 3 | +import com.intellij.openapi.util.Condition; |
| 4 | +import com.intellij.patterns.PlatformPatterns; |
| 5 | +import com.intellij.psi.PsiComment; |
| 6 | +import com.intellij.psi.PsiElement; |
| 7 | +import com.intellij.psi.PsiWhiteSpace; |
| 8 | +import com.intellij.psi.util.PsiTreeUtil; |
| 9 | +import com.jetbrains.php.lang.psi.elements.Method; |
| 10 | +import com.jetbrains.php.lang.psi.elements.PhpClass; |
| 11 | +import com.jetbrains.php.lang.psi.elements.PhpNamedElement; |
| 12 | +import com.jetbrains.php.lang.psi.resolve.types.PhpType; |
| 13 | +import com.jetbrains.twig.elements.TwigCompositeElement; |
| 14 | +import com.jetbrains.twig.elements.TwigElementTypes; |
| 15 | +import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil; |
| 16 | + |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.Collection; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.regex.Matcher; |
| 21 | +import java.util.regex.Pattern; |
| 22 | + |
| 23 | +public class TwigTypeResolveUtil { |
| 24 | + |
| 25 | + public static String[] formatPsiTypeName(PsiElement psiElement) { |
| 26 | + |
| 27 | + String typeNames = PhpElementsUtil.getPrevSiblingAsTextUntil(psiElement, PlatformPatterns.psiElement(PsiWhiteSpace.class)).trim(); |
| 28 | + if(typeNames.endsWith(".")) { |
| 29 | + typeNames = typeNames.substring(0, typeNames.length() -1); |
| 30 | + } |
| 31 | + |
| 32 | + String[] possibleTypes; |
| 33 | + if(typeNames.contains(".")) { |
| 34 | + possibleTypes = typeNames.split("\\."); |
| 35 | + } else { |
| 36 | + possibleTypes = new String[]{typeNames}; |
| 37 | + } |
| 38 | + |
| 39 | + return possibleTypes; |
| 40 | + } |
| 41 | + |
| 42 | + public static Collection<? extends PhpNamedElement> resolveTwigMethodName(PsiElement psiElement, String[] typeName) { |
| 43 | + |
| 44 | + if(typeName.length == 0) { |
| 45 | + return null; |
| 46 | + } |
| 47 | + |
| 48 | + Collection<? extends PhpNamedElement> rootVariable = getRootVariableByName(psiElement, typeName[0]); |
| 49 | + if(typeName.length == 1) { |
| 50 | + return rootVariable; |
| 51 | + } |
| 52 | + |
| 53 | + Collection<? extends PhpNamedElement> type = rootVariable; |
| 54 | + for (int i = 1; i <= typeName.length - 1; i++ ) { |
| 55 | + type = resolveTwigMethodName(type, typeName[i]); |
| 56 | + } |
| 57 | + |
| 58 | + return type; |
| 59 | + } |
| 60 | + |
| 61 | + private static Collection<? extends PhpNamedElement> findInlineDocBlockVariableByName(PsiElement psiInsideBlock, String variableName) { |
| 62 | + |
| 63 | + PsiElement twigCompositeElement = PsiTreeUtil.findFirstParent(psiInsideBlock, new Condition<PsiElement>() { |
| 64 | + @Override |
| 65 | + public boolean value(PsiElement psiElement) { |
| 66 | + if (psiElement instanceof TwigCompositeElement) { |
| 67 | + if (PlatformPatterns.psiElement(TwigElementTypes.BLOCK_STATEMENT).accepts(psiElement)) { |
| 68 | + return true; |
| 69 | + } |
| 70 | + } |
| 71 | + return false; |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + |
| 76 | + Pattern pattern = Pattern.compile("\\{#[\\s]+" + Pattern.quote(variableName) + "[\\s]+(.*)[\\s]+#}"); |
| 77 | + Collection<PsiComment> psiComments = PsiTreeUtil.findChildrenOfType(twigCompositeElement, PsiComment.class); |
| 78 | + |
| 79 | + ArrayList<PhpNamedElement> arrayList = new ArrayList<PhpNamedElement>(); |
| 80 | + for(PsiComment psiComment: psiComments) { |
| 81 | + Matcher matcher = pattern.matcher(psiComment.getText()); |
| 82 | + if (matcher.find()) { |
| 83 | + |
| 84 | + // think of multi resolve |
| 85 | + PhpClass phpClass = PhpElementsUtil.getClass(psiComment.getProject(), matcher.group(1)); |
| 86 | + if(phpClass != null) { |
| 87 | + arrayList.add(phpClass); |
| 88 | + return arrayList; |
| 89 | + } |
| 90 | + |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + private static Collection<? extends PhpNamedElement> getRootVariableByName(PsiElement psiElement, String variableName) { |
| 98 | + |
| 99 | + HashMap<String, String> globalVars = new HashMap<String, String>(); |
| 100 | + globalVars.put("app", "\\Symfony\\Bundle\\FrameworkBundle\\Templating\\GlobalVariables"); |
| 101 | + |
| 102 | + ArrayList<PhpNamedElement> phpNamedElements = new ArrayList<PhpNamedElement>(); |
| 103 | + |
| 104 | + // parameter prio? |
| 105 | + if(globalVars.containsKey(variableName)) { |
| 106 | + PhpClass phpClass = PhpElementsUtil.getClass(psiElement.getProject(), globalVars.get(variableName)); |
| 107 | + if(phpClass != null) { |
| 108 | + phpNamedElements.add(phpClass); |
| 109 | + return phpNamedElements; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + return findInlineDocBlockVariableByName(psiElement, variableName); |
| 115 | + } |
| 116 | + |
| 117 | + private static Collection<? extends PhpNamedElement> resolveTwigMethodName(Collection<? extends PhpNamedElement> previousElement, String typeName) { |
| 118 | + |
| 119 | + ArrayList<PhpNamedElement> phpNamedElements = new ArrayList<PhpNamedElement>(); |
| 120 | + |
| 121 | + for(PhpNamedElement phpNamedElement: previousElement) { |
| 122 | + if(phpNamedElement instanceof PhpClass) { |
| 123 | + for(Method method: ((PhpClass) phpNamedElement).getMethods()) { |
| 124 | + if(method.getName().toLowerCase().equals("get" + typeName.toLowerCase())) { |
| 125 | + |
| 126 | + PhpType phpType = method.getType(); |
| 127 | + for(String typeString: phpType.getTypes()) { |
| 128 | + PhpNamedElement phpNamedElement1 = PhpElementsUtil.getClassInterface(phpNamedElement.getProject(), typeString); |
| 129 | + if(phpNamedElement1 != null) { |
| 130 | + phpNamedElements.add(phpNamedElement1); |
| 131 | + } |
| 132 | + |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + return phpNamedElements; |
| 140 | + } |
| 141 | +} |
0 commit comments