Skip to content

Commit a74f584

Browse files
committed
migrate all index related yaml usages to new yaml structure #626
1 parent 396e9d3 commit a74f584

File tree

7 files changed

+189
-169
lines changed

7 files changed

+189
-169
lines changed

src/fr/adrienbrault/idea/symfony2plugin/dic/container/util/ServiceContainerUtil.java

+5-22
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import fr.adrienbrault.idea.symfony2plugin.dic.container.ServiceInterface;
1515
import fr.adrienbrault.idea.symfony2plugin.dic.container.SerializableService;
1616
import fr.adrienbrault.idea.symfony2plugin.dic.container.visitor.ServiceConsumer;
17+
import fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper;
1718
import org.apache.commons.lang.StringUtils;
1819
import org.jetbrains.annotations.NotNull;
1920
import org.jetbrains.yaml.psi.YAMLDocument;
@@ -98,31 +99,13 @@ private static SerializableService createService(@NotNull ServiceConsumer servic
9899
}
99100

100101
public static void visitFile(@NotNull YAMLFile psiFile, @NotNull Consumer<ServiceConsumer> consumer) {
101-
YAMLDocument yamlDocument = PsiTreeUtil.getChildOfType(psiFile, YAMLDocument.class);
102-
if(yamlDocument == null) {
103-
return;
104-
}
105-
106-
// get services or parameter key
107-
YAMLKeyValue[] yamlKeys = PsiTreeUtil.getChildrenOfType(yamlDocument, YAMLKeyValue.class);
108-
if(yamlKeys == null) {
109-
return;
110-
}
111-
112-
for(YAMLKeyValue yamlKeyValue : yamlKeys) {
113-
String yamlConfigKey = yamlKeyValue.getName();
114-
if(yamlConfigKey == null || !yamlConfigKey.equals("services")) {
102+
for (YAMLKeyValue keyValue : YamlHelper.getQualifiedKeyValuesInFile(psiFile, "services")) {
103+
String serviceId = keyValue.getKeyText();
104+
if(StringUtils.isBlank(serviceId)) {
115105
continue;
116106
}
117107

118-
for (YAMLKeyValue keyValue : PsiTreeUtil.getChildrenOfTypeAsList(yamlKeyValue.getValue(), YAMLKeyValue.class)) {
119-
String serviceId = keyValue.getKeyText();
120-
if(serviceId == null) {
121-
continue;
122-
}
123-
124-
consumer.consume(new ServiceConsumer(keyValue, serviceId, new YamlKeyValueAttributeValue(keyValue)));
125-
}
108+
consumer.consume(new ServiceConsumer(keyValue, serviceId, new YamlKeyValueAttributeValue(keyValue)));
126109
}
127110
}
128111

src/fr/adrienbrault/idea/symfony2plugin/doctrine/DoctrineUtil.java

+4-9
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,17 @@
1515
import com.jetbrains.php.lang.psi.PhpFile;
1616
import com.jetbrains.php.lang.psi.elements.PhpClass;
1717
import com.jetbrains.php.lang.psi.elements.PhpPsiElement;
18-
import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.AnnotationRoutesStubIndex;
1918
import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.visitor.AnnotationElementWalkingVisitor;
20-
import fr.adrienbrault.idea.symfony2plugin.util.AnnotationBackportUtil;
2119
import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils;
2220
import fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper;
2321
import org.apache.commons.lang.ArrayUtils;
2422
import org.apache.commons.lang.StringUtils;
2523
import org.jetbrains.annotations.NotNull;
2624
import org.jetbrains.annotations.Nullable;
27-
import org.jetbrains.yaml.psi.YAMLDocument;
28-
import org.jetbrains.yaml.psi.YAMLFile;
29-
import org.jetbrains.yaml.psi.YAMLKeyValue;
25+
import org.jetbrains.yaml.psi.*;
3026

3127
import java.util.ArrayList;
3228
import java.util.Collection;
33-
import java.util.Map;
3429
import java.util.Set;
3530
import java.util.regex.Matcher;
3631
import java.util.regex.Pattern;
@@ -185,14 +180,14 @@ private static Collection<Pair<String, String>> getClassRepositoryPair(@NotNull
185180
return null;
186181
}
187182

188-
YAMLKeyValue[] yamlKeys = PsiTreeUtil.getChildrenOfType(yamlDocument, YAMLKeyValue.class);
189-
if(yamlKeys == null) {
183+
YAMLValue topLevelValue = yamlDocument.getTopLevelValue();
184+
if(!(topLevelValue instanceof YAMLMapping)) {
190185
return null;
191186
}
192187

193188
Collection<Pair<String, String>> pairs = new ArrayList<Pair<String, String>>();
194189

195-
for (YAMLKeyValue yamlKey : yamlKeys) {
190+
for (YAMLKeyValue yamlKey : ((YAMLMapping) topLevelValue).getKeyValues()) {
196191
String keyText = yamlKey.getKeyText();
197192
if(StringUtils.isBlank(keyText)) {
198193
continue;

src/fr/adrienbrault/idea/symfony2plugin/form/util/FormUtil.java

+16-36
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
import org.apache.commons.lang.StringUtils;
3030
import org.jetbrains.annotations.NotNull;
3131
import org.jetbrains.annotations.Nullable;
32-
import org.jetbrains.yaml.psi.*;
32+
import org.jetbrains.yaml.psi.YAMLFile;
33+
import org.jetbrains.yaml.psi.YAMLKeyValue;
3334

3435
import java.util.*;
3536

@@ -214,43 +215,22 @@ public static void attachFormAliasesCompletions(@NotNull PhpClass phpClass, @Not
214215
}
215216
}
216217

217-
public static Map<String, Set<String>> getTags(YAMLFile psiFile) {
218+
/**
219+
* acme_demo.form.type.gender:
220+
* class: espend\Form\TypeBundle\Form\FooType
221+
* tags:
222+
* - { name: form.type, alias: foo_type_alias }
223+
* - { name: foo }
224+
*/
225+
@NotNull
226+
public static Map<String, Set<String>> getTags(@NotNull YAMLFile yamlFile) {
218227

219228
Map<String, Set<String>> map = new HashMap<String, Set<String>>();
220-
221-
YAMLDocument yamlDocument = PsiTreeUtil.getChildOfType(psiFile, YAMLDocument.class);
222-
if(yamlDocument == null) {
223-
return map;
224-
}
225-
226-
// get services or parameter key
227-
YAMLKeyValue[] yamlKeys = PsiTreeUtil.getChildrenOfType(yamlDocument, YAMLKeyValue.class);
228-
if(yamlKeys == null) {
229-
return map;
230-
}
231-
232-
/**
233-
* acme_demo.form.type.gender:
234-
* class: espend\Form\TypeBundle\Form\FooType
235-
* tags:
236-
* - { name: form.type, alias: foo_type_alias }
237-
* - { name: foo }
238-
*/
239-
240-
for(YAMLKeyValue yamlKeyValue : yamlKeys) {
241-
String yamlConfigKey = yamlKeyValue.getName();
242-
if(yamlConfigKey != null && yamlConfigKey.equals("services")) {
243-
244-
for(YAMLKeyValue yamlServiceKeyValue : PsiTreeUtil.getChildrenOfTypeAsList(yamlKeyValue.getValue(), YAMLKeyValue.class)) {
245-
String serviceName = yamlServiceKeyValue.getName();
246-
247-
Set<String> serviceTagMap = YamlHelper.collectServiceTags(yamlServiceKeyValue);
248-
if(serviceTagMap != null && serviceTagMap.size() > 0) {
249-
map.put(serviceName, serviceTagMap);
250-
}
251-
252-
}
253-
229+
for(YAMLKeyValue yamlServiceKeyValue : YamlHelper.getQualifiedKeyValuesInFile(yamlFile, "services")) {
230+
String serviceName = yamlServiceKeyValue.getName();
231+
Set<String> serviceTagMap = YamlHelper.collectServiceTags(yamlServiceKeyValue);
232+
if(serviceTagMap != null && serviceTagMap.size() > 0) {
233+
map.put(serviceName, serviceTagMap);
254234
}
255235
}
256236

src/fr/adrienbrault/idea/symfony2plugin/routing/RouteHelper.java

+37-38
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import com.jetbrains.php.lang.psi.elements.*;
3131
import fr.adrienbrault.idea.symfony2plugin.Settings;
3232
import fr.adrienbrault.idea.symfony2plugin.Symfony2Icons;
33-
import fr.adrienbrault.idea.symfony2plugin.Symfony2InterfacesUtil;
3433
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
3534
import fr.adrienbrault.idea.symfony2plugin.extension.RoutingLoader;
3635
import fr.adrienbrault.idea.symfony2plugin.extension.RoutingLoaderParameter;
@@ -56,6 +55,7 @@
5655
import org.jetbrains.annotations.NotNull;
5756
import org.jetbrains.annotations.Nullable;
5857
import org.jetbrains.yaml.YAMLFileType;
58+
import org.jetbrains.yaml.YAMLUtil;
5959
import org.jetbrains.yaml.psi.*;
6060

6161
import java.io.File;
@@ -599,55 +599,54 @@ public boolean process(VirtualFile virtualFile) {
599599
}
600600

601601
@NotNull
602-
public static Collection<StubIndexedRoute> getYamlRouteDefinitions(YAMLDocument yamlDocument) {
603-
602+
public static Collection<StubIndexedRoute> getYamlRouteDefinitions(@NotNull YAMLDocument yamlDocument) {
604603
Collection<StubIndexedRoute> indexedRoutes = new ArrayList<StubIndexedRoute>();
605604

606-
// get services or parameter key
607-
YAMLKeyValue[] yamlKeys = PsiTreeUtil.getChildrenOfType(yamlDocument, YAMLKeyValue.class);
608-
if(yamlKeys == null) {
609-
return Collections.emptyList();
610-
}
605+
for(YAMLKeyValue yamlKeyValue : YamlHelper.getTopLevelKeyValues((YAMLFile) yamlDocument.getContainingFile())) {
611606

612-
for(YAMLKeyValue yamlKeyValue : yamlKeys) {
607+
YAMLValue element = yamlKeyValue.getValue();
613608

614-
PsiElement element = yamlKeyValue.getValue();
615-
if(element instanceof YAMLCompoundValue) {
616-
Set<String> keySet = YamlHelper.getYamlCompoundValueKeyNames((YAMLCompoundValue) element);
609+
YAMLKeyValue path = YAMLUtil.findKeyInProbablyMapping(element, "path");
617610

618-
if((keySet.contains("path") || keySet.contains("pattern"))) {
619-
// cleanup: 'foo', "foo"
620-
String keyText = StringUtils.strip(StringUtils.strip(yamlKeyValue.getKeyText(), "'"), "\"");
621-
if(StringUtils.isNotBlank(keyText)) {
622-
StubIndexedRoute route = new StubIndexedRoute(keyText);
611+
// Symfony bc
612+
if(path == null) {
613+
path = YAMLUtil.findKeyInProbablyMapping(element, "pattern");
614+
}
623615

624-
String routePath = YamlHelper.getYamlKeyValueAsString((YAMLCompoundValue) element, "path", false);
625-
if(routePath == null) {
626-
routePath = YamlHelper.getYamlKeyValueAsString((YAMLCompoundValue) element, "pattern", false);
627-
}
616+
if(path == null) {
617+
continue;
618+
}
628619

629-
if(routePath != null && StringUtils.isNotBlank(routePath)) {
630-
route.setPath(routePath);
631-
}
620+
// cleanup: 'foo', "foo"
621+
String keyText = StringUtils.strip(StringUtils.strip(yamlKeyValue.getKeyText(), "'"), "\"");
622+
if(StringUtils.isBlank(keyText)) {
623+
continue;
624+
}
632625

633-
String methods = YamlHelper.getYamlKeyValueAsString((YAMLCompoundValue) element, "methods", false);
634-
if(methods != null) {
635-
// value: [GET, POST,
636-
String[] split = methods.replace("[", "").replace("]", "").replaceAll(" +", "").split(",");
637-
if(split.length > 0) {
638-
route.addMethod(split);
639-
}
640-
}
626+
StubIndexedRoute route = new StubIndexedRoute(keyText);
641627

642-
String controller = getYamlController(yamlKeyValue);
643-
if(controller != null) {
644-
route.setController(controller);
645-
}
628+
String routePath = path.getValueText();
629+
if(StringUtils.isNotBlank(routePath)) {
630+
route.setPath(routePath);
631+
}
646632

647-
indexedRoutes.add(route);
648-
}
633+
String methods = YamlHelper.getStringValueOfKeyInProbablyMapping(element, "methods");
634+
if(methods != null) {
635+
// value: [GET, POST,
636+
String[] split = methods.replace("[", "").replace("]", "").replaceAll(" +", "").split(",");
637+
if(split.length > 0) {
638+
route.addMethod(split);
649639
}
650640
}
641+
642+
String controller = getYamlController(yamlKeyValue);
643+
if(controller != null) {
644+
route.setController(controller);
645+
}
646+
647+
indexedRoutes.add(route);
648+
649+
651650
}
652651

653652
return indexedRoutes;

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

+1-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.intellij.psi.PsiElement;
44
import com.intellij.psi.PsiFile;
5-
import com.intellij.psi.util.PsiTreeUtil;
65
import com.intellij.psi.xml.XmlFile;
76
import com.intellij.psi.xml.XmlTag;
87
import com.intellij.util.Consumer;
@@ -15,7 +14,6 @@
1514
import org.apache.commons.lang.StringUtils;
1615
import org.jetbrains.annotations.NotNull;
1716
import org.jetbrains.annotations.Nullable;
18-
import org.jetbrains.yaml.psi.YAMLDocument;
1917
import org.jetbrains.yaml.psi.YAMLFile;
2018
import org.jetbrains.yaml.psi.YAMLKeyValue;
2119

@@ -37,12 +35,7 @@ public static void visitFile(@NotNull PsiFile psiFile, @NotNull Consumer<FileRes
3735
* resources: 'FOO'
3836
*/
3937
private static void visitYamlFile(@NotNull YAMLFile yamlFile, @NotNull Consumer<FileResourceConsumer> consumer) {
40-
YAMLDocument yamlDocument = PsiTreeUtil.getChildOfType(yamlFile, YAMLDocument.class);
41-
if(yamlDocument == null) {
42-
return;
43-
}
44-
45-
for (YAMLKeyValue yamlKeyValue : PsiTreeUtil.getChildrenOfTypeAsList(yamlDocument, YAMLKeyValue.class)) {
38+
for (YAMLKeyValue yamlKeyValue : YamlHelper.getTopLevelKeyValues(yamlFile)) {
4639
YAMLKeyValue resourceKey = YamlHelper.getYamlKeyValue(yamlKeyValue, "resource", true);
4740
if(resourceKey == null) {
4841
continue;

0 commit comments

Comments
 (0)