-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathAnnotationGoToDeclarationHandler.java
More file actions
184 lines (155 loc) · 6.67 KB
/
Copy pathAnnotationGoToDeclarationHandler.java
File metadata and controls
184 lines (155 loc) · 6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package de.espend.idea.php.annotation.navigation;
import com.intellij.codeInsight.navigation.actions.GotoDeclarationHandler;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Editor;
import com.intellij.patterns.ElementPattern;
import com.intellij.patterns.PlatformPatterns;
import com.intellij.patterns.StandardPatterns;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.php.lang.PhpLanguage;
import com.jetbrains.php.lang.documentation.phpdoc.lexer.PhpDocTokenTypes;
import com.jetbrains.php.lang.documentation.phpdoc.parser.PhpDocElementTypes;
import com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocTag;
import com.jetbrains.php.lang.psi.elements.Field;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import de.espend.idea.php.annotation.extension.PhpAnnotationDocTagGotoHandler;
import de.espend.idea.php.annotation.extension.PhpAnnotationVirtualProperties;
import de.espend.idea.php.annotation.extension.parameter.AnnotationDocTagGotoHandlerParameter;
import de.espend.idea.php.annotation.extension.parameter.AnnotationVirtualPropertyTargetsParameter;
import de.espend.idea.php.annotation.pattern.AnnotationPattern;
import de.espend.idea.php.annotation.util.AnnotationUtil;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
/**
* @author Daniel Espendiller <daniel@espendiller.net>
*/
public class AnnotationGoToDeclarationHandler implements GotoDeclarationHandler {
// <@Test>, <@Test\Test>
private static final ElementPattern<PsiElement> DOC_TAG_NAME_PATTERN =
PlatformPatterns.psiElement(PhpDocElementTypes.DOC_TAG_NAME).withText(StandardPatterns.string().startsWith("@")).withLanguage(PhpLanguage.INSTANCE);
// @Route(name=<ClassName>::FOO)
private static final ElementPattern<PsiElement> DOC_IDENTIFIER_BEFORE_STATIC_PATTERN =
PlatformPatterns.psiElement(PhpDocTokenTypes.DOC_IDENTIFIER).beforeLeaf(AnnotationPattern.getDocStaticPattern()).withLanguage(PhpLanguage.INSTANCE);
@Nullable
@Override
public PsiElement[] getGotoDeclarationTargets(PsiElement psiElement, int i, Editor editor) {
// @Test(<foo>=)
List<PsiElement> psiElements = new ArrayList<>();
if(AnnotationPattern.getDocAttribute().accepts(psiElement)) {
this.addPropertyGoto(psiElement, psiElements);
}
// <@Test>
// <@Test\Test>
if (DOC_TAG_NAME_PATTERN.accepts(psiElement)) {
this.addDocTagNameGoto(psiElement, psiElements);
}
// @Route(name=<ClassName>::FOO)
if (DOC_IDENTIFIER_BEFORE_STATIC_PATTERN.accepts(psiElement)) {
this.addStaticClassTargets(psiElement, psiElements);
}
// @Route(name=ClassName::<FOO>)
if (AnnotationPattern.getClassConstant().accepts(psiElement)) {
this.addStaticClassConstTargets(psiElement, psiElements);
}
return psiElements.toArray(new PsiElement[0]);
}
/**
* Add goto for DocTag itself which should be the PhpClass and provide Extension
*
* @param psiElement origin DOC_TAG_NAME psi element
* @param targets Goto targets
*/
private void addDocTagNameGoto(PsiElement psiElement, List<PsiElement> targets) {
PsiElement phpDocTagValue = psiElement.getContext();
if(!(phpDocTagValue instanceof PhpDocTag)) {
return;
}
PhpClass phpClass = AnnotationUtil.getAnnotationReference((PhpDocTag) phpDocTagValue);
if(phpClass == null) {
return;
}
targets.add(phpClass);
AnnotationDocTagGotoHandlerParameter parameter = new AnnotationDocTagGotoHandlerParameter((PhpDocTag) phpDocTagValue, phpClass, targets);
for(PhpAnnotationDocTagGotoHandler phpAnnotationExtension : AnnotationUtil.EP_DOC_TAG_GOTO.getExtensions()) {
phpAnnotationExtension.getGotoDeclarationTargets(parameter);
}
}
/**
* Add goto for property value which are Fields inside PhpClass
*
* @param psiElement origin DOC_IDENTIFIER psi element
* @param targets Goto targets
*/
private void addPropertyGoto(PsiElement psiElement, List<PsiElement> targets) {
PhpDocTag phpDocTag = PsiTreeUtil.getParentOfType(psiElement, PhpDocTag.class);
if(phpDocTag == null) {
return;
}
PhpClass phpClass = AnnotationUtil.getAnnotationReference(phpDocTag);
if(phpClass == null) {
return;
}
String property = psiElement.getText();
if(StringUtils.isBlank(property)) {
return;
}
AnnotationUtil.visitAttributes(phpClass, (attributeName, type, target) -> {
if(attributeName.equals(property)) {
targets.add(target);
}
return null;
});
// extension point to provide virtual properties / fields targets
AnnotationVirtualPropertyTargetsParameter parameter = null;
for (PhpAnnotationVirtualProperties ep : AnnotationUtil.EP_VIRTUAL_PROPERTIES.getExtensions()) {
if(parameter == null) {
parameter = new AnnotationVirtualPropertyTargetsParameter(phpClass, psiElement, property);
}
ep.getTargets(parameter);
}
if(parameter != null) {
targets.addAll(parameter.getTargets());
}
}
/**
* Add class targets @Route(name=<ClassName>::FOO)
*
* @param psiElement DOC_IDENTIFIER
* @param targets Goto targets
*/
private void addStaticClassTargets(PsiElement psiElement, List<PsiElement> targets) {
PhpClass phpClass = AnnotationUtil.getClassFromDocIdentifier(psiElement);
if(phpClass != null) {
targets.add(phpClass);
}
}
/**
* Add static field targets for @Route(name=ClassName::<FOO>)
*
* @param psiElement DOC_IDENTIFIER after DOC_STATIC
* @param targets Goto targets
*/
private void addStaticClassConstTargets(PsiElement psiElement, List<PsiElement> targets) {
PhpClass phpClass = AnnotationUtil.getClassFromConstant(psiElement);
if(phpClass != null) {
String constName = psiElement.getText();
if ("class".equals(constName)) {
targets.add(phpClass);
} else {
Field field = phpClass.findFieldByName(constName, true);
if(field != null) {
targets.add(field);
}
}
}
}
@Nullable
@Override
public String getActionText(@NotNull DataContext context) {
return null;
}
}