1
- /**
1
+ /*
2
2
* Copyright © Magento, Inc. All rights reserved.
3
3
* See COPYING.txt for license details.
4
4
*/
5
+
5
6
package com .magento .idea .magento2plugin .linemarker .php ;
6
7
7
8
import com .intellij .codeInsight .daemon .LineMarkerInfo ;
15
16
import com .magento .idea .magento2plugin .MagentoIcons ;
16
17
import com .magento .idea .magento2plugin .project .Settings ;
17
18
import com .magento .idea .magento2plugin .stubs .indexes .WebApiTypeIndex ;
19
+ import java .util .ArrayList ;
20
+ import java .util .Collection ;
21
+ import java .util .HashMap ;
22
+ import java .util .List ;
23
+ import java .util .Map ;
18
24
import org .jetbrains .annotations .NotNull ;
19
25
import org .jetbrains .annotations .Nullable ;
20
26
21
- import java .util .*;
22
-
23
27
/**
24
28
* Line marker for methods and classes which are exposed as web APIs.
25
29
* <p/>
28
32
*/
29
33
public class WebApiLineMarkerProvider implements LineMarkerProvider {
30
34
31
- @ Nullable
32
35
@ Override
33
- public LineMarkerInfo getLineMarkerInfo (@ NotNull PsiElement psiElement ) {
36
+ public @ Nullable LineMarkerInfo <?> getLineMarkerInfo (final @ NotNull PsiElement psiElement ) {
34
37
return null ;
35
38
}
36
39
37
40
@ Override
38
- public void collectSlowLineMarkers (@ NotNull List <? extends PsiElement > psiElements , @ NotNull Collection <? super LineMarkerInfo <?>> collection ) {
39
- if (psiElements .size () > 0 ) {
40
- if (!Settings .isEnabled (psiElements .get (0 ).getProject ())) {
41
- return ;
42
- }
41
+ public void collectSlowLineMarkers (
42
+ final @ NotNull List <? extends PsiElement > psiElements ,
43
+ final @ NotNull Collection <? super LineMarkerInfo <?>> collection
44
+ ) {
45
+ if (psiElements .isEmpty ()) {
46
+ return ;
47
+ }
48
+
49
+ if (!Settings .isEnabled (psiElements .get (0 ).getProject ())) {
50
+ return ;
43
51
}
44
- for (PsiElement psiElement : psiElements ) {
45
- WebApiRoutesCollector collector = new WebApiRoutesCollector ();
52
+
53
+ for (final PsiElement psiElement : psiElements ) {
54
+ final WebApiRoutesCollector collector = new WebApiRoutesCollector ();
46
55
List <XmlTag > results = new ArrayList <>();
56
+
47
57
if (psiElement instanceof Method ) {
48
58
results = collector .getRoutes ((Method ) psiElement );
49
59
} else if (psiElement instanceof PhpClass ) {
50
60
results = collector .getRoutes ((PhpClass ) psiElement );
51
61
}
52
62
53
- if (!( results .size () > 0 )) {
63
+ if (results .isEmpty ( )) {
54
64
continue ;
55
65
}
56
66
57
- StringBuilder tooltipText = new StringBuilder ("Navigate to Web API configuration:<pre>" );
58
- for (XmlTag routeTag : results ) {
67
+ StringBuilder tooltipText = new StringBuilder (
68
+ "Navigate to Web API configuration:<pre>"
69
+ );
70
+ for (final XmlTag routeTag : results ) {
59
71
tooltipText .append (routeTag .getName ()).append ("\n " );
60
72
}
61
73
tooltipText .append ("</pre>" );
@@ -72,24 +84,27 @@ public void collectSlowLineMarkers(@NotNull List<? extends PsiElement> psiElemen
72
84
*/
73
85
private static class WebApiRoutesCollector {
74
86
75
- private HashMap <String , List <XmlTag >> routesCache = new HashMap <>();
76
-
77
- private static final Map <String , Integer > HTTP_METHODS_SORT_ORDER = new HashMap <String , Integer >() {{
78
- put ("GET" , 1 );
79
- put ("PUT" , 2 );
80
- put ("POS" , 3 );
81
- put ("DEL" , 4 );
82
- }};
87
+ private final Map <String , List <XmlTag >> routesCache = new HashMap <>();
88
+ private static final Map <String , Integer > HTTP_METHODS_SORT_ORDER = new HashMap <>() {
89
+ {
90
+ put ("GET" , 1 );
91
+ put ("PUT" , 2 );
92
+ put ("POS" , 3 );
93
+ put ("DEL" , 4 );
94
+ }
95
+ };
83
96
84
97
/**
85
98
* Get sorted list of Web API routes related to the specified class.
86
99
*/
87
- List <XmlTag > getRoutes (@ NotNull PhpClass phpClass ) {
88
- List <XmlTag > routesForClass = new ArrayList <>();
89
- for (Method method : phpClass .getMethods ()) {
100
+ public List <XmlTag > getRoutes (final @ NotNull PhpClass phpClass ) {
101
+ final List <XmlTag > routesForClass = new ArrayList <>();
102
+
103
+ for (final Method method : phpClass .getMethods ()) {
90
104
routesForClass .addAll (getRoutes (method ));
91
105
}
92
106
sortRoutes (routesForClass );
107
+
93
108
return routesForClass ;
94
109
}
95
110
@@ -98,13 +113,15 @@ List<XmlTag> getRoutes(@NotNull PhpClass phpClass) {
98
113
* <p/>
99
114
* Results are cached.
100
115
*/
101
- List <XmlTag > getRoutes (@ NotNull Method method ) {
102
- String methodFqn = method .getFQN ();
116
+ public List <XmlTag > getRoutes (final @ NotNull Method method ) {
117
+ final String methodFqn = method .getFQN ();
118
+
103
119
if (!routesCache .containsKey (methodFqn )) {
104
120
List <XmlTag > routesForMethod = extractRoutesForMethod (method );
105
121
sortRoutes (routesForMethod );
106
122
routesCache .put (methodFqn , routesForMethod );
107
123
}
124
+
108
125
return routesCache .get (methodFqn );
109
126
}
110
127
@@ -114,37 +131,42 @@ List<XmlTag> getRoutes(@NotNull Method method) {
114
131
* Web API declarations for parent classes are taken into account.
115
132
* Results are not cached.
116
133
*/
117
- List <XmlTag > extractRoutesForMethod (@ NotNull Method method ) {
118
- List <XmlTag > routesForMethod = WebApiTypeIndex .getWebApiRoutes (method );
119
- PhpClass phpClass = method .getContainingClass ();
134
+ public List <XmlTag > extractRoutesForMethod (final @ NotNull Method method ) {
135
+ final List <XmlTag > routesForMethod = WebApiTypeIndex .getWebApiRoutes (method );
136
+ final PhpClass phpClass = method .getContainingClass ();
137
+
120
138
if (phpClass == null ) {
121
139
return routesForMethod ;
122
140
}
123
- for (PhpClass parent : method .getContainingClass ().getSupers ()) {
141
+ for (final PhpClass parent : method .getContainingClass ().getSupers ()) {
124
142
for (Method parentMethod : parent .getMethods ()) {
125
143
if (parentMethod .getName ().equals (method .getName ())) {
126
144
routesForMethod .addAll (extractRoutesForMethod (parentMethod ));
127
145
}
128
146
}
129
147
}
148
+
130
149
return routesForMethod ;
131
150
}
132
151
133
152
/**
134
153
* Make sure that routes are sorted as follows: GET, PUT, POST, DELETE. Then by path.
135
154
*/
136
- private void sortRoutes (List <XmlTag > routes ) {
155
+ private void sortRoutes (final List <XmlTag > routes ) {
137
156
routes .sort (
138
- (firstTag , secondTag ) -> {
139
- String substring = firstTag .getName ().substring (2 , 5 );
140
- Integer firstSortOrder = HTTP_METHODS_SORT_ORDER .get (substring );
141
- Integer secondSortOrder = HTTP_METHODS_SORT_ORDER .get (secondTag .getName ().substring (2 , 5 ));
142
- if (firstSortOrder .compareTo (secondSortOrder ) == 0 ) {
143
- // Sort by route if HTTP methods are equal
144
- return firstTag .getName ().compareTo (secondTag .getName ());
157
+ (firstTag , secondTag ) -> {
158
+ String substring = firstTag .getName ().substring (2 , 5 );
159
+ Integer firstSortOrder = HTTP_METHODS_SORT_ORDER .get (substring );
160
+ Integer secondSortOrder = HTTP_METHODS_SORT_ORDER .get (
161
+ secondTag .getName ().substring (2 , 5 )
162
+ );
163
+ if (firstSortOrder .compareTo (secondSortOrder ) == 0 ) {
164
+ // Sort by route if HTTP methods are equal
165
+ return firstTag .getName ().compareTo (secondTag .getName ());
166
+ }
167
+
168
+ return firstSortOrder .compareTo (secondSortOrder );
145
169
}
146
- return firstSortOrder .compareTo (secondSortOrder );
147
- }
148
170
);
149
171
}
150
172
}
0 commit comments