Skip to content

Commit 7f2b6bb

Browse files
author
Vitaliy
authored
Merge pull request #359 from drpayyne/issue-322-backport
Added linemarker for interface plugin target (backport)
2 parents 98d80df + 3140cca commit 7f2b6bb

File tree

1 file changed

+77
-54
lines changed

1 file changed

+77
-54
lines changed

src/com/magento/idea/magento2plugin/linemarker/php/PluginTargetLineMarkerProvider.java

Lines changed: 77 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,56 @@
22
* Copyright © Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5+
56
package com.magento.idea.magento2plugin.linemarker.php;
67

78
import com.intellij.codeInsight.daemon.LineMarkerInfo;
89
import com.intellij.codeInsight.daemon.LineMarkerProvider;
910
import com.intellij.codeInsight.navigation.NavigationGutterIconBuilder;
1011
import com.intellij.icons.AllIcons;
1112
import com.intellij.psi.PsiElement;
13+
import com.intellij.psi.util.PsiTreeUtil;
1214
import com.jetbrains.php.PhpIndex;
1315
import com.jetbrains.php.lang.psi.elements.Method;
1416
import com.jetbrains.php.lang.psi.elements.PhpClass;
1517
import com.magento.idea.magento2plugin.magento.files.Plugin;
1618
import com.magento.idea.magento2plugin.project.Settings;
19+
import com.magento.idea.magento2plugin.util.magento.plugin.GetTargetClassNamesByPluginClassName;
20+
import java.util.ArrayList;
21+
import java.util.Collection;
22+
import java.util.HashMap;
23+
import java.util.List;
1724
import org.jetbrains.annotations.NotNull;
1825
import org.jetbrains.annotations.Nullable;
19-
import com.magento.idea.magento2plugin.util.magento.plugin.GetTargetClassNamesByPluginClassName;
20-
import com.intellij.psi.util.PsiTreeUtil;
21-
22-
import java.util.*;
2326

