diff --git a/src/main/java/fr/adrienbrault/idea/symfony2plugin/completion/PhpAttributeCompletionContributor.java b/src/main/java/fr/adrienbrault/idea/symfony2plugin/completion/PhpAttributeCompletionContributor.java index 0b1799ed3..aa5af24df 100644 --- a/src/main/java/fr/adrienbrault/idea/symfony2plugin/completion/PhpAttributeCompletionContributor.java +++ b/src/main/java/fr/adrienbrault/idea/symfony2plugin/completion/PhpAttributeCompletionContributor.java @@ -16,11 +16,15 @@ import com.jetbrains.php.lang.psi.elements.PhpClass; import fr.adrienbrault.idea.symfony2plugin.Symfony2Icons; import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent; +import fr.adrienbrault.idea.symfony2plugin.intentions.php.AddRouteAttributeIntention; import fr.adrienbrault.idea.symfony2plugin.util.CodeUtil; import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil; import org.apache.commons.lang3.StringUtils; import org.jetbrains.annotations.NotNull; +import java.util.ArrayList; +import java.util.Collection; + /** * Provides completion for Symfony PHP attributes like #[Route()] * @@ -65,6 +69,23 @@ protected void addCompletions(@NotNull CompletionParameters parameters, @NotNull return; } + Collection lookupElements = new ArrayList<>(); + + PhpClass containingClass = method.getContainingClass(); + if (containingClass != null && AddRouteAttributeIntention.isControllerClass(containingClass)) { + lookupElements.addAll(getControllerCompletions(project)); + } + + // Stop here - don't show other completions when typing "#" for attributes + if (!lookupElements.isEmpty()) { + result.addAllElements(lookupElements); + result.stopHere(); + } + } + + private Collection getControllerCompletions(@NotNull Project project) { + Collection lookupElements = new ArrayList<>(); + // Add Route attribute completion if (PhpElementsUtil.getClassInterface(project, ROUTE_ATTRIBUTE_FQN) != null) { LookupElement routeLookupElement = LookupElementBuilder @@ -74,7 +95,7 @@ protected void addCompletions(@NotNull CompletionParameters parameters, @NotNull .withInsertHandler(new PhpAttributeInsertHandler(ROUTE_ATTRIBUTE_FQN, CursorPosition.INSIDE_QUOTES)) .bold(); - result.addElement(routeLookupElement); + lookupElements.add(routeLookupElement); } // Add IsGranted attribute completion @@ -86,7 +107,7 @@ protected void addCompletions(@NotNull CompletionParameters parameters, @NotNull .withInsertHandler(new PhpAttributeInsertHandler(IS_GRANTED_ATTRIBUTE_FQN, CursorPosition.INSIDE_QUOTES)) .bold(); - result.addElement(isGrantedLookupElement); + lookupElements.add(isGrantedLookupElement); } // Add Cache attribute completion @@ -98,11 +119,10 @@ protected void addCompletions(@NotNull CompletionParameters parameters, @NotNull .withInsertHandler(new PhpAttributeInsertHandler(CACHE_ATTRIBUTE_FQN, CursorPosition.INSIDE_PARENTHESES)) .bold(); - result.addElement(cacheLookupElement); + lookupElements.add(cacheLookupElement); } - // Stop here - don't show other completions when typing "#" for attributes - result.stopHere(); + return lookupElements; } /** diff --git a/src/main/java/fr/adrienbrault/idea/symfony2plugin/intentions/php/AddRouteAttributeIntention.java b/src/main/java/fr/adrienbrault/idea/symfony2plugin/intentions/php/AddRouteAttributeIntention.java index db08fe97d..65516d02c 100644 --- a/src/main/java/fr/adrienbrault/idea/symfony2plugin/intentions/php/AddRouteAttributeIntention.java +++ b/src/main/java/fr/adrienbrault/idea/symfony2plugin/intentions/php/AddRouteAttributeIntention.java @@ -131,8 +131,8 @@ private boolean hasRouteAttribute(@NotNull Method method) { return false; } - private boolean isControllerClass(@NotNull PhpClass phpClass) { - if (PhpElementsUtil.isInstanceOf(phpClass, ABSTRACT_CONTROLLER_CLASS)) { + public static boolean isControllerClass(@NotNull PhpClass phpClass) { + if (phpClass.getName().endsWith("Controller")) { return true; } @@ -140,6 +140,16 @@ private boolean isControllerClass(@NotNull PhpClass phpClass) { return true; } + if (PhpElementsUtil.isInstanceOf(phpClass, ABSTRACT_CONTROLLER_CLASS)) { + return true; + } + + for (String routeAnnotation : RouteHelper.ROUTE_ANNOTATIONS) { + if (!phpClass.getAttributes(routeAnnotation).isEmpty()) { + return true; + } + } + for (Method ownMethod : phpClass.getOwnMethods()) { if (!ownMethod.getAccess().isPublic() || ownMethod.isStatic()) { continue; @@ -152,12 +162,6 @@ private boolean isControllerClass(@NotNull PhpClass phpClass) { } } - for (String routeAnnotation : RouteHelper.ROUTE_ANNOTATIONS) { - if (!phpClass.getAttributes(routeAnnotation).isEmpty()) { - return true; - } - } - return false; }