Skip to content

Commit 31b834e

Browse files
authored
Merge pull request #1949 from Haehnchen/feature/resources-index
extend resources / import index with context options
2 parents a7311d3 + 1f35917 commit 31b834e

File tree

5 files changed

+96
-51
lines changed

5 files changed

+96
-51
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/stubs/dict/FileResource.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,48 @@
55

66
import java.io.Serializable;
77
import java.util.Objects;
8+
import java.util.TreeMap;
89

910
/**
1011
* @author Daniel Espendiller <daniel@espendiller.net>
1112
*/
1213
public class FileResource implements Serializable {
13-
1414
private final String resource;
15-
private String prefix = null;
15+
private final FileResourceContextTypeEnum contextType;
16+
private final TreeMap<String, String> contextValues;
1617

17-
public FileResource(@Nullable String resource) {
18+
public FileResource(@Nullable String resource, @Nullable FileResourceContextTypeEnum contextType, @Nullable TreeMap<String, String> contextValues) {
1819
this.resource = resource;
20+
this.contextType = contextType;
21+
this.contextValues = contextValues;
1922
}
2023

2124
@Nullable
2225
public String getResource() {
2326
return resource;
2427
}
2528

26-
public FileResource setPrefix(@Nullable String prefix) {
27-
this.prefix = prefix;
28-
return this;
29+
@Nullable
30+
public FileResourceContextTypeEnum getContextType() {
31+
return contextType;
32+
}
33+
34+
@Nullable
35+
public TreeMap<String, String> getContextValues() {
36+
return contextValues;
2937
}
3038

39+
@Nullable
3140
public String getPrefix() {
32-
return prefix;
41+
return contextValues == null ? null : contextValues.get("prefix");
3342
}
3443

3544
@Override
3645
public int hashCode() {
3746
return new HashCodeBuilder()
3847
.append(this.resource)
39-
.append(this.prefix)
48+
.append(this.contextType != null ? this.contextType.toString() : "")
49+
.append(this.contextValues != null ? this.contextValues.hashCode() : "")
4050
.toHashCode()
4151
;
4252
}
@@ -45,7 +55,8 @@ public int hashCode() {
4555
public boolean equals(Object obj) {
4656
return obj instanceof FileResource &&
4757
Objects.equals(((FileResource) obj).resource, this.resource) &&
48-
Objects.equals(((FileResource) obj).prefix, this.prefix)
58+
Objects.equals(((FileResource) obj).contextType, this.contextType) &&
59+
Objects.equals(((FileResource) obj).contextValues, this.contextValues)
4960
;
5061
}
5162
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package fr.adrienbrault.idea.symfony2plugin.stubs.dict;
2+
3+
/**
4+
* @author Daniel Espendiller <daniel@espendiller.net>
5+
*/
6+
public enum FileResourceContextTypeEnum {
7+
UNKNOWN,
8+
ROUTE,
9+
CONTAINER
10+
}

src/main/java/fr/adrienbrault/idea/symfony2plugin/stubs/indexes/FileResourcesIndex.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public boolean dependsOnFileContent() {
8080

8181
@Override
8282
public int getVersion() {
83-
return 2;
83+
return 3;
8484
}
8585

8686
public static boolean isValidForIndex(FileContent inputData, PsiFile psiFile) {

src/main/java/fr/adrienbrault/idea/symfony2plugin/util/FileResourceVisitorUtil.java

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,22 @@
55
import com.intellij.psi.xml.XmlFile;
66
import com.intellij.psi.xml.XmlTag;
77
import com.intellij.util.Consumer;
8-
import fr.adrienbrault.idea.symfony2plugin.dic.attribute.value.AttributeValueInterface;
9-
import fr.adrienbrault.idea.symfony2plugin.dic.attribute.value.DummyAttributeValue;
10-
import fr.adrienbrault.idea.symfony2plugin.dic.attribute.value.XmlTagAttributeValue;
11-
import fr.adrienbrault.idea.symfony2plugin.dic.attribute.value.YamlKeyValueAttributeValue;
128
import fr.adrienbrault.idea.symfony2plugin.stubs.dict.FileResource;
9+
import fr.adrienbrault.idea.symfony2plugin.stubs.dict.FileResourceContextTypeEnum;
1310
import fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper;
1411
import org.apache.commons.lang.StringUtils;
1512
import org.jetbrains.annotations.NotNull;
16-
import org.jetbrains.annotations.Nullable;
1713
import org.jetbrains.yaml.psi.YAMLFile;
1814
import org.jetbrains.yaml.psi.YAMLKeyValue;
1915

16+
import java.util.HashMap;
17+
import java.util.Map;
18+
import java.util.TreeMap;
19+
2020
/**
2121
* @author Daniel Espendiller <daniel@espendiller.net>
2222
*/
2323
public class FileResourceVisitorUtil {
24-
2524
public static void visitFile(@NotNull PsiFile psiFile, @NotNull Consumer<FileResourceConsumer> consumer) {
2625
if(psiFile instanceof XmlFile) {
2726
visitXmlFile((XmlFile) psiFile, consumer);
@@ -46,7 +45,26 @@ private static void visitYamlFile(@NotNull YAMLFile yamlFile, @NotNull Consumer<
4645
continue;
4746
}
4847

49-
consumer.consume(new FileResourceConsumer(resourceKey, yamlKeyValue, normalize(resource)));
48+
FileResourceContextTypeEnum fileResourceContextType = FileResourceContextTypeEnum.UNKNOWN;
49+
50+
Map<String, String> map = new HashMap<>();
51+
for (String option: new String[] {"type", "prefix", "name_prefix"}) {
52+
String attributeValue = YamlHelper.getYamlKeyValueAsString(yamlKeyValue, option, true);
53+
if (StringUtils.isNotBlank(attributeValue) && attributeValue.length() < 128) {
54+
map.put(option, attributeValue);
55+
}
56+
}
57+
58+
boolean isRouteContext = map.containsKey("type")
59+
|| map.containsKey("prefix")
60+
|| map.containsKey("name_prefix")
61+
|| YamlHelper.getYamlKeyValue(yamlKeyValue, "requirements", true) != null;
62+
63+
if (isRouteContext) {
64+
fileResourceContextType = FileResourceContextTypeEnum.ROUTE;
65+
}
66+
67+
consumer.consume(new FileResourceConsumer(resourceKey, normalize(resource), fileResourceContextType, map));
5068
}
5169
}
5270

@@ -65,7 +83,15 @@ private static void visitXmlFile(@NotNull XmlFile psiFile, @NotNull Consumer<Fil
6583
continue;
6684
}
6785

68-
consumer.consume(new FileResourceConsumer(xmlTag, xmlTag, normalize(resource)));
86+
Map<String, String> map = new HashMap<>();
87+
for (String option: new String[] {"type", "prefix", "name-prefix"}) {
88+
String attributeValue = xmlTag.getAttributeValue(option);
89+
if (StringUtils.isNotBlank(attributeValue) && attributeValue.length() < 128) {
90+
map.put(option.replace("-", "_"), attributeValue);
91+
}
92+
}
93+
94+
consumer.consume(new FileResourceConsumer(xmlTag, normalize(resource), FileResourceContextTypeEnum.ROUTE, map));
6995
}
7096
}
7197

@@ -75,39 +101,23 @@ public static String normalize(@NotNull String resource) {
75101
}
76102

77103
public static class FileResourceConsumer {
78-
79104
@NotNull
80105
private final PsiElement psiElement;
81106

82-
@Nullable
83-
private AttributeValueInterface attributeValue = null;
107+
@NotNull
108+
private final String resource;
84109

85110
@NotNull
86-
private final PsiElement scope;
111+
private final FileResourceContextTypeEnum contextType;
112+
87113
@NotNull
88-
private final String resource;
114+
private final Map<String, String> contextValues;
89115

90-
public FileResourceConsumer(@NotNull PsiElement target, @NotNull PsiElement scope, @NotNull String resource) {
116+
public FileResourceConsumer(@NotNull PsiElement target, @NotNull String resource, @NotNull FileResourceContextTypeEnum fileResourceContextTypeEnum, @NotNull Map<String, String> contextValues) {
91117
this.psiElement = target;
92-
this.scope = scope;
93118
this.resource = resource;
94-
}
95-
96-
@NotNull
97-
public AttributeValueInterface getAttributeValue() {
98-
if(this.attributeValue != null) {
99-
return this.attributeValue;
100-
}
101-
102-
// We use lazy instances
103-
// @TODO: replace with factory pattern
104-
if(this.psiElement instanceof YAMLKeyValue) {
105-
return this.attributeValue = new YamlKeyValueAttributeValue((YAMLKeyValue) this.scope);
106-
} else if(this.psiElement instanceof XmlTag) {
107-
return this.attributeValue = new XmlTagAttributeValue((XmlTag) this.scope);
108-
}
109-
110-
return this.attributeValue = new DummyAttributeValue(this.psiElement);
119+
this.contextType = fileResourceContextTypeEnum;
120+
this.contextValues = contextValues;
111121
}
112122

113123
@NotNull
@@ -121,14 +131,13 @@ public PsiElement getPsiElement() {
121131
}
122132

123133
@NotNull
124-
public FileResource createFileResource() {
125-
FileResource fileResource = new FileResource(this.getResource());
126-
String prefix = this.getAttributeValue().getString("prefix");
127-
if(prefix != null) {
128-
fileResource.setPrefix(prefix);
129-
}
134+
public FileResourceContextTypeEnum getContextType() {
135+
return contextType;
136+
}
130137

131-
return fileResource;
138+
@NotNull
139+
public FileResource createFileResource() {
140+
return new FileResource(this.getResource(), this.getContextType(), new TreeMap<>(this.contextValues));
132141
}
133142
}
134143
}

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/stubs/indexes/FileResourcesIndexTest.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
import com.intellij.util.containers.ContainerUtil;
66
import com.intellij.util.indexing.FileBasedIndex;
77
import fr.adrienbrault.idea.symfony2plugin.stubs.dict.FileResource;
8+
import fr.adrienbrault.idea.symfony2plugin.stubs.dict.FileResourceContextTypeEnum;
89
import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.FileResourcesIndex;
910
import fr.adrienbrault.idea.symfony2plugin.tests.SymfonyLightCodeInsightFixtureTestCase;
1011
import org.jetbrains.yaml.YAMLFileType;
1112

13+
import java.util.Map;
14+
import java.util.TreeMap;
15+
1216
/**
1317
* @author Daniel Espendiller <daniel@espendiller.net>
1418
*
@@ -22,7 +26,9 @@ public void setUp() throws Exception {
2226
myFixture.configureByText(YAMLFileType.YML, "" +
2327
"app:\n" +
2428
" resource: \"@AppBundle/Controller/\"\n" +
29+
" type: annotation\n" +
2530
" prefix: \"/foo\"\n" +
31+
" name_prefix: 'blog_'\n" +
2632
"\n" +
2733
"app1:\n" +
2834
" resource: \"@AcmeOtherBundle/Resources/config/routing1.yml\"\n" +
@@ -36,7 +42,7 @@ public void setUp() throws Exception {
3642

3743
myFixture.configureByText(XmlFileType.INSTANCE, "" +
3844
"<routes>\n" +
39-
" <import resource=\"@AcmeOtherBundle/Resources/config/routing.xml\" prefix=\"/foo2\"/>\n" +
45+
" <import resource=\"@AcmeOtherBundle/Resources/config/routing.xml\" type=\"annotation\" prefix=\"/foo\" name-prefix=\"blog_\"/>\n" +
4046
" <import resource=\"@AcmeOtherBundle//Resources/config/routing1.xml\" />\n" +
4147
" <import resource=\"@AcmeOtherBundle\\\\\\Resources/config///routing2.xml\" />\n" +
4248
"</routes>"
@@ -59,10 +65,19 @@ public void testXmlResourcesImport() {
5965
}
6066

6167
public void testIndexValue() {
68+
Map<String, String> treeMap = new TreeMap<>();
69+
treeMap.put("name_prefix", "blog_");
70+
treeMap.put("prefix", "/foo");
71+
treeMap.put("type", "annotation");
72+
6273
FileResource item = ContainerUtil.getFirstItem(FileBasedIndex.getInstance().getValues(FileResourcesIndex.KEY, "@AppBundle/Controller", GlobalSearchScope.allScope(getProject())));
6374
assertEquals("/foo", item.getPrefix());
75+
assertEquals(treeMap, item.getContextValues());
76+
assertEquals(FileResourceContextTypeEnum.ROUTE, item.getContextType());
6477

6578
item = ContainerUtil.getFirstItem(FileBasedIndex.getInstance().getValues(FileResourcesIndex.KEY, "@AcmeOtherBundle/Resources/config/routing.xml", GlobalSearchScope.allScope(getProject())));
66-
assertEquals("/foo2", item.getPrefix());
79+
assertEquals("/foo", item.getPrefix());
80+
assertEquals(FileResourceContextTypeEnum.ROUTE, item.getContextType());
81+
assertEquals(treeMap, item.getContextValues());
6782
}
6883
}

0 commit comments

Comments
 (0)