10000 add lazy decorated service resolving to fix Argument 'decorated' of S… · Koc/idea-php-symfony2-plugin@6ef034a · GitHub
[go: up one dir, main page]

Skip to content

Commit 6ef034a

Browse files
committed
add lazy decorated service resolving to fix Argument 'decorated' of ServiceUtil.getLineMarkerForDecoratedServiceId must not be null Haehnchen#982
1 parent 39db522 commit 6ef034a

File tree

3 files changed

+57
-21
lines changed

3 files changed

+57
-21
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package fr.adrienbrault.idea.symfony2plugin.dic.linemarker;
2+
3+
import com.intellij.openapi.project.Project;
4+
import fr.adrienbrault.idea.symfony2plugin.dic.ContainerService;
5+
import fr.adrienbrault.idea.symfony2plugin.stubs.ServiceIndexUtil;
6+
import org.jetbrains.annotations.NotNull;
7+
import org.jetbrains.annotations.Nullable;
8+
9+
import java.util.Collection;
10+
import java.util.Map;
11+
12+
/**
13+
* Proxy to load decorated services lazily
14+
*
15+
* @author Daniel Espendiller <daniel@espendiller.net>
16+
*/
17+
class LazyDecoratedServiceValues {
18+
@NotNull
19+
private final Project project;
20+
21+
@Nullable
22+
private Map<String, Collection<ContainerService>> map;
23+
24+
LazyDecoratedServiceValues(@NotNull Project project) {
25+
this.project = project;
26+
}
27+
28+
@NotNull
29+
public Map<String, Collection<ContainerService>> getDecoratedServices() {
30+
if(this.map == null) {
31+
this.map = ServiceIndexUtil.getDecoratedServices(this.project);
32+
}
33+
34+
return map;
35+
}
36+
}

src/fr/adrienbrault/idea/symfony2plugin/dic/linemarker/XmlLineMarkerProvider.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
1111
import fr.adrienbrault.idea.symfony2plugin.config.xml.XmlHelper;
1212
import fr.adrienbrault.idea.symfony2plugin.dic.ContainerService;
13-
import fr.adrienbrault.idea.symfony2plugin.stubs.ServiceIndexUtil;
1413
import fr.adrienbrault.idea.symfony2plugin.util.dict.ServiceUtil;
1514
import org.apache.commons.lang.StringUtils;
1615
import org.jetbrains.annotations.NotNull;
@@ -40,37 +39,37 @@ public void collectSlowLineMarkers(@NotNull List<PsiElement> psiElements, @NotNu
4039
return;
4140
}
4241

42+
LazyDecoratedServiceValues lazyDecoratedServiceValues = null;
43+
4344
for (PsiElement psiElement : psiElements) {
45+
if(lazyDecoratedServiceValues == null) {
46+
lazyDecoratedServiceValues = new LazyDecoratedServiceValues(psiElements.get(0).getProject());
47+
}
48+
4449
// <services><service id="foo"/></services>
4550
if(psiElement instanceof XmlTag && getServiceIdPattern().accepts(psiElement)) {
46-
visitServiceId((XmlTag) psiElement, result);
51+
visitServiceId((XmlTag) psiElement, result, lazyDecoratedServiceValues);
4752
}
4853
}
49-
50-
decoratedServiceCache = null;
5154
}
5255

