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
@@ -1,30 +1,30 @@
package fr.adrienbrault.idea.symfony2plugin.templating.util;

import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiRecursiveElementWalkingVisitor;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.containers.ArrayListSet;
import com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocTag;
import com.jetbrains.php.lang.parser.PhpElementTypes;
import com.jetbrains.php.lang.psi.elements.*;
import de.espend.idea.php.annotation.util.AnnotationUtil;
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
import fr.adrienbrault.idea.symfony2plugin.config.SymfonyPhpReferenceContributor;
import fr.adrienbrault.idea.symfony2plugin.extension.PluginConfigurationExtension;
import fr.adrienbrault.idea.symfony2plugin.extension.PluginConfigurationExtensionParameter;
import fr.adrienbrault.idea.symfony2plugin.templating.variable.dict.PsiVariable;
import fr.adrienbrault.idea.symfony2plugin.util.AnnotationBackportUtil;
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils;
import kotlin.Pair;
import kotlin.Triple;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static fr.adrienbrault.idea.symfony2plugin.util.StringUtils.underscore;

/**
* @author Daniel Espendiller <daniel@espendiller.net>
Expand Down Expand Up @@ -380,12 +380,35 @@ private void visitPhpDocTag(@NotNull PhpDocTag phpDocTag) {
}

String annotationFqnName = AnnotationBackportUtil.getClassNameReference(phpDocTag, fileImports);
if(!"Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Template".equals(StringUtils.stripStart(annotationFqnName, "\\"))) {
if(!StringUtils.stripStart(TwigUtil.TEMPLATE_ANNOTATION_CLASS, "\\").equals(StringUtils.stripStart(annotationFqnName, "\\"))) {
return;
}

String template = AnnotationUtil.getPropertyValueOrDefault(phpDocTag, "template");
if(template != null && template.endsWith(".twig")) {
if (template == null) {
// see \Sensio\Bundle\FrameworkExtraBundle\Templating\TemplateGuesser
// App\Controller\MyNiceController::myAction => my_nice/my.html.twig
Method methodScope = AnnotationBackportUtil.getMethodScope(phpDocTag);
if(methodScope != null) {
PhpClass phpClass = methodScope.getContainingClass();
if (phpClass != null) {
// App\Controller\ "MyNice" Controller
Matcher matcher = Pattern.compile("Controller\\\\(.+)Controller$", Pattern.MULTILINE).matcher(StringUtils.stripStart(phpClass.getFQN(), "\\"));
if(matcher.find()){
String group = underscore(matcher.group(1).replace("\\", "/"));
String name = methodScope.getName();

// __invoke is using controller as template name
if (name.equals("__invoke")) {
addTemplateWithScope(group + ".html.twig", methodScope, null);
} else {
String action = name.endsWith("Action") ? name.substring(0, name.length() - "Action".length()) : name;
addTemplateWithScope(group + "/" + underscore(action) + ".html.twig", methodScope, null);
}
}
}
}
} else if(template.endsWith(".twig")) {
Method methodScope = AnnotationBackportUtil.getMethodScope(phpDocTag);
if(methodScope != null) {
addTemplateWithScope(template, methodScope, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,67 @@ public void testThatTemplatePropertyAnnotationIsIndexed() {
"foo-annotation-property.html.twig".equals(value.getTemplate()) && value.getScopes().contains("Foobar.foobar")
);
}

public void testEmptyTemplateAnnotationIndexUsingTemplateGuesser() {
myFixture.configureByText(PhpFileType.INSTANCE, "<?php\n" +
"namespace App\\Controller;" +
"" +
"use Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Template;" +
"class MyNiceFoobarController\n" +
"{" +
"/**\n" +
" *\n" +
" * @Template()\n" +
" */" +
"public function foobarWhatAction() {}" +
"}\n"
);

assertIndexContains(
PhpTwigTemplateUsageStubIndex.KEY,
"my_nice_foobar/foobar_what.html.twig"
);
}

public void testEmptyTemplateAnnotationIndexUsingInvokeTemplateGuesser() {
myFixture.configureByText(PhpFileType.INSTANCE, "<?php\n" +
"namespace App\\Controller;" +
"" +
"use Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Template;" +
"class MyNiceFoobarController\n" +
"{" +
"/**\n" +
" *\n" +
" * @Template()\n" +
" */" +
"public function __invoke() {}" +
"}\n"
);

assertIndexContains(
PhpTwigTemplateUsageStubIndex.KEY,
"my_nice_foobar.html.twig"
);
}

public void testEmptyTemplateAnnotationIndexWithDirectoryUseTemplateGuesser() {
myFixture.configureByText(PhpFileType.INSTANCE, "<?php\n" +
"namespace App\\Controller\\CarItem\\WithApple;" +
"" +
"use Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Template;" +
"class MyNiceFoobarController\n" +
"{" +
"/**\n" +
" *\n" +
" * @Template()\n" +
" */" +
"public function foobarWhatAction() {}" +
"}\n"
);

assertIndexContains(
PhpTwigTemplateUsageStubIndex.KEY,
"car_item/with_apple/my_nice_foobar/foobar_what.html.twig"
);
}
}