Skip to content

Commit

Permalink
also optimize template filename resolving #321
Browse files Browse the repository at this point in the history
  • Loading branch information
Haehnchen committed Aug 16, 2014
1 parent a67a43b commit bb0b027
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 34 deletions.
105 changes: 74 additions & 31 deletions src/fr/adrienbrault/idea/symfony2plugin/TwigHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static Map<String, VirtualFile> getTemplateFilesByName(Project project) {
@Nullable
public static TwigNamespaceSetting findManagedTwigNamespace(Project project, TwigPath twigPath) {

ArrayList<TwigNamespaceSetting> twigNamespaces = (ArrayList<TwigNamespaceSetting>) Settings.getInstance(project).twigNamespaces;
List<TwigNamespaceSetting> twigNamespaces = Settings.getInstance(project).twigNamespaces;
if(twigNamespaces == null) {
return null;
}
Expand All @@ -116,29 +116,12 @@ public static TwigNamespaceSetting findManagedTwigNamespace(Project project, Twi
return null;
}

/**
* todo: migrate getTemplatePsiElements to this method. think of support twig and php templates
*/
public static PsiFile[] getTemplateFilesByName(Project project, String templateName) {

ArrayList<PsiFile> psiFiles = new ArrayList<PsiFile>();

for(PsiElement templateTarget: TwigHelper.getTemplatePsiElements(project, templateName)) {
if(templateTarget instanceof PsiFile) {
psiFiles.add((TwigFile) templateTarget);
}
}

return psiFiles.toArray(new PsiFile[psiFiles.size()]);
}

@Nullable
public static PsiFile getTemplateFileByName(Project project, String templateName) {

for(PsiElement templateTarget: TwigHelper.getTemplatePsiElements(project, templateName)) {
if(templateTarget instanceof PsiFile) {
return (PsiFile) templateTarget;
}
PsiFile[] templatePsiElements = TwigHelper.getTemplatePsiElements(project, templateName);
if(templatePsiElements.length > 0) {
return templatePsiElements[0];
}

return null;
Expand Down Expand Up @@ -173,23 +156,83 @@ public static String normalizeTemplateName(String templateName) {

}

public static PsiElement[] getTemplatePsiElements(Project project, String templateName) {
/**
* Find file in a twig path collection
*
* @param project current project
* @param templateName path known, should not be normalized
* @return target files
*/
public static PsiFile[] getTemplatePsiElements(Project project, String templateName) {


String normalizedTemplateName = normalizeTemplateName(templateName);

Map<String, VirtualFile> twigFiles = TwigHelper.getTemplateFilesByName(project);
if(!twigFiles.containsKey(normalizedTemplateName)) {
return new PsiElement[0];
}
Collection<PsiFile> psiFiles = new HashSet<PsiFile>();

VirtualFile virtualFile = twigFiles.get(normalizedTemplateName);
for (TwigPath twigPath : getTwigNamespaces(project)) {

if(!twigPath.isEnabled()) {
continue;
}

if(normalizedTemplateName.startsWith("@")) {
// @Namespace/base.html.twig
// @Namespace/folder/base.html.twig
if(normalizedTemplateName.length() > 1 && twigPath.getNamespaceType() != TwigPathIndex.NamespaceType.BUNDLE) {
int i = normalizedTemplateName.indexOf("/");
if(i > 0) {
String templateNs = normalizedTemplateName.substring(1, i);
if(twigPath.getNamespace().equals(templateNs)) {
addFileInsideTwigPath(project, normalizedTemplateName.substring(i + 1), psiFiles, twigPath);
}
}
}
} else if(normalizedTemplateName.startsWith(":")) {
// ::base.html.twig
// :Foo:base.html.twig
if(normalizedTemplateName.length() > 1 && twigPath.getNamespaceType() == TwigPathIndex.NamespaceType.ADD_PATH) {
String templatePath = StringUtils.strip(normalizedTemplateName.replace(":", "/"), "/");
addFileInsideTwigPath(project, templatePath, psiFiles, twigPath);
}
} else {
// FooBundle::base.html.twig
// FooBundle:Bar:base.html.twig
if(twigPath.getNamespaceType() == TwigPathIndex.NamespaceType.BUNDLE) {
int i = normalizedTemplateName.indexOf(":");
if(i > 0) {
String templateNs = normalizedTemplateName.substring(0, i);
if(twigPath.getNamespace().equals(templateNs)) {
String templatePath = StringUtils.strip(normalizedTemplateName.substring(i + 1).replace(":", "/").replace("//", "/"), "/");
addFileInsideTwigPath(project, templatePath, psiFiles, twigPath);
}

}
}

// form_div_layout.html.twig
if(twigPath.isGlobalNamespace() && twigPath.getNamespaceType() == TwigPathIndex.NamespaceType.ADD_PATH) {
String templatePath = StringUtils.strip(normalizedTemplateName.replace(":", "/"), "/");
addFileInsideTwigPath(project, templatePath, psiFiles, twigPath);
}

}

PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
if(psiFile != null) {
return new PsiElement[] {psiFile};
}

return new PsiElement[0];
return psiFiles.toArray(new PsiFile[psiFiles.size()]);

}

private static void addFileInsideTwigPath(Project project, String templatePath, Collection<PsiFile> psiFiles, TwigPath twigPath) {
String[] split = templatePath.split("/");
VirtualFile virtualFile = VfsUtil.findRelativeFile(twigPath.getDirectory(project), split);
if(virtualFile != null) {
PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
if(psiFile != null) {
psiFiles.add(psiFile);
}
}
}

public static List<TwigPath> getTwigNamespaces(Project project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ protected PsiElement[] parameterGoToDeclaration(PsiElement psiElement, String ps
}

protected List<PsiFile> templateGoto(PsiElement psiElement, String templateName) {
return Arrays.asList(TwigHelper.getTemplateFilesByName(psiElement.getProject(), templateName));
return Arrays.asList(TwigHelper.getTemplatePsiElements(psiElement.getProject(), templateName));
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void visitElement(PsiElement element) {
if(includeTag != null) {
String templateName = includeTag.getText();
if(StringUtils.isNotBlank(templateName)) {
for(PsiFile templateFile: TwigHelper.getTemplateFilesByName(element.getProject(), templateName)) {
for(PsiFile templateFile: TwigHelper.getTemplatePsiElements(element.getProject(), templateName)) {
if(templateFile.equals(psiFile)) {
collectIncludeContextVars((TwigTagWithFileReference) element, includeTag, variables);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public TemplateAction(Project project, @Nullable String text) {
@Override
public void actionPerformed(AnActionEvent e) {

List<PsiFile> psiFiles = Arrays.asList(TwigHelper.getTemplateFilesByName(project, templateName));
List<PsiFile> psiFiles = Arrays.asList(TwigHelper.getTemplatePsiElements(project, templateName));

// @TODO: multiple targets?
if(psiFiles.size() > 0) {
Expand Down

0 comments on commit bb0b027

Please sign in to comment.