8000 provide targets for route annotations without defined "name" property · Ma27/idea-php-symfony2-plugin@69db84d · GitHub
[go: up one dir, main page]

Skip to content

Commit 69db84d

Browse files
committed
provide targets for route annotations without defined "name" property
1 parent c9cadd9 commit 69db84d

File tree

5 files changed

+89
-62
lines changed

5 files changed

+89
-62
lines changed

src/fr/adrienbrault/idea/symfony2plugin/routing/RouteHelper.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import fr.adrienbrault.idea.symfony2plugin.stubs.dict.StubIndexedRoute;
4040
import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.AnnotationRoutesStubIndex;
4141
import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.RoutesStubIndex;
42+
import fr.adrienbrault.idea.symfony2plugin.util.AnnotationBackportUtil;
4243
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
4344
import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils;
4445
import fr.adrienbrault.idea.symfony2plugin.util.SymfonyBundleUtil;
@@ -55,8 +56,6 @@
5556

5657
import java.io.File;
5758
import java.util.*;
58-
import java.util.regex.Matcher;
59-
import java.util.regex.Pattern;
6059

6160
public class RouteHelper {
6261

@@ -724,7 +723,7 @@ public static List<Route> getRoutesOnControllerAction(@NotNull Method method) {
724723
}
725724

726725
@Nullable
727-
public static PsiElement getRouteNameTarget(Project project, String routeName) {
726+
public static PsiElement getRouteNameTarget(@NotNull Project project, @NotNull String routeName) {
728727

729728
VirtualFile[] virtualFiles = RouteHelper.getRouteDefinitionInsideFile(project, routeName);
730729
for(VirtualFile virtualFile: virtualFiles) {
@@ -755,9 +754,14 @@ public static PsiElement getRouteNameTarget(Project project, String routeName) {
755754
PsiElement phpDocAttributeList = PsiElementUtils.getChildrenOfType(phpDocTag, PlatformPatterns.psiElement(PhpDocElementTypes.phpDocAttributeList));
756755
if(phpDocAttributeList != null) {
757756
// @TODO: use pattern
758-
Matcher matcher = Pattern.compile("name\\s*=\\s*\"(\\w+)\"").matcher(phpDocAttributeList.getText());
759-
if (matcher.find() && matcher.group(1).equals(routeName)) {
757+
String annotationRouteName = AnnotationBackportUtil.getAnnotationRouteName(phpDocAttributeList.getText());
758+
if(annotationRouteName != null) {
760759
return phpDocAttributeList;
760+
} else {
761+
String routeByMethod = AnnotationBackportUtil.getRouteByMethod(phpDocTag);
762+
if(routeName.equals(routeByMethod)) {
763+
return phpDocTag;
764+
}
761765
}
762766
}
763767
}

src/fr/adrienbrault/idea/symfony2plugin/stubs/indexes/AnnotationRoutesStubIndex.java

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
import java.io.IOException;
3333
import java.util.HashMap;
3434
import java.util.Map;
35-
import java.util.regex.Matcher;
36-
import java.util.regex.Pattern;
3735

3836
public class AnnotationRoutesStubIndex extends FileBasedIndexExtension<String, RouteInterface> {
3937

@@ -219,7 +217,7 @@ public void visitPhpDocTag(PhpDocTag phpDocTag) {
219217

220218
String routeName = AnnotationBackportUtil.getAnnotationRouteName(phpDocAttributeList.getText());
221219
if(routeName == null) {
222-
routeName = getRouteByMethod(phpDocTag);
220+
routeName = AnnotationBackportUtil.getRouteByMethod(phpDocTag);
223221
}
224222

225223
if(routeName != null && StringUtils.isNotBlank(routeName)) {
@@ -258,7 +256,7 @@ public void visitPhpDocTag(PhpDocTag phpDocTag) {
258256
*/
259257
@Nullable
260258
private String getController(@NotNull PhpDocTag phpDocTag) {
261-
Method method = getMethodScope(phpDocTag);
259+
Method method = AnnotationBackportUtil.getMethodScope(phpDocTag);
262260

263261
if(method != null) {
264262
PhpClass containingClass = method.getContainingClass();
@@ -277,59 +275,6 @@ private String getController(@NotNull PhpDocTag phpDocTag) {
277275
return null;
278276
}
279277

280-
/**
281-
* "@SensioBlogBundle/Controller/PostController.php => sensio_blog_post_index"
282-
*/
283-
private String getRouteByMethod(@NotNull PhpDocTag phpDocTag) {
284-
PhpPsiElement method = getMethodScope(phpDocTag);
285-
if (method == null) {
286-
return null;
287-
}
288-
289-
String name = method.getName();
290-
if(name == null) {
291-
return null;
292-
}
293-
294-
if(name.endsWith("Action")) {
295-
name = name.substring(0, name.length() - "Action".length());
296-
}
297-
298-
PhpClass containingClass = ((Method) method).getContainingClass();
299-
if(containingClass == null) {
300-
return null;
301-
}
302-
303-
String fqn = containingClass.getFQN();
304-
if(fqn != null) {
305-
Matcher matcher = Pattern.compile("\\\\(\\w+)Bundle\\\\Controller\\\\(\\w+)Controller").matcher(fqn);
306-
if (matcher.find()) {
307-
return String.format("%s_%s_%s",
308-
fr.adrienbrault.idea.symfony2plugin.util.StringUtils.underscore(matcher.group(1)),
309-
fr.adrienbrault.idea.symfony2plugin.util.StringUtils.underscore(matcher.group(2)),
310-
name
311-
);
312-
}
313-
}
314-
315-
return null;
316-
}
317-
318-
@Nullable
319-
private Method getMethodScope(@NotNull PhpDocTag phpDocTag) {
320-
PhpDocComment parentOfType = PsiTreeUtil.getParentOfType(phpDocTag, PhpDocComment.class);
321-
if(parentOfType == null) {
322-
return null;
323-
}
324-
325-
PhpPsiElement method = parentOfType.getNextPsiSibling();
326-
if(!(method instanceof Method)) {
327-
return null;
328-
}
329-
330-
return (Method) method;
331-
}
332-
333278
@Nullable
334279
private String getClassRoutePattern(@NotNull PhpDocTag phpDocTag) {
335280
PhpClass phpClass = PsiTreeUtil.getParentOfType(phpDocTag, PhpClass.class);

src/fr/adrienbrault/idea/symfony2plugin/util/AnnotationBackportUtil.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package fr.adrienbrault.idea.symfony2plugin.util;
22

3+
import com.intellij.patterns.PlatformPatterns;
34
import com.intellij.psi.PsiElement;
45
import com.intellij.psi.PsiFile;
56
import com.intellij.psi.PsiRecursiveElementWalkingVisitor;
67
import com.intellij.psi.util.PsiTreeUtil;
78
import com.jetbrains.php.codeInsight.PhpCodeInsightUtil;
89
import com.jetbrains.php.lang.PhpLangUtil;
910
import com.jetbrains.php.lang.documentation.phpdoc.PhpDocUtil;
11+
import com.jetbrains.php.lang.documentation.phpdoc.parser.PhpDocElementTypes;
1012
import com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocComment;
1113
import com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocTag;
1214
import com.jetbrains.php.lang.psi.PhpPsiUtil;
1315
import com.jetbrains.php.lang.psi.elements.*;
16+
import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.AnnotationRoutesStubIndex;
1417
import org.jetbrains.annotations.NotNull;
1518
import org.jetbrains.annotations.Nullable;
1619

@@ -214,4 +217,58 @@ public static String getQualifiedName(@NotNull PsiElement psiElement, @NotNull S
214217

215218
return qualifiedName;
216219
}
220+
221+
/**
222+
* "@SensioBlogBundle/Controller/PostController.php => sensio_blog_post_index"
223+
*/
224+
public static String getRouteByMethod(@NotNull PhpDocTag phpDocTag) {
225+
PhpPsiElement method = getMethodScope(phpDocTag);
226+
if (method == null) {
227+
return null;
228+
}
229+
230+
String name = method.getName();
231+
if(name == null) {
232+
return null;
233+
}
234+
235+
if(name.endsWith("Action")) {
236+
name = name.substring(0, name.length() - "Action".length());
237+
}
238+
239+
PhpClass containingClass = ((Method) method).getContainingClass();
240+
if(containingClass == null) {
241+
return null;
242+
}
243+
244+
String fqn = containingClass.getFQN();
245+
if(fqn != null) {
246+
Matcher matcher = Pattern.compile("\\\\(\\w+)Bundle\\\\Controller\\\\(\\w+)Controller").matcher(fqn);
247+
if (matcher.find()) {
248+
return String.format("%s_%s_%s",
249+
fr.adrienbrault.idea.symfony2plugin.util.StringUtils.underscore(matcher.group(1)),
250+
fr.adrienbrault.idea.symfony2plugin.util.StringUtils.underscore(matcher.group(2)),
251+
name
252+
);
253+
}
254+
}
255+
256+
return null;
257+
}
258+
259+
@Nullable
260+
public static Method getMethodScope(@NotNull PhpDocTag phpDocTag) {
261+
PhpDocComment parentOfType = PsiTreeUtil.getParentOfType(phpDocTag, PhpDocComment.class);
262+
if(parentOfType == null) {
263+
return null;
264+
}
265+
266+
PhpPsiElement method = parentOfType.getNextPsiSibling();
267+
if(!(method instanceof Method)) {
268+
return null;
269+
}
270+
271+
return (Method) method;
272+
}
273+
217274
}

tests/fr/adrienbrault/idea/symfony2plugin/tests/routing/RouteHelperTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.intellij.codeInsight.lookup.LookupElement;
44
import com.intellij.ide.highlighter.XmlFileType;
55
import com.intellij.openapi.util.Condition;
6+
import com.intellij.psi.PsiElement;
67
import com.intellij.psi.PsiFileFactory;
78
import com.intellij.psi.xml.XmlFile;
89
import com.intellij.util.containers.ContainerUtil;
@@ -227,6 +228,19 @@ public void testGetRoutesInsideUrlGeneratorFile() {
227228
assertNull(routes.get("_assetic_91dd2a8"));
228229
}
229230

231+
/**
232+
* @see fr.adrienbrault.idea.symfony2plugin.routing.RouteHelper#getRouteNameTarget
233+
*/
234+
public void testGetRouteNameTarget() {
235+
PsiElement element = RouteHelper.getRouteNameTarget(getProject(), "my_car_foo_stuff");
236+
assertNotNull(element);
237+
assertTrue(element.getText().contains("my_car_foo_stuff"));
238+
239+
element = RouteHelper.getRouteNameTarget(getProject(), "my_foo_bar_car_index");
240+
assertNotNull(element);
241+
assertTrue(element.getText().contains("Route"));
242+
}
243+
230244
@NotNull
231245
private XmlFile createXmlFile(@NotNull String content) {
232246
return (XmlFile) PsiFileFactory.getInstance(getProject()).createFileFromText("DUMMY__." + XmlFileType.INSTANCE.getDefaultExtension(), XmlFileType.INSTANCE, content);

tests/fr/adrienbrault/idea/symfony2plugin/tests/routing/fixtures/RouteHelper.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,12 @@ class CarController
1515
public function indexAction()
1616
{
1717
}
18+
19+
/**
20+
* @Route("/edit/{id}", name="my_car_foo_stuff")
21+
*/
22+
public function fooAction()
23+
{
24+
}
1825
}
1926
}

0 commit comments

Comments
 (0)
0