5356
/**
5457
* <service id="foo"/>
5558
*/
56-
private void visitServiceId(@NotNull XmlTag xmlTag, @NotNull Collection<LineMarkerInfo> result) {
59+
private void visitServiceId(@NotNull XmlTag xmlTag, @NotNull Collection<LineMarkerInfo> result, @NotNull LazyDecoratedServiceValues lazyDecoratedServiceValues) {
5760
String id = xmlTag.getAttributeValue("id");
5861
if(StringUtils.isBlank(id)) {
5962
return;
6063
}
6164

6265
// <service id="foo" decorates=foobar" />
6366
String decorates = xmlTag.getAttributeValue("decorates");
64-
if(StringUtils.isNotBlank(decorates)) {
67+
if(decorates != null && StringUtils.isNotBlank(decorates)) {
6568
result.add(ServiceUtil.getLineMarkerForDecoratesServiceId(xmlTag, decorates, result));
6669
}
6770

68-
if(this.decoratedServiceCache == null) {
69-
this.decoratedServiceCache = ServiceIndexUtil.getDecoratedServices(xmlTag.getProject());
70-
}
71-
7271
NavigationGutterIconBuilder<PsiElement> lineMarker = ServiceUtil.getLineMarkerForDecoratedServiceId(
73-
xmlTag.getProject(), this.decoratedServiceCache, id
72+
xmlTag.getProject(), lazyDecoratedServiceValues.getDecoratedServices(), id
7473
);
7574

7675
if(lineMarker == null) {

src/fr/adrienbrault/idea/symfony2plugin/dic/linemarker/YamlLineMarkerProvider.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
88
import fr.adrienbrault.idea.symfony2plugin.config.yaml.YamlElementPatternHelper;
99
import fr.adrienbrault.idea.symfony2plugin.dic.ContainerService;
10-
import fr.adrienbrault.idea.symfony2plugin.stubs.ServiceIndexUtil;
1110
import fr.adrienbrault.idea.symfony2plugin.util.dict.ServiceUtil;
1211
import fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper;
1312
import org.apache.commons.lang.StringUtils;
@@ -39,32 +38,34 @@ public void collectSlowLineMarkers(@NotNull List<PsiElement> psiElements, @NotNu
3938
return;
4039
}
4140

41+
final LazyDecoratedServiceValues[] lazyDecoratedServices = {null};
42+
4243
// services -> service_name
4344
psiElements.stream()
4445
.filter(psiElement -> psiElement instanceof YAMLKeyValue && YamlElementPatternHelper.getServiceIdKeyValuePattern().accepts(psiElement))
45-
.forEach(psiElement -> visitServiceId((YAMLKeyValue) psiElement, result));
46+
.forEach((PsiElement psiElement) -> {
47+
if(lazyDecoratedServices[0] == null) {
48+
lazyDecoratedServices[0] = new LazyDecoratedServiceValues(psiElements.get(0).getProject());
49+
}
4650

47-
decoratedServiceCache = null;
51+
visitServiceId((YAMLKeyValue) psiElement, result, lazyDecoratedServices[0]);
52+
});
4853
}
4954

50-
private void visitServiceId(@NotNull YAMLKeyValue yamlKeyValue, @NotNull Collection<LineMarkerInfo> result) {
55+
private void visitServiceId(@NotNull YAMLKeyValue yamlKeyValue, @NotNull Collection<LineMarkerInfo> result, @NotNull LazyDecoratedServiceValues lazyDecoratedServices) {
5156
String id = yamlKeyValue.getKeyText();
5257
if(StringUtils.isBlank(id)) {
5358
return;
5459
}
5560

5661
// decorates: @foobar
5762
String decorates = YamlHelper.getYamlKeyValueAsString(yamlKeyValue, "decorates");
58-
if(StringUtils.isNotBlank(decorates)) {
63+
if(decorates != null && StringUtils.isNotBlank(decorates)) {
5964
result.add(ServiceUtil.getLineMarkerForDecoratesServiceId(yamlKeyValue, decorates, result));
6065
}
6166

62-
if(this.decoratedServiceCache == null) {
63-
this.decoratedServiceCache = ServiceIndexUtil.getDecoratedServices(yamlKeyValue.getProject());
64-
}
65-
6667
NavigationGutterIconBuilder<PsiElement> lineMarker = ServiceUtil.getLineMarkerForDecoratedServiceId(
67-
yamlKeyValue.getProject(), this.decoratedServiceCache, id
68+
yamlKeyValue.getProject(), lazyDecoratedServices.getDecoratedServices(), id
6869
);
6970

7071
if(lineMarker == null) {

0 commit comments

Comments
 (0)
0