Skip to content

Commit

Permalink
Add navigate to Twig include file references #889 and use lazy value …
Browse files Browse the repository at this point in the history
…provider for better performance #809
  • Loading branch information
Haehnchen committed Apr 16, 2017
1 parent 5eb983e commit f48dd88
Show file tree
Hide file tree
Showing 9 changed files with 269 additions and 231 deletions.
1 change: 0 additions & 1 deletion META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,6 @@
<fileBasedIndex implementation="fr.adrienbrault.idea.symfony2plugin.stubs.indexes.ContainerParameterStubIndex"/>
<fileBasedIndex implementation="fr.adrienbrault.idea.symfony2plugin.stubs.indexes.YamlTranslationStubIndex"/>
<fileBasedIndex implementation="fr.adrienbrault.idea.symfony2plugin.stubs.indexes.TwigIncludeStubIndex"/>
<fileBasedIndex implementation="fr.adrienbrault.idea.symfony2plugin.stubs.indexes.TwigMacroFromStubIndex"/>
<fileBasedIndex implementation="fr.adrienbrault.idea.symfony2plugin.stubs.indexes.TwigMacroFunctionStubIndex"/>
<fileBasedIndex implementation="fr.adrienbrault.idea.symfony2plugin.stubs.indexes.AnnotationRoutesStubIndex"/>
<fileBasedIndex implementation="fr.adrienbrault.idea.symfony2plugin.stubs.indexes.ServicesTagStubIndex"/>
Expand Down
15 changes: 11 additions & 4 deletions src/fr/adrienbrault/idea/symfony2plugin/TwigHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -936,14 +936,14 @@ public static ElementPattern<PsiElement> getTagTokenParserPattern() {
}

/**
* {% embed "vertical_boxes_skeleton.twig" %}
* {% FOOBAR "WANTED.html.twig" %}
*/
public static ElementPattern<PsiElement> getEmbedPattern() {
public static ElementPattern<PsiElement> getTagNameParameterPattern(@NotNull IElementType elementType, @NotNull String tagName) {
//noinspection unchecked
return PlatformPatterns
.psiElement(TwigTokenTypes.STRING_TEXT)
.withParent(
PlatformPatterns.psiElement(TwigElementTypes.EMBED_TAG)
PlatformPatterns.psiElement(elementType)
)
.afterLeafSkipping(
PlatformPatterns.or(
Expand All @@ -952,11 +952,18 @@ public static ElementPattern<PsiElement> getEmbedPattern() {
PlatformPatterns.psiElement(TwigTokenTypes.SINGLE_QUOTE),
PlatformPatterns.psiElement(TwigTokenTypes.DOUBLE_QUOTE)
),
PlatformPatterns.psiElement(TwigTokenTypes.TAG_NAME).withText("embed")
PlatformPatterns.psiElement(TwigTokenTypes.TAG_NAME).withText(tagName)
)
.withLanguage(TwigLanguage.INSTANCE);
}

/**
* {% embed "vertical_boxes_skeleton.twig" %}
*/
public static ElementPattern<PsiElement> getEmbedPattern() {
return getTagNameParameterPattern(TwigElementTypes.EMBED_TAG, "embed");
}

public static ElementPattern<PsiElement> getPrintBlockFunctionPattern() {
return PlatformPatterns.psiElement().withParent(PlatformPatterns.psiElement(TwigElementTypes.PRINT_BLOCK)).withLanguage(TwigLanguage.INSTANCE);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
package fr.adrienbrault.idea.symfony2plugin.stubs.indexes;

import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.indexing.*;
import com.intellij.util.io.DataExternalizer;
import com.intellij.util.io.EnumeratorStringDescriptor;
import com.intellij.util.io.KeyDescriptor;
import com.jetbrains.twig.TwigFile;
import com.jetbrains.twig.TwigFileType;
import com.jetbrains.twig.elements.TwigCompositeElement;
import com.jetbrains.twig.elements.TwigElementTypes;
import com.jetbrains.twig.elements.TwigTagWithFileReference;
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
import fr.adrienbrault.idea.symfony2plugin.TwigHelper;
import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils;
import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigUtil;
import gnu.trove.THashMap;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;

import java.util.Map;
Expand Down Expand Up @@ -50,38 +43,9 @@ public DataIndexer<String, Void, FileContent> getIndexer() {
return map;
}

PsiTreeUtil.collectElements(psiFile, psiElement -> {
// {% include %}
if(psiElement instanceof TwigTagWithFileReference && psiElement.getNode().getElementType() == TwigElementTypes.INCLUDE_TAG) {
for (String templateName : TwigHelper.getIncludeTagStrings((TwigTagWithFileReference) psiElement)) {
map.put(templateName, null);
}
}

if(psiElement instanceof TwigCompositeElement) {

// {{ include() }}
PsiElement includeTag = PsiElementUtils.getChildrenOfType(psiElement, TwigHelper.getPrintBlockFunctionPattern("include", "source"));
if(includeTag != null) {
String templateName = includeTag.getText();
if(StringUtils.isNotBlank(templateName)) {
map.put(templateName, null);
}
}

// {% embed "foo.html.twig"
PsiElement embedTag = PsiElementUtils.getChildrenOfType(psiElement, TwigHelper.getEmbedPattern());
if(embedTag != null) {
String templateName = embedTag.getText();
if(StringUtils.isNotBlank(templateName)) {
map.put(templateName, null);
}
}

}

return false;
});
TwigUtil.visitTemplateIncludes((TwigFile) psiFile, templateInclude ->
map.put(templateInclude.getTemplateName(), null)
);

return map;
};
Expand Down Expand Up @@ -113,7 +77,7 @@ public boolean dependsOnFileContent() {

@Override
public int getVersion() {
return 2;
return 3;
}

}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public static void forceReindex() {
ServicesTagStubIndex.KEY,
TwigExtendsStubIndex.KEY,
TwigIncludeStubIndex.KEY,
TwigMacroFromStubIndex.KEY,
TwigMacroFunctionStubIndex.KEY,
YamlTranslationStubIndex.KEY,
};
Expand Down
Loading

0 comments on commit f48dd88

Please sign in to comment.