2427
public class PluginTargetLineMarkerProvider implements LineMarkerProvider {
2528
@Nullable
2629
@Override
27-
public LineMarkerInfo getLineMarkerInfo(@NotNull PsiElement psiElement) {
30+
public LineMarkerInfo getLineMarkerInfo(@NotNull final PsiElement psiElement) {
2831
return null;
2932
}
3033

3134
@Override
32-
public void collectSlowLineMarkers(@NotNull List<PsiElement> psiElements, @NotNull Collection<LineMarkerInfo> collection) {
33-
if (psiElements.size() > 0) {
34-
if (!Settings.isEnabled(psiElements.get(0).getProject())) {
35-
return;
36-
}
35+
public void collectSlowLineMarkers(
36+
@NotNull final List<PsiElement> psiElements,
37+
@NotNull final Collection<LineMarkerInfo> collection
38+
) {
39+
if (!psiElements.isEmpty() && !Settings.isEnabled(psiElements.get(0).getProject())) {
40+
return;
3741
}
38-
PluginClassCache pluginClassCache = new PluginClassCache();
39-
TargetClassesCollector TargetClassesCollector = new TargetClassesCollector(pluginClassCache);
40-
TargetMethodsCollector TargetMethodsCollector = new TargetMethodsCollector(pluginClassCache);
42+
final PluginClassCache pluginClassCache = new PluginClassCache();
43+
final TargetClassesCollector targetClassesCollector =
44+
new TargetClassesCollector(pluginClassCache);
45+
final TargetMethodsCollector targetMethodsCollector =
46+
new TargetMethodsCollector(pluginClassCache);
4147

42-
for (PsiElement psiElement : psiElements) {
48+
for (final PsiElement psiElement : psiElements) {
4349
if (psiElement instanceof PhpClass || psiElement instanceof Method) {
4450
List<? extends PsiElement> results;
4551

4652
if (psiElement instanceof PhpClass) {
47-
results = TargetClassesCollector.collect((PhpClass) psiElement);
48-
if (results.size() > 0 ) {
53+
results = targetClassesCollector.collect((PhpClass) psiElement);
54+
if (!results.isEmpty()) {
4955
collection.add(NavigationGutterIconBuilder
5056
.create(AllIcons.Nodes.Class)
5157
.setTargets(results)
@@ -54,8 +60,8 @@ public void collectSlowLineMarkers(@NotNull List<PsiElement> psiElements, @NotNu
5460
);
5561
}
5662
} else {
57-
results = TargetMethodsCollector.collect((Method) psiElement);
58-
if (results.size() > 0 ) {
63+
results = targetMethodsCollector.collect((Method) psiElement);
64+
if (!results.isEmpty()) {
5965
collection.add(NavigationGutterIconBuilder
6066
.create(AllIcons.Nodes.Method)
6167
.setTargets(results)
@@ -70,37 +76,49 @@ public void collectSlowLineMarkers(@NotNull List<PsiElement> psiElements, @NotNu
7076
}
7177

7278
private static class PluginClassCache {
73-
private HashMap<String, List<PhpClass>> pluginClassesMap = new HashMap<String, List<PhpClass>>();
79+
private final HashMap<String, List<PhpClass>> pluginClassesMap = // NOPMD
80+
new HashMap<>();
7481

75-
List<PhpClass> getTargetClassesForPlugin(@NotNull PhpClass phpClass, @NotNull String classFQN) {
76-
List<PhpClass> results = new ArrayList<>();
82+
private List<PhpClass> getTargetClassesForPlugin(
83+
@NotNull final PhpClass phpClass,
84+
@NotNull final String classFQN
85+
) {
7786

7887
if (pluginClassesMap.containsKey(classFQN)) {
7988
return pluginClassesMap.get(classFQN);
8089
}
8190

82-
GetTargetClassNamesByPluginClassName targetClassesService = GetTargetClassNamesByPluginClassName.getInstance(phpClass.getProject());
83-
ArrayList<String> targetClassNames = targetClassesService.execute(classFQN);
91+
final GetTargetClassNamesByPluginClassName targetClassesService =
92+
GetTargetClassNamesByPluginClassName.getInstance(phpClass.getProject());
93+
final ArrayList<String> targetClassNames = targetClassesService.execute(classFQN);
94+
95+
final List<PhpClass> results = new ArrayList<>();
8496

85-
if (targetClassNames.size() == 0) {
97+
if (targetClassNames.isEmpty()) {
8698
pluginClassesMap.put(classFQN, results);
8799
return results;
88100
}
89101

90-
PhpIndex phpIndex = PhpIndex.getInstance(phpClass.getProject());
102+
final PhpIndex phpIndex = PhpIndex.getInstance(phpClass.getProject());
91103

92-
for (String targetClassName : targetClassNames) {
93-
Collection<PhpClass> targets = phpIndex.getClassesByFQN(targetClassName);
104+
for (final String targetClassName : targetClassNames) {
105+
Collection<PhpClass> targets = phpIndex.getInterfacesByFQN(targetClassName);
106+
107+
if (targets.isEmpty()) {
108+
targets = phpIndex.getClassesByFQN(targetClassName);
109+
}
94110

95111
results.addAll(targets);
96112
}
97113

98114
return results;
99115
}
100116

101-
List<PhpClass> getTargetClassesForPlugin(@NotNull PhpClass phpClass) {
102-
List<PhpClass> classesForPlugin = getTargetClassesForPlugin(phpClass, phpClass.getPresentableFQN());
103-
for (PhpClass parent: phpClass.getSupers()) {
117+
protected List<PhpClass> getTargetClassesForPlugin(@NotNull final PhpClass phpClass) {
118+
final List<PhpClass> classesForPlugin = getTargetClassesForPlugin(
119+
phpClass, phpClass.getPresentableFQN()
120+
);
121+
for (final PhpClass parent : phpClass.getSupers()) {
104122
classesForPlugin.addAll(getTargetClassesForPlugin(parent));
105123
}
106124

@@ -109,52 +127,59 @@ List<PhpClass> getTargetClassesForPlugin(@NotNull PhpClass phpClass) {
109127
}
110128

111129
private static class TargetClassesCollector implements Collector<PhpClass, PhpClass> {
112-
private PluginTargetLineMarkerProvider.PluginClassCache pluginClassCache;
130+
private final PluginTargetLineMarkerProvider.PluginClassCache pluginClassCache;
113131

114-
TargetClassesCollector(PluginTargetLineMarkerProvider.PluginClassCache pluginClassCache) {
132+
TargetClassesCollector(// NOPMD
133+
final PluginTargetLineMarkerProvider.PluginClassCache pluginClassCache
134+
) {
115135
this.pluginClassCache = pluginClassCache;
116136
}
117137

118138
@Override
119-
public List<PhpClass> collect(@NotNull PhpClass psiElement) {
139+
public List<PhpClass> collect(@NotNull final PhpClass psiElement) {
120140
return pluginClassCache.getTargetClassesForPlugin(psiElement);
121141
}
122142
}
123143

124144
private static class TargetMethodsCollector implements Collector<Method, Method> {
125-
private PluginTargetLineMarkerProvider.PluginClassCache pluginClassCache;
145+
private final PluginTargetLineMarkerProvider.PluginClassCache pluginClassCache;
126146

127-
TargetMethodsCollector(PluginTargetLineMarkerProvider.PluginClassCache pluginClassCache) {
147+
TargetMethodsCollector(// NOPMD
148+
final PluginTargetLineMarkerProvider.PluginClassCache pluginClassCache
149+
) {
128150
this.pluginClassCache = pluginClassCache;
129151
}
130152

131153
@Override
132-
public List<Method> collect(@NotNull Method pluginMethod) {
133-
List<Method> results = new ArrayList<>();
154+
public List<Method> collect(@NotNull final Method pluginMethod) {
155+
final List<Method> results = new ArrayList<>();
134156

135157
/* Check if the method is a plugin */
136158
if (null == getPluginPrefix(pluginMethod)) {
137159
return results;
138160
}
139161

140-
PhpClass pluginClass = pluginMethod.getContainingClass();
162+
final PhpClass pluginClass = pluginMethod.getContainingClass();
141163
if (pluginClass == null) {
142164
return results;
143165
}
144166

145-
List<PhpClass> targetClasses = pluginClassCache.getTargetClassesForPlugin(pluginClass);
146-
if (targetClasses.size() == 0) {
167+
final List<PhpClass> targetClasses
168+
= pluginClassCache.getTargetClassesForPlugin(pluginClass);
169+
if (targetClasses.isEmpty()) {
147170
return results;
148171
}
149172

150-
for (PhpClass targetClass: targetClasses) {
151-
String pluginPrefix = getPluginPrefix(pluginMethod);
152-
String targetClassMethodName = getTargetMethodName(pluginMethod, pluginPrefix);
173+
for (final PhpClass targetClass : targetClasses) {
174+
final String pluginPrefix = getPluginPrefix(pluginMethod);
175+
final String targetClassMethodName = getTargetMethodName(
176+
pluginMethod, pluginPrefix
177+
);
153178
if (targetClassMethodName == null) {
154179
continue;
155180
}
156181

157-
Method targetMethod = targetClass.findMethodByName(targetClassMethodName);
182+
final Method targetMethod = targetClass.findMethodByName(targetClassMethodName);
158183
if (targetMethod == null) {
159184
continue;
160185
}
@@ -165,23 +190,21 @@ public List<Method> collect(@NotNull Method pluginMethod) {
165190
return results;
166191
}
167192

168-
private String getTargetMethodName(Method pluginMethod, String pluginPrefix) {
169-
String pluginMethodName = pluginMethod.getName();
170-
String targetClassMethodName = pluginMethodName.
171-
replace(pluginPrefix, "");
193+
private String getTargetMethodName(final Method pluginMethod, final String pluginPrefix) {
194+
final String targetClassMethodName = pluginMethod.getName().replace(pluginPrefix, "");
172195
if (targetClassMethodName.isEmpty()) {
173196
return null;
174197
}
175-
char firstCharOfTargetName = targetClassMethodName.charAt(0);
176-
int charType = Character.getType(firstCharOfTargetName);
177-
if (charType == Character.LOWERCASE_LETTER) {
198+
final char firstCharOfTargetName = targetClassMethodName.charAt(0);
199+
if (Character.getType(firstCharOfTargetName) == Character.LOWERCASE_LETTER) {
178200
return null;
179201
}
180-
return Character.toLowerCase(firstCharOfTargetName) + targetClassMethodName.substring(1);
202+
return Character.toLowerCase(firstCharOfTargetName)
203+
+ targetClassMethodName.substring(1);
181204
}
182205

183-
private String getPluginPrefix(Method pluginMethod) {
184-
String pluginMethodName = pluginMethod.getName();
206+
private String getPluginPrefix(final Method pluginMethod) {
207+
final String pluginMethodName = pluginMethod.getName();
185208
if (pluginMethodName.startsWith(Plugin.PluginType.around.toString())) {
186209
return Plugin.PluginType.around.toString();
187210
}

0 commit comments

Comments
 (0)