|
4 | 4 | import com.intellij.patterns.PlatformPatterns;
|
5 | 5 | import com.intellij.psi.PsiComment;
|
6 | 6 | import com.intellij.psi.PsiElement;
|
| 7 | +import com.intellij.psi.PsiFile; |
7 | 8 | import com.intellij.psi.PsiWhiteSpace;
|
8 | 9 | import com.intellij.psi.util.PsiTreeUtil;
|
9 | 10 | import com.jetbrains.php.lang.psi.elements.Method;
|
10 | 11 | import com.jetbrains.php.lang.psi.elements.PhpClass;
|
11 | 12 | import com.jetbrains.php.lang.psi.elements.PhpNamedElement;
|
12 | 13 | import com.jetbrains.php.lang.psi.resolve.types.PhpType;
|
| 14 | +import com.jetbrains.twig.TwigFile; |
13 | 15 | import com.jetbrains.twig.elements.TwigCompositeElement;
|
14 | 16 | import com.jetbrains.twig.elements.TwigElementTypes;
|
| 17 | +import fr.adrienbrault.idea.symfony2plugin.TwigHelper; |
15 | 18 | import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
|
| 19 | +import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils; |
| 20 | +import fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper; |
16 | 21 |
|
17 | 22 | import java.util.ArrayList;
|
18 | 23 | import java.util.Collection;
|
@@ -43,7 +48,7 @@ public static String[] formatPsiTypeName(PsiElement psiElement) {
|
43 | 48 | public static Collection<? extends PhpNamedElement> resolveTwigMethodName(PsiElement psiElement, String[] typeName) {
|
44 | 49 |
|
45 | 50 | if(typeName.length == 0) {
|
46 |
| - return null; |
| 51 | + return Collections.emptyList(); |
47 | 52 | }
|
48 | 53 |
|
49 | 54 | Collection<? extends PhpNamedElement> rootVariable = getRootVariableByName(psiElement, typeName[0]);
|
@@ -101,13 +106,80 @@ public boolean value(PsiElement psiElement) {
|
101 | 106 | return null;
|
102 | 107 | }
|
103 | 108 |
|
| 109 | + /** |
| 110 | + * duplicate use a collector interface |
| 111 | + */ |
| 112 | + private static HashMap<String, String> findInlineVariableDocBlock(PsiElement psiInsideBlock) { |
| 113 | + |
| 114 | + PsiElement twigCompositeElement = PsiTreeUtil.findFirstParent(psiInsideBlock, new Condition<PsiElement>() { |
| 115 | + @Override |
| 116 | + public boolean value(PsiElement psiElement) { |
| 117 | + if (psiElement instanceof TwigCompositeElement) { |
| 118 | + if (PlatformPatterns.psiElement(TwigElementTypes.BLOCK_STATEMENT).accepts(psiElement)) { |
| 119 | + return true; |
| 120 | + } |
| 121 | + } |
| 122 | + return false; |
| 123 | + } |
| 124 | + }); |
| 125 | + |
| 126 | + Pattern pattern = Pattern.compile("\\{#[\\s]+([\\w]+)[\\s]+(.*)[\\s]+#}"); |
| 127 | + |
| 128 | + // wtf in completion { | } root we have no comments in child context !? |
| 129 | + HashMap<String, String> variables = new HashMap<String, String>(); |
| 130 | + for(PsiElement psiComment: YamlHelper.getChildrenFix(twigCompositeElement)) { |
| 131 | + if(psiComment instanceof PsiComment) { |
| 132 | + Matcher matcher = pattern.matcher(psiComment.getText()); |
| 133 | + if (matcher.find()) { |
| 134 | + variables.put(matcher.group(1), matcher.group(2)); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + return variables; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * duplicate use a collector interface |
| 144 | + */ |
| 145 | + private static HashMap<String, String> findFileVariableDocBlock(TwigFile twigFile) { |
| 146 | + |
| 147 | + Pattern pattern = Pattern.compile("\\{#[\\s]+([\\w]+)[\\s]+(.*)[\\s]+#}"); |
| 148 | + |
| 149 | + // wtf in completion { | } root we have no comments in child context !? |
| 150 | + HashMap<String, String> variables = new HashMap<String, String>(); |
| 151 | + for(PsiElement psiComment: YamlHelper.getChildrenFix(twigFile)) { |
| 152 | + if(psiComment instanceof PsiComment) { |
| 153 | + Matcher matcher = pattern.matcher(psiComment.getText()); |
| 154 | + if (matcher.find()) { |
| 155 | + variables.put(matcher.group(1), matcher.group(2)); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + return variables; |
| 161 | + } |
| 162 | + |
| 163 | + public static HashMap<String, String> collectorRootScopeVariables(PsiElement psiElement) { |
| 164 | + |
| 165 | + HashMap<String, String> globalVars = new HashMap<String, String>(); |
| 166 | + globalVars.put("app", "\\Symfony\\Bundle\\FrameworkBundle\\Templating\\GlobalVariables"); |
| 167 | + |
| 168 | + globalVars.putAll(findInlineVariableDocBlock(psiElement)); |
| 169 | + |
| 170 | + globalVars.putAll(findFileVariableDocBlock((TwigFile) psiElement.getContainingFile())); |
| 171 | + |
| 172 | + return globalVars; |
| 173 | + } |
| 174 | + |
104 | 175 | private static Collection<? extends PhpNamedElement> getRootVariableByName(PsiElement psiElement, String variableName) {
|
105 | 176 |
|
106 | 177 | HashMap<String, String> globalVars = new HashMap<String, String>();
|
| 178 | + |
107 | 179 | globalVars.put("app", "\\Symfony\\Bundle\\FrameworkBundle\\Templating\\GlobalVariables");
|
| 180 | + globalVars.putAll(findFileVariableDocBlock((TwigFile) psiElement.getContainingFile())); |
108 | 181 |
|
109 | 182 | ArrayList<PhpNamedElement> phpNamedElements = new ArrayList<PhpNamedElement>();
|
110 |
| - |
111 | 183 | // parameter prio?
|
112 | 184 | if(globalVars.containsKey(variableName)) {
|
113 | 185 | PhpClass phpClass = PhpElementsUtil.getClass(psiElement.getProject(), globalVars.get(variableName));
|
|
0 commit comments