Skip to content

Commit 41e2cc8

Browse files
committed
Enhance attribute completion by restricting #[Route()], #[IsGranted()], and #[Cache()] suggestions to controller classes.
1 parent 9e45a0c commit 41e2cc8

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/completion/PhpAttributeCompletionContributor.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616
import com.jetbrains.php.lang.psi.elements.PhpClass;
1717
import fr.adrienbrault.idea.symfony2plugin.Symfony2Icons;
1818
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
19+
import fr.adrienbrault.idea.symfony2plugin.intentions.php.AddRouteAttributeIntention;
1920
import fr.adrienbrault.idea.symfony2plugin.util.CodeUtil;
2021
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
2122
import org.apache.commons.lang3.StringUtils;
2223
import org.jetbrains.annotations.NotNull;
2324

25+
import java.util.ArrayList;
26+
import java.util.Collection;
27+
2428
/**
2529
* Provides completion for Symfony PHP attributes like #[Route()]
2630
*
@@ -65,6 +69,23 @@ protected void addCompletions(@NotNull CompletionParameters parameters, @NotNull
6569
return;
6670
}
6771

72+
Collection<LookupElement> lookupElements = new ArrayList<>();
73+
74+
PhpClass containingClass = method.getContainingClass();
75+
if (containingClass != null && AddRouteAttributeIntention.isControllerClass(containingClass)) {
76+
lookupElements.addAll(getControllerCompletions(project));
77+
}
78+
79+
// Stop here - don't show other completions when typing "#" for attributes
80+
if (!lookupElements.isEmpty()) {
81+
result.addAllElements(lookupElements);
82+
result.stopHere();
83+
}
84+
}
85+
86+
private Collection<LookupElement> getControllerCompletions(@NotNull Project project) {
87+
Collection<LookupElement> lookupElements = new ArrayList<>();
88+
6889
// Add Route attribute completion
6990
if (PhpElementsUtil.getClassInterface(project, ROUTE_ATTRIBUTE_FQN) != null) {
7091
LookupElement routeLookupElement = LookupElementBuilder
@@ -74,7 +95,7 @@ protected void addCompletions(@NotNull CompletionParameters parameters, @NotNull
7495
.withInsertHandler(new PhpAttributeInsertHandler(ROUTE_ATTRIBUTE_FQN, CursorPosition.INSIDE_QUOTES))
7596
.bold();
7697

77-
result.addElement(routeLookupElement);
98+
lookupElements.add(routeLookupElement);
7899
}
79100

80101
// Add IsGranted attribute completion
@@ -86,7 +107,7 @@ protected void addCompletions(@NotNull CompletionParameters parameters, @NotNull
86107
.withInsertHandler(new PhpAttributeInsertHandler(IS_GRANTED_ATTRIBUTE_FQN, CursorPosition.INSIDE_QUOTES))
87108
.bold();
88109

89-
result.addElement(isGrantedLookupElement);
110+
lookupElements.add(isGrantedLookupElement);
90111
}
91112

92113
// Add Cache attribute completion
@@ -98,11 +119,10 @@ protected void addCompletions(@NotNull CompletionParameters parameters, @NotNull
98119
.withInsertHandler(new PhpAttributeInsertHandler(CACHE_ATTRIBUTE_FQN, CursorPosition.INSIDE_PARENTHESES))
99120
.bold();
100121

101-
result.addElement(cacheLookupElement);
122+
lookupElements.add(cacheLookupElement);
102123
}
103124

104-
// Stop here - don't show other completions when typing "#" for attributes
105-
result.stopHere();
125+
return lookupElements;
106126
}
107127

108128
/**

src/main/java/fr/adrienbrault/idea/symfony2plugin/intentions/php/AddRouteAttributeIntention.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,25 @@ private boolean hasRouteAttribute(@NotNull Method method) {
131131
return false;
132132
}
133133

134-
private boolean isControllerClass(@NotNull PhpClass phpClass) {
135-
if (PhpElementsUtil.isInstanceOf(phpClass, ABSTRACT_CONTROLLER_CLASS)) {
134+
public static boolean isControllerClass(@NotNull PhpClass phpClass) {
135+
if (phpClass.getName().endsWith("Controller")) {
136136
return true;
137137
}
138138

139139
if (!phpClass.getAttributes(AS_CONTROLLER_ATTRIBUTE).isEmpty()) {
140140
return true;
141141
}
142142

143+
if (PhpElementsUtil.isInstanceOf(phpClass, ABSTRACT_CONTROLLER_CLASS)) {
144+
return true;
145+
}
146+
147+
for (String routeAnnotation : RouteHelper.ROUTE_ANNOTATIONS) {
148+
if (!phpClass.getAttributes(routeAnnotation).isEmpty()) {
149+
return true;
150+
}
151+
}
152+
143153
for (Method ownMethod : phpClass.getOwnMethods()) {
144154
if (!ownMethod.getAccess().isPublic() || ownMethod.isStatic()) {
145155
continue;
@@ -152,12 +162,6 @@ private boolean isControllerClass(@NotNull PhpClass phpClass) {
152162
}
153163
}
154164

155-
for (String routeAnnotation : RouteHelper.ROUTE_ANNOTATIONS) {
156-
if (!phpClass.getAttributes(routeAnnotation).isEmpty()) {
157-
return true;
158-
}
159-
}
160-
161165
return false;
162166
}
163167

0 commit comments

Comments
 (0)