Skip to content

Named arguments should be clickable #1240 #1247

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
Oct 30, 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 @@ -11,6 +11,7 @@
import com.intellij.psi.tree.IElementType;
import com.jetbrains.php.PhpIndex;
import com.jetbrains.php.lang.psi.elements.Method;
import com.jetbrains.php.lang.psi.elements.Parameter;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
import fr.adrienbrault.idea.symfony2plugin.config.EventDispatcherSubscriberUtil;
Expand Down Expand Up @@ -88,6 +89,14 @@ 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)) ;
}
}

// yaml Plugin BC: "!php/const:" is a tag
if(elementType == YAMLTokenTypes.TAG) {
String psiText = PsiElementUtils.getText(psiElement);
Expand Down Expand Up @@ -167,6 +176,17 @@ public PsiElement[] getGotoDeclarationTargets(PsiElement psiElement, int i, Edit
return targets.toArray(new PsiElement[targets.size()]);
}

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

Parameter yamlNamedArgument = ServiceContainerUtil.getYamlNamedArgument(psiElement, new ContainerCollectionResolver.LazyServiceCollector(psiElement.getProject()));
if (yamlNamedArgument != null) {
psiElements.add(yamlNamedArgument);
}

return psiElements;
}

private boolean hasNewConst(@NotNull PsiElement psiElement) {
PsiElement prevSibling = psiElement.getPrevSibling();
while (prevSibling != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.jetbrains.php.PhpIndex;
import com.jetbrains.php.lang.psi.elements.Field;
import com.jetbrains.php.lang.psi.elements.Method;
import com.jetbrains.php.lang.psi.elements.Parameter;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import fr.adrienbrault.idea.symfony2plugin.config.xml.XmlHelper;
import fr.adrienbrault.idea.symfony2plugin.dic.attribute.value.AttributeValueInterface;
Expand Down Expand Up @@ -366,6 +367,42 @@ public static ServiceTypeHint getYamlConstructorTypeHint(@NotNull YAMLScalar yam
return null;
}

/**
* arguments: ['$foobar': '@foo']
*/
@Nullable
public static Parameter getYamlNamedArgument(@NotNull PsiElement psiElement, @NotNull ContainerCollectionResolver.LazyServiceCollector lazyServiceCollector) {
PsiElement context = psiElement.getContext();
if(context instanceof YAMLKeyValue) {
// arguments: ['$foobar': '@foo']

String parameterName = ((YAMLKeyValue) context).getKeyText();
if(parameterName.startsWith("$") && parameterName.length() > 1) {
PsiElement yamlMapping = context.getParent();
if(yamlMapping instanceof YAMLMapping) {
PsiElement yamlKeyValue = yamlMapping.getParent();
if(yamlKeyValue instanceof YAMLKeyValue) {
String keyText = ((YAMLKeyValue) yamlKeyValue).getKeyText();
if(keyText.equals("arguments")) {
YAMLMapping parentMapping = ((YAMLKeyValue) yamlKeyValue).getParentMapping();
if(parentMapping != null) {
String serviceId = getServiceClassFromServiceMapping(parentMapping);
if(StringUtils.isNotBlank(serviceId)) {
PhpClass serviceClass = ServiceUtil.getResolvedClassDefinition(psiElement.getProject(), serviceId, lazyServiceCollector);
if(serviceClass != null) {
return PhpElementsUtil.getConstructorParameterArgumentByName(serviceClass, StringUtils.stripStart(parameterName, "$"));
}
}
}
}
}
}
}
}

return null;
}

/**
* Symfony 3.3: "class" is optional; use service name for its it
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1299,6 +1299,21 @@ public static Collection<String> getTernaryExpressionConditionStrings(@NotNull T
return types;
}

/**
* Find argument by name in constructor parameter: __construct($foobar)
*/
@Nullable
public static Parameter getConstructorParameterArgumentByName(@NotNull PhpClass phpClass, @NotNull String argumentName) {
Method constructor = phpClass.getConstructor();
if(constructor == null) {
return null;
}

return Arrays.stream(constructor.getParameters()).filter(
parameter -> argumentName.equals(parameter.getName())
).findFirst().orElse(null);
}

/**
* Find argument by name in constructor parameter: __construct($foobar)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.intellij.patterns.PsiElementPattern;
import com.jetbrains.php.lang.PhpFileType;
import com.jetbrains.php.lang.psi.elements.Method;
import com.jetbrains.php.lang.psi.elements.Parameter;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import fr.adrienbrault.idea.symfony2plugin.tests.SymfonyLightCodeInsightFixtureTestCase;
Expand Down Expand Up @@ -226,6 +227,16 @@ public void testThatNavigationForControllerInvokeMethodIsAvailable() {
);
}

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

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