Skip to content

Named arguments in bind should provide navigation with #1241 #1249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,22 @@ public static PsiElementPattern.Capture<PsiElement> getTagsAsSequencePattern() {
);
}

/**
* services:
* _defaults:
* bind:
* $fo<caret>obar: '@foobar'
*/
public static PsiElementPattern.Capture<PsiElement> getBindArgumentPattern() {
return PlatformPatterns.psiElement().withParent(
PlatformPatterns.psiElement(YAMLKeyValue.class).withParent(
PlatformPatterns.psiElement(YAMLMapping.class).withParent(
PlatformPatterns.psiElement(YAMLKeyValue.class).withName("bind")
)
)
);
}

/**
* !tagged twig.extension
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.yaml.YAMLTokenTypes;
import org.jetbrains.yaml.psi.YAMLKeyValue;
import org.jetbrains.yaml.psi.YAMLMapping;
import org.jetbrains.yaml.psi.YAMLScalar;
import org.jetbrains.yaml.psi.YAMLSequenceItem;
import org.jetbrains.yaml.psi.*;

import java.util.*;

Expand Down Expand Up @@ -92,8 +89,16 @@ public PsiElement[] getGotoDeclarationTargets(PsiElement psiElement, int i, Edit
if (elementType == YAMLTokenTypes.SCALAR_KEY) {
String psiText = PsiElementUtils.getText(psiElement);

if(psiText.startsWith("$")) {
targets.addAll(namedArgumentGoto(psiElement)) ;
if (psiText.startsWith("$")) {
targets.addAll(namedArgumentGoto(psiElement));

// services:
// _defaults:
// bind:
// $fo<caret>obar: '@foobar'
if (YamlElementPatternHelper.getBindArgumentPattern().accepts(psiElement)) {
targets.addAll(namedDefaultBindArgumentGoto(psiElement, psiText));
}
}
}

Expand Down Expand Up @@ -176,7 +181,29 @@ public PsiElement[] getGotoDeclarationTargets(PsiElement psiElement, int i, Edit
return targets.toArray(new PsiElement[targets.size()]);
}

private Collection<? extends PsiElement> namedArgumentGoto(PsiElement psiElement) {
private Collection<? extends PsiElement> namedDefaultBindArgumentGoto(@NotNull PsiElement psiElement, @NotNull String parameterName) {
Collection<PsiElement> psiElements = new HashSet<>();

PsiFile containingFile = psiElement.getContainingFile();
if (containingFile instanceof YAMLFile) {
for (PhpClass phpClass : YamlHelper.getPhpClassesInYamlFile((YAMLFile) containingFile, new ContainerCollectionResolver.LazyServiceCollector(psiElement.getProject()))) {
Method constructor = phpClass.getConstructor();
if (constructor == null) {
continue;
}

for (Parameter parameter : constructor.getParameters()) {
if (parameter.getName().equals(parameterName.substring(1))) {
psiElements.add(parameter);
}
}
}
}

return psiElements;
}

private Collection<? extends PsiElement> namedArgumentGoto(@NotNull PsiElement psiElement) {
Collection<PsiElement> psiElements = new HashSet<>();

Parameter yamlNamedArgument = ServiceContainerUtil.getYamlNamedArgument(psiElement, new ContainerCollectionResolver.LazyServiceCollector(psiElement.getProject()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ public static Parameter getYamlNamedArgument(@NotNull PsiElement psiElement, @No
* arguments: ~
*/
@Nullable
private static String getServiceClassFromServiceMapping(@NotNull YAMLMapping yamlMapping) {
public static String getServiceClassFromServiceMapping(@NotNull YAMLMapping yamlMapping) {
YAMLKeyValue classKeyValue = yamlMapping.getKeyValueByKey("class");

// Symfony 3.3: "class" is optional; use service id for class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@
import com.intellij.util.Processor;
import com.intellij.util.containers.ContainerUtil;
import com.jetbrains.php.lang.psi.elements.Parameter;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.jetbrains.php.refactoring.PhpNameUtil;
import fr.adrienbrault.idea.symfony2plugin.dic.ParameterResolverConsumer;
import fr.adrienbrault.idea.symfony2plugin.dic.container.util.ServiceContainerUtil;
import fr.adrienbrault.idea.symfony2plugin.dic.tags.yaml.StaticAttributeResolver;
import fr.adrienbrault.idea.symfony2plugin.stubs.ContainerCollectionResolver;
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils;
import fr.adrienbrault.idea.symfony2plugin.util.dict.ServiceUtil;
import fr.adrienbrault.idea.symfony2plugin.util.yaml.visitor.ParameterVisitor;
import fr.adrienbrault.idea.symfony2plugin.util.yaml.visitor.YamlServiceTag;
import fr.adrienbrault.idea.symfony2plugin.util.yaml.visitor.YamlTagVisitor;
Expand All @@ -34,6 +39,7 @@
import org.jetbrains.yaml.YAMLUtil;
import org.jetbrains.yaml.psi.*;
import org.jetbrains.yaml.psi.impl.YAMLHashImpl;
import org.jetbrains.yaml.psi.impl.YAMLPlainTextImpl;

import java.util.*;

Expand Down Expand Up @@ -1168,4 +1174,33 @@ public boolean accepts(@NotNull PsiElement psiElement, ProcessingContext process
return psiElement.getNextSibling() instanceof YAMLMapping;
}
}

@NotNull
public static Collection<PhpClass> getPhpClassesInYamlFile(@NotNull YAMLFile yamlFile, @NotNull ContainerCollectionResolver.LazyServiceCollector lazyServiceCollector) {
Collection<PhpClass> phpClasses = new HashSet<>();

for (YAMLKeyValue keyValue : YamlHelper.getQualifiedKeyValuesInFile(yamlFile, "services")) {
YAMLValue value = keyValue.getValue();
if (value instanceof YAMLMapping) {
// foo.bar:
// classes: ...
String serviceId = ServiceContainerUtil.getServiceClassFromServiceMapping((YAMLMapping) value);
if (StringUtils.isNotBlank(serviceId)) {
PhpClass serviceClass = ServiceUtil.getResolvedClassDefinition(yamlFile.getProject(), serviceId, lazyServiceCollector);
if (serviceClass != null) {
phpClasses.add(serviceClass);
}
}
} else if(value instanceof YAMLPlainTextImpl) {
// Foo\Bar: ~
String text = keyValue.getKeyText();

if (StringUtils.isNotBlank(text) && YamlHelper.isClassServiceId(text)) {
phpClasses.addAll(PhpElementsUtil.getClassesInterface(yamlFile.getProject(), text));
}
}
}

return phpClasses;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,27 @@ public void testNamedArgumentsNavigationForService() {
);
}

public void testNamedArgumentsNavigationForDefaultBinding() {
assertNavigationMatch("services.yml", "" +
"services:\n" +
" _defaults:\n" +
" bind:\n" +
" $<caret>i: ~\n"+
" Foo\\Bar: ~" +
PlatformPatterns.psiElement(Parameter.class)
);

assertNavigationMatch("services.yml", "" +
"services:\n" +
" _defaults:\n" +
" bind:\n" +
" $<caret>i: ~\n"+
" foobar:\n" +
" class: Foo\\Bar\n" +
PlatformPatterns.psiElement(Parameter.class)
);
}

@NotNull
private PsiElementPattern.Capture<PhpClass> getClassPattern() {
return PlatformPatterns.psiElement(PhpClass.class);
Expand Down