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 .stubs .indexes .js ;
6
7
7
8
import com .intellij .lang .javascript .JavaScriptFileType ;
8
- import com .intellij .lang .javascript .psi .*;
9
+ import com .intellij .lang .javascript .psi .JSExpression ;
10
+ import com .intellij .lang .javascript .psi .JSFile ;
11
+ import com .intellij .lang .javascript .psi .JSObjectLiteralExpression ;
12
+ import com .intellij .lang .javascript .psi .JSProperty ;
13
+ import com .intellij .lang .javascript .psi .JSVarStatement ;
14
+ import com .intellij .lang .javascript .psi .JSVariable ;
9
15
import com .intellij .psi .PsiElement ;
10
16
import com .intellij .psi .util .PsiTreeUtil ;
11
- import com .intellij .util .indexing .*;
17
+ import com .intellij .util .indexing .DataIndexer ;
18
+ import com .intellij .util .indexing .FileBasedIndex ;
19
+ import com .intellij .util .indexing .FileBasedIndexExtension ;
20
+ import com .intellij .util .indexing .FileContent ;
21
+ import com .intellij .util .indexing .ID ;
12
22
import com .intellij .util .io .DataExternalizer ;
13
23
import com .intellij .util .io .EnumeratorStringDescriptor ;
14
24
import com .intellij .util .io .KeyDescriptor ;
15
- import org .jetbrains .annotations .NotNull ;
16
25
import java .util .HashMap ;
17
26
import java .util .Map ;
27
+ import org .jetbrains .annotations .NotNull ;
18
28
19
29
public class RequireJsIndex extends FileBasedIndexExtension <String , String > {
20
- public static final ID <String , String > KEY =
21
- ID .create ("com.magento.idea.magento2plugin.stubs.indexes.require_js" );
22
30
23
- @ NotNull
31
+ public static final ID <String , String > KEY = ID .create (
32
+ "com.magento.idea.magento2plugin.stubs.indexes.require_js"
33
+ );
34
+
24
35
@ Override
25
- public ID <String , String > getName () {
36
+ public @ NotNull ID <String , String > getName () {
26
37
return KEY ;
27
38
}
28
39
29
- @ NotNull
30
40
@ Override
31
- public DataIndexer <String , String , FileContent > getIndexer () {
41
+ public @ NotNull DataIndexer <String , String , FileContent > getIndexer () {
32
42
return inputData -> {
33
- Map <String , String > map = new HashMap <>();
34
- JSFile jsFile = (JSFile )inputData .getPsiFile ();
43
+ final Map <String , String > map = new HashMap <>();
44
+ final JSFile jsFile = (JSFile ) inputData .getPsiFile ();
45
+
46
+ final JSVarStatement jsVarStatement = PsiTreeUtil .getChildOfType (
47
+ jsFile ,
48
+ JSVarStatement .class
49
+ );
35
50
36
- JSVarStatement jsVarStatement = PsiTreeUtil .getChildOfType (jsFile , JSVarStatement .class );
37
51
if (jsVarStatement == null ) {
38
52
return map ;
39
53
}
40
- JSVariable [] jsVariableList = jsVarStatement .getVariables ();
41
- for (JSVariable jsVariable : jsVariableList ) {
42
- String name = jsVariable .getName ();
43
- if (name .equals ("config" )) {
44
- JSObjectLiteralExpression config = PsiTreeUtil .getChildOfType (jsVariable , JSObjectLiteralExpression .class );
54
+ final JSVariable [] jsVariableList = jsVarStatement .getVariables ();
55
+
56
+ for (final JSVariable jsVariable : jsVariableList ) {
57
+ final String name = jsVariable .getName ();
58
+
59
+ if ("config" .equals (name )) {
60
+ final JSObjectLiteralExpression config = PsiTreeUtil .getChildOfType (
61
+ jsVariable ,
62
+ JSObjectLiteralExpression .class
63
+ );
64
+
45
65
if (config == null ) {
46
66
return map ;
47
67
}
48
68
parseConfigMap (map , config );
49
-
50
- JSProperty pathsMap = config .findProperty ("paths" );
51
- if (pathsMap == null ) {
52
- return map ;
53
- }
54
- JSObjectLiteralExpression [] pathGroupsWrappers = PsiTreeUtil .getChildrenOfType (pathsMap , JSObjectLiteralExpression .class );
55
- for (JSObjectLiteralExpression pathGroupsWrapper : pathGroupsWrappers ) {
56
- JSProperty [] allConfigs = pathGroupsWrapper .getProperties ();
57
- for (JSProperty mapping : allConfigs ) {
58
- String nameConfig = mapping .getName ();
59
- JSExpression value = mapping .getValue ();
60
- if (value == null ) {
61
- continue ;
62
- }
63
- String valueConfig = value .getText ();
64
- map .put (nameConfig , valueConfig );
65
- }
66
- }
69
+ parseConfigPaths (map , config );
67
70
}
68
71
}
69
72
70
73
return map ;
71
74
};
72
75
}
73
76
74
- private void parseConfigMap (Map <String , String > map , JSObjectLiteralExpression config ) {
75
- JSProperty configMap = config .findProperty ("map" );
77
+ private void parseConfigMap (
78
+ final Map <String , String > map ,
79
+ final JSObjectLiteralExpression config
80
+ ) {
81
+ final JSProperty configMap = config .findProperty ("map" );
82
+
76
83
if (configMap == null ) {
77
84
return ;
78
85
}
86
+ final JSObjectLiteralExpression [] configGroupsWrappers = PsiTreeUtil .getChildrenOfType (
87
+ configMap ,
88
+ JSObjectLiteralExpression .class
89
+ );
90
+ if (configGroupsWrappers == null ) {
91
+ return ;
92
+ }
79
93
80
- JSObjectLiteralExpression [] configGroupsWrappers = PsiTreeUtil .getChildrenOfType (configMap , JSObjectLiteralExpression .class );
81
- for (JSObjectLiteralExpression configGroupsWrapper : configGroupsWrappers ) {
82
- PsiElement [] configGroups = configGroupsWrapper .getChildren ();
94
+ for (final JSObjectLiteralExpression configGroupsWrapper : configGroupsWrappers ) {
95
+ final PsiElement [] configGroups = configGroupsWrapper .getChildren ();
83
96
84
- for (PsiElement configGroup : configGroups ) {
85
- JSObjectLiteralExpression mappingWrapper = PsiTreeUtil .getChildOfType (configGroup , JSObjectLiteralExpression .class );
86
- JSProperty [] allConfigs = mappingWrapper .getProperties ();
97
+ for (final PsiElement configGroup : configGroups ) {
98
+ final JSObjectLiteralExpression mappingWrapper = PsiTreeUtil .getChildOfType (
99
+ configGroup ,
100
+ JSObjectLiteralExpression .class
101
+ );
87
102
88
- for (JSProperty mapping : allConfigs ) {
89
- String nameConfig = mapping .getName ();
90
- JSExpression value = mapping .getValue ();
91
- if (value == null ) {
92
- continue ;
93
- }
94
- String valueConfig = value .getText ();
95
- map .put (nameConfig , valueConfig );
103
+ if (mappingWrapper == null ) {
104
+ continue ;
96
105
}
106
+ processObjectProperties (map , mappingWrapper .getProperties ());
97
107
}
98
108
}
99
109
}
100
110
101
- @ NotNull
111
+ private void parseConfigPaths (
112
+ final Map <String , String > map ,
113
+ final JSObjectLiteralExpression config
114
+ ) {
115
+ final JSProperty pathsMap = config .findProperty ("paths" );
116
+
117
+ if (pathsMap == null ) {
118
+ return ;
119
+ }
120
+ final JSObjectLiteralExpression [] pathGroupsWrappers = PsiTreeUtil
121
+ .getChildrenOfType (pathsMap , JSObjectLiteralExpression .class );
122
+
123
+ if (pathGroupsWrappers == null ) {
124
+ return ;
125
+ }
126
+
127
+ for (final JSObjectLiteralExpression pathGroupsWrapper : pathGroupsWrappers ) {
128
+ processObjectProperties (map , pathGroupsWrapper .getProperties ());
129
+ }
130
+ }
131
+
132
+ private void processObjectProperties (
133
+ final Map <String , String > map ,
134
+ final JSProperty ... allConfigs
135
+ ) {
136
+ for (final JSProperty mapping : allConfigs ) {
137
+ final String nameConfig = mapping .getName ();
138
+ final JSExpression value = mapping .getValue ();
139
+
140
+ if (value == null ) {
141
+ continue ;
142
+ }
143
+ final String valueConfig = value .getText ();
144
+ map .put (nameConfig , valueConfig );
145
+ }
146
+ }
147
+
102
148
@ Override
103
- public KeyDescriptor <String > getKeyDescriptor () {
149
+ public @ NotNull KeyDescriptor <String > getKeyDescriptor () {
104
150
return new EnumeratorStringDescriptor ();
105
151
}
106
152
107
- @ NotNull
108
153
@ Override
109
- public FileBasedIndex .InputFilter getInputFilter () {
110
- return virtualFile -> (
111
- virtualFile .getFileType ().equals (JavaScriptFileType .INSTANCE ) && virtualFile . getName (). equals ( "requirejs-config.js" )
112
- );
154
+ public @ NotNull FileBasedIndex .InputFilter getInputFilter () {
155
+ return virtualFile ->
156
+ virtualFile .getFileType ().equals (JavaScriptFileType .INSTANCE )
157
+ && "requirejs-config.js" . equals ( virtualFile . getName () );
113
158
}
114
159
115
160
@ Override
@@ -122,8 +167,8 @@ public int getVersion() {
122
167
return 1 ;
123
168
}
124
169
125
- @ NotNull
126
- public DataExternalizer <String > getValueExternalizer () {
170
+ @ Override
171
+ public @ NotNull DataExternalizer <String > getValueExternalizer () {
127
172
return EnumeratorStringDescriptor .INSTANCE ;
128
173
}
129
174
}
0 commit comments