-
-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2240 from Haehnchen/feature/embed-index
add Twig block embed index
- Loading branch information
Showing
5 changed files
with
158 additions
and
1 deletion.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
src/main/java/fr/adrienbrault/idea/symfony2plugin/stubs/indexes/TwigBlockEmbedIndex.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package fr.adrienbrault.idea.symfony2plugin.stubs.indexes; | ||
|
||
import com.intellij.psi.PsiFile; | ||
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 fr.adrienbrault.idea.symfony2plugin.stubs.indexes.externalizer.StringSetDataExternalizer; | ||
import fr.adrienbrault.idea.symfony2plugin.templating.util.TwigUtil; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
/** | ||
* @author Daniel Espendiller <[email protected]> | ||
*/ | ||
public class TwigBlockEmbedIndex extends FileBasedIndexExtension<String, Set<String>> { | ||
|
||
public static final ID<String, Set<String>> KEY = ID.create("fr.adrienbrault.idea.symfony2plugin.twig_block_names_embed"); | ||
|
||
private final KeyDescriptor<String> KEY_DESCRIPTOR = new EnumeratorStringDescriptor(); | ||
private static final StringSetDataExternalizer DATA_EXTERNALIZER = new StringSetDataExternalizer(); | ||
|
||
@NotNull | ||
@Override | ||
public DataIndexer<String, Set<String>, FileContent> getIndexer() { | ||
return fileContent -> { | ||
Map<String, Set<String>> blocks = new HashMap<>(); | ||
|
||
PsiFile psiFile = fileContent.getPsiFile(); | ||
if (psiFile instanceof TwigFile twigFile) { | ||
TwigUtil.visitEmbedBlocks(twigFile, pair -> { | ||
String templateName = pair.getFirst(); | ||
|
||
blocks.putIfAbsent(templateName, new HashSet<>()); | ||
blocks.get(templateName).add(pair.getSecond()); | ||
}); | ||
} | ||
|
||
return blocks; | ||
}; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public ID<String, Set<String>> getName() { | ||
return KEY; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public KeyDescriptor<String> getKeyDescriptor() { | ||
return KEY_DESCRIPTOR; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public DataExternalizer<Set<String>> getValueExternalizer() { | ||
return DATA_EXTERNALIZER; | ||
} | ||
|
||
@Override | ||
public int getVersion() { | ||
return 2; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public FileBasedIndex.InputFilter getInputFilter() { | ||
return file -> file.getFileType() == TwigFileType.INSTANCE; | ||
} | ||
|
||
@Override | ||
public boolean dependsOnFileContent() { | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...java/fr/adrienbrault/idea/symfony2plugin/tests/stubs/indexes/TwigBlockEmbedIndexTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package fr.adrienbrault.idea.symfony2plugin.tests.stubs.indexes; | ||
|
||
import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.TwigBlockEmbedIndex; | ||
import fr.adrienbrault.idea.symfony2plugin.tests.SymfonyLightCodeInsightFixtureTestCase; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* @author Daniel Espendiller <[email protected]> | ||
* @see fr.adrienbrault.idea.symfony2plugin.stubs.indexes.TwigBlockEmbedIndex | ||
*/ | ||
public class TwigBlockEmbedIndexTest extends SymfonyLightCodeInsightFixtureTestCase { | ||
public void setUp() throws Exception { | ||
super.setUp(); | ||
|
||
myFixture.copyFileToProject("blocks_embed.html.twig"); | ||
} | ||
|
||
public String getTestDataPath() { | ||
return "src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/stubs/indexes/fixtures"; | ||
} | ||
|
||
public void testThatValuesAreInIndex() { | ||
assertIndexContainsKeyWithValue(TwigBlockEmbedIndex.KEY, "teasers/skeleton.html.twig", value -> value.containsAll(Arrays.asList("left_teaser", "right_teaser"))); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...a/fr/adrienbrault/idea/symfony2plugin/tests/stubs/indexes/fixtures/blocks_embed.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{% block foo %} | ||
{% block foo_inner 'foo' %} | ||
{% endblock %} | ||
|
||
{{ block('foobar_print') }} | ||
|
||
{% embed "teasers/skeleton.html.twig" %} | ||
{% block left_teaser %} | ||
{{ block('foobar_print_embed') }} | ||
{% endblock %} | ||
{% block right_teaser %} | ||
{% endblock %} | ||
{% endembed %} | ||
|
||
{% extends 'extends/foo.html.twig' %} | ||
{% use 'use/foo.html.twig' %} | ||
|
||
{% embed "teasers\skeleton.html.twig" %} | ||
{% extends 'embed_extends\foo.html.twig' %} | ||
{% use 'embed_use\foo.html.twig' %} | ||
{% endembed %} |