21
21
import java .util .Collection ;
22
22
import java .util .HashMap ;
23
23
import java .util .List ;
24
+ import java .util .Map ;
24
25
import org .jetbrains .annotations .NotNull ;
25
26
import org .jetbrains .annotations .Nullable ;
26
27
27
28
public class PluginTargetLineMarkerProvider implements LineMarkerProvider {
28
- @ Nullable
29
+
30
+ private static final String TARGET_CLASS_TOOLTIP_TEXT = "Navigate to target class" ;
31
+ private static final String TARGET_METHOD_TOOLTIP_TEXT = "Navigate to target method" ;
32
+
29
33
@ Override
30
- public LineMarkerInfo getLineMarkerInfo (@ NotNull final PsiElement psiElement ) {
34
+ public @ Nullable LineMarkerInfo <?> getLineMarkerInfo (final @ NotNull PsiElement psiElement ) {
31
35
return null ;
32
36
}
33
37
@@ -36,48 +40,52 @@ public void collectSlowLineMarkers(
36
40
final @ NotNull List <? extends PsiElement > psiElements ,
37
41
final @ NotNull Collection <? super LineMarkerInfo <?>> collection
38
42
) {
39
- if (!psiElements .isEmpty () && !Settings .isEnabled (psiElements .get (0 ).getProject ())) {
43
+ if (psiElements .isEmpty ()) {
44
+ return ;
45
+ }
46
+
47
+ if (!Settings .isEnabled (psiElements .get (0 ).getProject ())) {
40
48
return ;
41
49
}
42
50
final PluginClassCache pluginClassCache = new PluginClassCache ();
43
- final TargetClassesCollector targetClassesCollector =
44
- new TargetClassesCollector (pluginClassCache );
45
- final TargetMethodsCollector targetMethodsCollector =
46
- new TargetMethodsCollector (pluginClassCache );
51
+ final TargetClassesCollector targetClassesCollector = new TargetClassesCollector (
52
+ pluginClassCache
53
+ );
54
+ final TargetMethodsCollector targetMethodsCollector = new TargetMethodsCollector (
55
+ pluginClassCache
56
+ );
47
57
48
58
for (final PsiElement psiElement : psiElements ) {
49
- if (psiElement instanceof PhpClass || psiElement instanceof Method ) {
50
- List <? extends PsiElement > results ;
51
-
52
- if ( psiElement instanceof PhpClass ) {
53
- results = targetClassesCollector . collect (( PhpClass ) psiElement );
54
- if (! results . isEmpty ()) {
55
- collection . add ( NavigationGutterIconBuilder
56
- . create ( AllIcons . Nodes . Class )
57
- . setTargets ( results )
58
- . setTooltipText ( "Navigate to target class" )
59
- . createLineMarkerInfo ( PsiTreeUtil . getDeepestFirst ( psiElement ))
60
- );
61
- }
62
- } else {
63
- results = targetMethodsCollector .collect ((Method ) psiElement );
64
- if (! results . isEmpty ()) {
65
- collection . add ( NavigationGutterIconBuilder
66
- . create ( AllIcons . Nodes . Method )
67
- . setTargets ( results )
68
- . setTooltipText ( "Navigate to target method" )
69
- . createLineMarkerInfo ( PsiTreeUtil . getDeepestFirst ( psiElement ) )
70
- );
71
- }
59
+ if (psiElement instanceof PhpClass ) {
60
+ final List <? extends PsiElement > results =
61
+ targetClassesCollector . collect (( PhpClass ) psiElement );
62
+
63
+ if (! results . isEmpty ()) {
64
+ collection . add ( NavigationGutterIconBuilder
65
+ . create ( AllIcons . Nodes . Class )
66
+ . setTargets ( results )
67
+ . setTooltipText ( TARGET_CLASS_TOOLTIP_TEXT )
68
+ . createLineMarkerInfo ( PsiTreeUtil . getDeepestFirst ( psiElement ) )
69
+ );
70
+ }
71
+ } else if ( psiElement instanceof Method ) {
72
+ final List <? extends PsiElement > results =
73
+ targetMethodsCollector .collect ((Method ) psiElement );
74
+
75
+ if (! results . isEmpty ()) {
76
+ collection . add ( NavigationGutterIconBuilder
77
+ . create ( AllIcons . Nodes . Method )
78
+ . setTargets ( results )
79
+ . setTooltipText ( TARGET_METHOD_TOOLTIP_TEXT )
80
+ . createLineMarkerInfo ( PsiTreeUtil . getDeepestFirst ( psiElement ))
81
+ );
72
82
}
73
-
74
83
}
75
84
}
76
85
}
77
86
78
87
private static class PluginClassCache {
79
- private final HashMap <String , List <PhpClass >> pluginClassesMap = // NOPMD
80
- new HashMap <>();
88
+ private final Map <String , List <PhpClass >> pluginClassesMap = new HashMap <>();
81
89
82
90
private List <PhpClass > getTargetClassesForPlugin (
83
91
@ NotNull final PhpClass phpClass ,
@@ -127,9 +135,10 @@ protected List<PhpClass> getTargetClassesForPlugin(@NotNull final PhpClass phpCl
127
135
}
128
136
129
137
private static class TargetClassesCollector implements Collector <PhpClass , PhpClass > {
138
+
130
139
private final PluginTargetLineMarkerProvider .PluginClassCache pluginClassCache ;
131
140
132
- TargetClassesCollector (// NOPMD
141
+ public TargetClassesCollector (
133
142
final PluginTargetLineMarkerProvider .PluginClassCache pluginClassCache
134
143
) {
135
144
this .pluginClassCache = pluginClassCache ;
@@ -142,9 +151,10 @@ public List<PhpClass> collect(@NotNull final PhpClass psiElement) {
142
151
}
143
152
144
153
private static class TargetMethodsCollector implements Collector <Method , Method > {
154
+
145
155
private final PluginTargetLineMarkerProvider .PluginClassCache pluginClassCache ;
146
156
147
- TargetMethodsCollector (// NOPMD
157
+ public TargetMethodsCollector (
148
158
final PluginTargetLineMarkerProvider .PluginClassCache pluginClassCache
149
159
) {
150
160
this .pluginClassCache = pluginClassCache ;
@@ -158,32 +168,33 @@ public List<Method> collect(@NotNull final Method pluginMethod) {
158
168
if (null == getPluginPrefix (pluginMethod )) {
159
169
return results ;
160
170
}
161
-
162
171
final PhpClass pluginClass = pluginMethod .getContainingClass ();
172
+
163
173
if (pluginClass == null ) {
164
174
return results ;
165
175
}
166
176
167
177
final List <PhpClass > targetClasses
168
178
= pluginClassCache .getTargetClassesForPlugin (pluginClass );
179
+
169
180
if (targetClasses .isEmpty ()) {
170
181
return results ;
171
182
}
172
183
173
- for (final PhpClass targetClass : targetClasses ) {
184
+ for (final PhpClass targetClass : targetClasses ) {
174
185
final String pluginPrefix = getPluginPrefix (pluginMethod );
175
186
final String targetClassMethodName = getTargetMethodName (
176
187
pluginMethod , pluginPrefix
177
188
);
189
+
178
190
if (targetClassMethodName == null ) {
179
191
continue ;
180
192
}
181
-
182
193
final Method targetMethod = targetClass .findMethodByName (targetClassMethodName );
194
+
183
195
if (targetMethod == null ) {
184
196
continue ;
185
197
}
186
-
187
198
results .add (targetMethod );
188
199
}
189
200
@@ -192,25 +203,31 @@ public List<Method> collect(@NotNull final Method pluginMethod) {
192
203
193
204
private String getTargetMethodName (final Method pluginMethod , final String pluginPrefix ) {
194
205
final String targetClassMethodName = pluginMethod .getName ().replace (pluginPrefix , "" );
206
+
195
207
if (targetClassMethodName .isEmpty ()) {
196
208
return null ;
197
209
}
198
210
final char firstCharOfTargetName = targetClassMethodName .charAt (0 );
211
+
199
212
if (Character .getType (firstCharOfTargetName ) == Character .LOWERCASE_LETTER ) {
200
213
return null ;
201
214
}
215
+
202
216
return Character .toLowerCase (firstCharOfTargetName )
203
217
+ targetClassMethodName .substring (1 );
204
218
}
205
219
206
220
private String getPluginPrefix (final Method pluginMethod ) {
207
221
final String pluginMethodName = pluginMethod .getName ();
222
+
208
223
if (pluginMethodName .startsWith (Plugin .PluginType .around .toString ())) {
209
224
return Plugin .PluginType .around .toString ();
210
225
}
226
+
211
227
if (pluginMethodName .startsWith (Plugin .PluginType .before .toString ())) {
212
228
return Plugin .PluginType .before .toString ();
213
229
}
230
+
214
231
if (pluginMethodName .startsWith (Plugin .PluginType .after .toString ())) {
215
232
return Plugin .PluginType .after .toString ();
216
233
}
0 commit comments