Skip to content

Commit 434a371

Browse files
authored
Merge pull request #1514 from Haehnchen/feature/linemarker-resource
provide linemarker for yaml service resource
2 parents 87d124e + 3ea9007 commit 434a371

File tree

3 files changed

+63
-21
lines changed

3 files changed

+63
-21
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/config/yaml/YamlGoToDeclarationHandler.java

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -492,35 +492,42 @@ private Collection<PsiElement> getEventGoto(@NotNull PsiElement psiElement) {
492492
* services:
493493
* My<caret>Class: ~
494494
*/
495-
private Collection<PsiElement> getClassesForServiceKey(@NotNull PsiElement psiElement) {
495+
private static Collection<PsiElement> getClassesForServiceKey(@NotNull PsiElement psiElement) {
496496
PsiElement parent = psiElement.getParent();
497497
if(parent instanceof YAMLKeyValue) {
498-
String valueText = ((YAMLKeyValue) parent).getKeyText();
499-
if (StringUtils.isNotBlank(valueText)) {
500-
Collection<PsiElement> targets = new HashSet<>();
501-
502-
// My<caret>Class\:
503-
// resource: '....'
504-
// exclude: '....'
505-
if (valueText.endsWith("\\")) {
506-
String resource = YamlHelper.getYamlKeyValueAsString((YAMLKeyValue) parent, "resource");
507-
if (resource != null) {
508-
String exclude = YamlHelper.getYamlKeyValueAsString((YAMLKeyValue) parent, "exclude");
509-
targets.addAll(getPhpClassFromResources(psiElement.getProject(), valueText, psiElement.getContainingFile().getVirtualFile(), resource, exclude));
510-
}
511-
}
498+
return getClassesForServiceKey((YAMLKeyValue) parent);
499+
}
500+
501+
return Collections.emptyList();
502+
}
503+
504+
@NotNull
505+
public static Collection<PsiElement> getClassesForServiceKey(@NotNull YAMLKeyValue yamlKeyValue) {
506+
String valueText = yamlKeyValue.getKeyText();
507+
if (StringUtils.isBlank(valueText)) {
508+
return Collections.emptyList();
509+
}
512510

513-
targets.addAll(PhpElementsUtil.getClassesInterface(psiElement.getProject(), valueText));
511+
Collection<PsiElement> targets = new HashSet<>();
514512

515-
return targets;
513+
// My<caret>Class\:
514+
// resource: '....'
515+
// exclude: '....'
516+
if (valueText.endsWith("\\")) {
517+
String resource = YamlHelper.getYamlKeyValueAsString(yamlKeyValue, "resource");
518+
if (resource != null) {
519+
String exclude = YamlHelper.getYamlKeyValueAsString(yamlKeyValue, "exclude");
520+
targets.addAll(getPhpClassFromResources(yamlKeyValue.getProject(), valueText, yamlKeyValue.getContainingFile().getVirtualFile(), resource, exclude));
516521
}
517522
}
518523

519-
return Collections.emptyList();
524+
targets.addAll(PhpElementsUtil.getClassesInterface(yamlKeyValue.getProject(), valueText));
525+
526+
return targets;
520527
}
521528

522529
@NotNull
523-
private Collection<PhpClass> getPhpClassFromResources(@NotNull Project project, @NotNull String namespace, @NotNull VirtualFile source, @NotNull String resource, @Nullable String exclude) {
530+
private static Collection<PhpClass> getPhpClassFromResources(@NotNull Project project, @NotNull String namespace, @NotNull VirtualFile source, @NotNull String resource, @Nullable String exclude) {
524531
Collection<PhpClass> phpClasses = new HashSet<>();
525532

526533
for (PhpClass phpClass : PhpIndexUtil.getPhpClassInsideNamespace(project, "\\" + StringUtils.strip(namespace, "\\"))) {

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
import com.intellij.codeInsight.daemon.LineMarkerInfo;
44
import com.intellij.codeInsight.daemon.LineMarkerProvider;
55
import com.intellij.codeInsight.navigation.NavigationGutterIconBuilder;
6+
import com.intellij.icons.AllIcons;
7+
import com.intellij.openapi.util.NotNullLazyValue;
68
import com.intellij.psi.PsiElement;
79
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
810
import fr.adrienbrault.idea.symfony2plugin.config.yaml.YamlElementPatternHelper;
11+
import fr.adrienbrault.idea.symfony2plugin.config.yaml.YamlGoToDeclarationHandler;
912
import fr.adrienbrault.idea.symfony2plugin.util.dict.ServiceUtil;
1013
import fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper;
1114
import org.apache.commons.lang.StringUtils;
@@ -52,7 +55,30 @@ public void collectSlowLineMarkers(@NotNull List<PsiElement> psiElements, @NotNu
5255

5356
// services -> service_name
5457
visitServiceId(psiElement, (YAMLKeyValue) yamlKeyValue, result, lazyDecoratedServices);
58+
59+
// services:
60+
// App\:
61+
// resource: '../src/*'
62+
visitServiceIdForResources(psiElement, (YAMLKeyValue) yamlKeyValue, result);
63+
}
64+
}
65+
66+
private void visitServiceIdForResources(PsiElement leafTarget, YAMLKeyValue yamlKeyValue, @NotNull Collection<LineMarkerInfo> result) {
67+
String resource = YamlHelper.getYamlKeyValueAsString(yamlKeyValue, "resource");
68+
if (resource == null) {
69+
return;
5570
}
71+
72+
result.add(NavigationGutterIconBuilder.create(AllIcons.Modules.SourceRoot)
73+
.setTargets(new NotNullLazyValue<Collection<? extends PsiElement>>() {
74+
@NotNull
75+
@Override
76+
protected Collection<? extends PsiElement> compute() {
77+
return YamlGoToDeclarationHandler.getClassesForServiceKey(yamlKeyValue);
78+
}
79+
})
80+
.setTooltipText("Navigate to class")
81+
.createLineMarkerInfo(leafTarget));
5682
}
5783

5884
private void visitServiceId(@NotNull PsiElement leafTarget, @NotNull YAMLKeyValue yamlKeyValue, @NotNull Collection<LineMarkerInfo> result, @NotNull LazyDecoratedParentServiceValues lazyDecoratedServices) {
@@ -63,13 +89,13 @@ private void visitServiceId(@NotNull PsiElement leafTarget, @NotNull YAMLKeyValu
6389

6490
// decorates: foobar
6591
String decorates = YamlHelper.getYamlKeyValueAsString(yamlKeyValue, "decorates");
66-
if(decorates != null && StringUtils.isNotBlank(decorates)) {
92+
if(StringUtils.isNotBlank(decorates)) {
6793
result.add(ServiceUtil.getLineMarkerForDecoratesServiceId(leafTarget, ServiceUtil.ServiceLineMarker.DECORATE, decorates));
6894
}
6995

7096
// parent: foobar
7197
String parent = YamlHelper.getYamlKeyValueAsString(yamlKeyValue, "parent");
72-
if(parent != null && StringUtils.isNotBlank(parent)) {
98+
if(StringUtils.isNotBlank(parent)) {
7399
result.add(ServiceUtil.getLineMarkerForDecoratesServiceId(leafTarget, ServiceUtil.ServiceLineMarker.PARENT, parent));
74100
}
75101

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/dic/linemarker/YamlLineMarkerProviderTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ public void testThatParentProvidesOverwriteMarker() {
6464
);
6565
}
6666

67+
public void testThatServiceResourceIsHavingLinemarker() {
68+
assertLineMarker(createYamlFile("" +
69+
"services:\n" +
70+
" App\\Controller\\:\n" +
71+
" resource: '../src/Controller'\n"),
72+
new LineMarker.ToolTipEqualsAssert("Navigate to class")
73+
);
74+
}
75+
6776
@NotNull
6877
private PsiElement createYamlFile(@NotNull String content) {
6978
return PsiFileFactory.getInstance(getProject()).createFileFromText("DUMMY__." + YAMLFileType.YML.getDefaultExtension(), YAMLFileType.YML, content);

0 commit comments

Comments
 (0)