Skip to content

Commit

Permalink
support profiler template rendering list in Symfony statusbar to allo…
Browse files Browse the repository at this point in the history
…w navigation to template entrypoint
  • Loading branch information
Haehnchen committed Aug 23, 2023
1 parent b8a5ae5 commit b2de061
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ public String getRoute() {

@Nullable
public String getTemplate() {
if (this.contents == null) {
return null;
}

// try to find template ordered loading list from "Rendered Templates", to find main template entrypoint
Matcher matcher = Pattern.compile("\"template_paths\"[\\w;:{]+\"([^\"]+)\"").matcher(this.contents);
while(matcher.find()){
int groupStart = matcher.start();
int i = this.contents.lastIndexOf(Character.toString('\0'), groupStart);
if (i > 0) {
String substring = this.contents.substring(i, groupStart);
if (substring.contains("Twig\\Profiler\\Profile")) {
String group = matcher.group(1);
if (!group.isBlank() && !group.startsWith("@WebProfiler")) {
return group;
}
}
}
}

return this.pregMatch(this.contents, "\"template.twig \\(([^\"]*\\.html\\.\\w{2,4})\\)\"");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,21 @@
public class SymfonyProfilerWidgetActions {

public static class TemplateAction extends AnAction {
private final String templateName;
private final Project project;

private String templateName;
private Project project;

public TemplateAction(Project project, @Nullable String text) {
super(TwigUtil.getFoldingTemplateNameOrCurrent(text), "Open Template", TwigIcons.TwigFileIcon);
this.templateName = text;
public TemplateAction(Project project, @NotNull String templateName) {
super(templateName, "Open Template", TwigIcons.TwigFileIcon);
this.templateName = templateName;
this.project = project;
}

@Override
public void actionPerformed(AnActionEvent e) {
public void actionPerformed(@NotNull AnActionEvent e) {
Collection<PsiFile> psiFiles = TwigUtil.getTemplatePsiElements(project, templateName);

// @TODO: multiple targets?
if(psiFiles.size() > 0) {
if(!psiFiles.isEmpty()) {
IdeHelper.navigateToPsiElement(psiFiles.iterator().next());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package fr.adrienbrault.idea.symfony2plugin.tests.profiler.collector;

import fr.adrienbrault.idea.symfony2plugin.profiler.collector.LocalDefaultDataCollector;
import fr.adrienbrault.idea.symfony2plugin.profiler.collector.LocalMailCollector;
import fr.adrienbrault.idea.symfony2plugin.profiler.dict.MailMessage;
import fr.adrienbrault.idea.symfony2plugin.profiler.utils.ProfilerUtil;
import junit.framework.TestCase;

import java.io.File;

public class LocalDefaultDataCollectorTest extends TestCase {
protected String getTestDataPath() {
return "src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/profiler/collector/fixtures";
}

public void testFoo() {
String data = ProfilerUtil.getContentForFile(new File(this.getTestDataPath() + "/template-d6bc80"));

LocalDefaultDataCollector localDefaultDataCollector = new LocalDefaultDataCollector(data);
String template = localDefaultDataCollector.getTemplate();

assertEquals("test.html.twig", template);
}
}
Binary file not shown.

0 comments on commit b2de061

Please sign in to comment.