Skip to content
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 @@ -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()]
*
Expand Down Expand Up @@ -65,6 +69,23 @@ protected void addCompletions(@NotNull CompletionParameters parameters, @NotNull
return;
}

Collection<LookupElement> 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<LookupElement> getControllerCompletions(@NotNull Project project) {
Collection<LookupElement> lookupElements = new ArrayList<>();

// Add Route attribute completion
if (PhpElementsUtil.getClassInterface(project, ROUTE_ATTRIBUTE_FQN) != null) {
LookupElement routeLookupElement = LookupElementBuilder
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,25 @@ 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;
}

if (!phpClass.getAttributes(AS_CONTROLLER_ATTRIBUTE).isEmpty()) {
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;
Expand All @@ -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;
}

Expand Down