Skip to content

Commit a62a9dd

Browse files
committed
add twig constants navigation Haehnchen#327
1 parent 7129ba9 commit a62a9dd

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/fr/adrienbrault/idea/symfony2plugin/templating/TwigTemplateGoToLocalDeclarationHandler.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import com.intellij.psi.PsiElement;
88
import com.intellij.psi.PsiFile;
99
import com.intellij.psi.util.PsiTreeUtil;
10+
import com.jetbrains.php.lang.psi.elements.Field;
1011
import com.jetbrains.php.lang.psi.elements.Method;
12+
import com.jetbrains.php.lang.psi.elements.PhpClass;
1113
import com.jetbrains.twig.TwigLanguage;
1214
import com.jetbrains.twig.TwigTokenTypes;
1315
import com.jetbrains.twig.elements.TwigBlockTag;
@@ -26,6 +28,7 @@
2628
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
2729
import fr.adrienbrault.idea.symfony2plugin.util.RegexPsiElementFilter;
2830
import fr.adrienbrault.idea.symfony2plugin.util.controller.ControllerIndex;
31+
import org.apache.commons.lang.StringUtils;
2932
import org.jetbrains.annotations.Nullable;
3033

3134
import java.util.ArrayList;
@@ -95,9 +98,41 @@ public PsiElement[] getGotoDeclarationTargets(PsiElement psiElement, int i, Edit
9598
psiElements.addAll(Arrays.asList(this.getParentGoto(psiElement)));
9699
}
97100

101+
// constant('Post::PUBLISHED')
102+
if(TwigHelper.getPrintBlockFunctionPattern("constant").accepts(psiElement)) {
103+
psiElements.addAll(this.getConstantGoto(psiElement));
104+
}
105+
98106
return psiElements.toArray(new PsiElement[psiElements.size()]);
99107
}
100108

109+
private Collection<PsiElement> getConstantGoto(PsiElement psiElement) {
110+
111+
Collection<PsiElement> targetPsiElements = new ArrayList<PsiElement>();
112+
113+
String contents = psiElement.getText();
114+
if(StringUtils.isBlank(contents) || !contents.contains(":")) {
115+
return targetPsiElements;
116+
}
117+
118+
String[] parts = contents.split("::");
119+
if(parts.length != 2) {
120+
return targetPsiElements;
121+
}
122+
123+
PhpClass phpClass = PhpElementsUtil.getClassInterface(psiElement.getProject(), parts[0]);
124+
if(phpClass == null) {
125+
return targetPsiElements;
126+
}
127+
128+
Field field = phpClass.findFieldByName(parts[1], true);
129+
if(field != null) {
130+
targetPsiElements.add(field);
131+
}
132+
133+
return targetPsiElements;
134+
}
135+
101136
private PsiElement[] getTypeGoto(PsiElement psiElement) {
102137

103138
ArrayList<PsiElement> targetPsiElements = new ArrayList<PsiElement>();

0 commit comments

Comments
 (0)