Skip to content

Commit caf1a28

Browse files
authored
Merge pull request #815 from bohdan-harniuk/bugfix/645-failed-to-build-require-js-index
Bugfix-645: Fixed failed to build require-js index
2 parents 20d5e93 + 8374386 commit caf1a28

File tree

1 file changed

+105
-60
lines changed

1 file changed

+105
-60
lines changed

src/com/magento/idea/magento2plugin/stubs/indexes/js/RequireJsIndex.java

Lines changed: 105 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,114 +2,159 @@
22
* Copyright © Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5+
56
package com.magento.idea.magento2plugin.stubs.indexes.js;
67

78
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;
915
import com.intellij.psi.PsiElement;
1016
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;
1222
import com.intellij.util.io.DataExternalizer;
1323
import com.intellij.util.io.EnumeratorStringDescriptor;
1424
import com.intellij.util.io.KeyDescriptor;
15-
import org.jetbrains.annotations.NotNull;
1625
import java.util.HashMap;
1726
import java.util.Map;
27+
import org.jetbrains.annotations.NotNull;
1828

1929
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");
2230

23-
@NotNull
31+
public static final ID<String, String> KEY = ID.create(
32+
"com.magento.idea.magento2plugin.stubs.indexes.require_js"
33+
);
34+
2435
@Override
25-
public ID<String, String> getName() {
36+
public @NotNull ID<String, String> getName() {
2637
return KEY;
2738
}
2839

29-
@NotNull
3040
@Override
31-
public DataIndexer<String, String, FileContent> getIndexer() {
41+
public @NotNull DataIndexer<String, String, FileContent> getIndexer() {
3242
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+
);
3550

36-
JSVarStatement jsVarStatement = PsiTreeUtil.getChildOfType(jsFile, JSVarStatement.class);
3751
if (jsVarStatement == null) {
3852
return map;
3953
}
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+
4565
if (config == null) {
4666
return map;
4767
}
4868
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);
6770
}
6871
}
6972

7073
return map;
7174
};
7275
}
7376

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+
7683
if (configMap == null) {
7784
return;
7885
}
86+
final JSObjectLiteralExpression[] configGroupsWrappers = PsiTreeUtil.getChildrenOfType(
87+
configMap,
88+
JSObjectLiteralExpression.class
89+
);
90+
if (configGroupsWrappers == null) {
91+
return;
92+
}
7993

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();
8396

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+
);
87102

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;
96105
}
106+
processObjectProperties(map, mappingWrapper.getProperties());
97107
}
98108
}
99109
}
100110

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+
102148
@Override
103-
public KeyDescriptor<String> getKeyDescriptor() {
149+
public @NotNull KeyDescriptor<String> getKeyDescriptor() {
104150
return new EnumeratorStringDescriptor();
105151
}
106152

107-
@NotNull
108153
@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());
113158
}
114159

115160
@Override
@@ -122,8 +167,8 @@ public int getVersion() {
122167
return 1;
123168
}
124169

125-
@NotNull
126-
public DataExternalizer<String> getValueExternalizer() {
170+
@Override
171+
public @NotNull DataExternalizer<String> getValueExternalizer() {
127172
return EnumeratorStringDescriptor.INSTANCE;
128173
}
129174
}

0 commit comments

Comments
 (0)