From 21d2be13dce743eab0096660d8cf4d5a2225214f Mon Sep 17 00:00:00 2001 From: Daniel Espendiller Date: Tue, 12 Dec 2023 19:55:11 +0100 Subject: [PATCH] provide Symfony ux "anonymous_template_directory" index --- .../stubs/dict/ConfigIndex.java | 12 ++- .../stubs/indexes/ConfigStubIndex.java | 81 ++++++++++--------- .../stubs/indexes/ConfigStubIndexTest.java | 40 +++++++++ .../indexes/fixtures/twig_component.yaml | 21 +++++ 4 files changed, 114 insertions(+), 40 deletions(-) create mode 100644 src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/stubs/indexes/ConfigStubIndexTest.java create mode 100644 src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/stubs/indexes/fixtures/twig_component.yaml diff --git a/src/main/java/fr/adrienbrault/idea/symfony2plugin/stubs/dict/ConfigIndex.java b/src/main/java/fr/adrienbrault/idea/symfony2plugin/stubs/dict/ConfigIndex.java index 81185b345..5b723d9dd 100644 --- a/src/main/java/fr/adrienbrault/idea/symfony2plugin/stubs/dict/ConfigIndex.java +++ b/src/main/java/fr/adrienbrault/idea/symfony2plugin/stubs/dict/ConfigIndex.java @@ -6,6 +6,7 @@ import java.io.Serializable; import java.util.Map; import java.util.Objects; +import java.util.Set; import java.util.TreeMap; /** @@ -18,9 +19,13 @@ public class ConfigIndex implements Serializable { @NotNull private final TreeMap> configs; - public ConfigIndex(@NotNull String name, @NotNull TreeMap> configs) { + @NotNull + private final Set values; + + public ConfigIndex(@NotNull String name, @NotNull TreeMap> configs, @NotNull Set values) { this.name = name; this.configs = configs; + this.values = values; } @NotNull @@ -33,6 +38,11 @@ public Map> getConfigs() { return configs; } + @NotNull + public Set getValues() { + return values; + } + public int hashCode() { return new HashCodeBuilder() .append(this.name) diff --git a/src/main/java/fr/adrienbrault/idea/symfony2plugin/stubs/indexes/ConfigStubIndex.java b/src/main/java/fr/adrienbrault/idea/symfony2plugin/stubs/indexes/ConfigStubIndex.java index d3cc5ffe1..6ba4f501f 100644 --- a/src/main/java/fr/adrienbrault/idea/symfony2plugin/stubs/indexes/ConfigStubIndex.java +++ b/src/main/java/fr/adrienbrault/idea/symfony2plugin/stubs/indexes/ConfigStubIndex.java @@ -18,10 +18,7 @@ import org.jetbrains.yaml.psi.*; import org.jetbrains.yaml.psi.impl.YAMLPlainTextImpl; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.TreeMap; +import java.util.*; /** * @author Daniel Espendiller @@ -51,14 +48,13 @@ public ID getName() { } Map map = new HashMap<>(); - TreeMap> configs = new TreeMap<>(); + Set anonymousTemplateDirectory = new HashSet<>(); for (YAMLKeyValue yamlKeyValue : YamlHelper.getTopLevelKeyValues(yamlFile)) { - String keyText = yamlKeyValue.getKeyText(); if ("twig_component".equals(keyText)) { - visitKey(yamlKeyValue, configs); + visitKey(yamlKeyValue, configs, anonymousTemplateDirectory); } if (keyText.startsWith("when@")) { @@ -67,7 +63,7 @@ public ID getName() { for (YAMLKeyValue yamlKeyValue2 : ((YAMLMapping) value).getKeyValues()) { String keyText2 = yamlKeyValue2.getKeyText(); if ("twig_component".equals(keyText2)) { - visitKey(yamlKeyValue2, configs); + visitKey(yamlKeyValue2, configs, anonymousTemplateDirectory); } } } @@ -75,52 +71,59 @@ public ID getName() { } if (!configs.isEmpty()) { - map.put("twig_component_defaults", new ConfigIndex("twig_component_defaults", configs)); + map.put("twig_component_defaults", new ConfigIndex("twig_component_defaults", configs, Collections.emptySet())); + } + + if (!anonymousTemplateDirectory.isEmpty()) { + map.put("anonymous_template_directory", new ConfigIndex("anonymous_template_directory", new TreeMap<>(), anonymousTemplateDirectory)); } return map; } - private static void visitKey(@NotNull YAMLKeyValue yamlKeyValue, @NotNull TreeMap> configs) { + private static void visitKey(@NotNull YAMLKeyValue yamlKeyValue, @NotNull TreeMap> configs, @NotNull Set anonymousTemplateDirectory) { YAMLValue value = yamlKeyValue.getValue(); if (value instanceof YAMLMapping yamlMapping) { YAMLKeyValue defaults = YamlHelper.getYamlKeyValue(yamlMapping, "defaults"); - if (defaults == null) { - return; - } - - YAMLValue value1 = defaults.getValue(); - if (value1 instanceof YAMLMapping yamlMapping1) { - for (YAMLKeyValue keyValue : yamlMapping1.getKeyValues()) { - String keyText1 = keyValue.getKeyText(); - - YAMLValue value2 = keyValue.getValue(); - if (value2 instanceof YAMLQuotedText || value2 instanceof YAMLPlainTextImpl) { - String s = PsiElementUtils.trimQuote(value2.getText()); - if (!StringUtils.isBlank(s)) { + if (defaults != null) { + YAMLValue value1 = defaults.getValue(); + if (value1 instanceof YAMLMapping yamlMapping1) { + for (YAMLKeyValue keyValue : yamlMapping1.getKeyValues()) { + String keyText1 = keyValue.getKeyText(); + + YAMLValue value2 = keyValue.getValue(); + if (value2 instanceof YAMLQuotedText || value2 instanceof YAMLPlainTextImpl) { + String s = PsiElementUtils.trimQuote(value2.getText()); + if (!StringUtils.isBlank(s)) { + TreeMap items = new TreeMap<>(); + items.put("template_directory", s); + configs.put(keyText1, items); + } + } else if (value2 instanceof YAMLMapping yamlMapping2) { TreeMap items = new TreeMap<>(); - items.put("template_directory", s); - configs.put(keyText1, items); - } - } else if (value2 instanceof YAMLMapping yamlMapping2) { - TreeMap items = new TreeMap<>(); - String templateDirectory = YamlHelper.getYamlKeyValueAsString(yamlMapping2, "template_directory"); - if (templateDirectory == null) { - continue; - } + String templateDirectory = YamlHelper.getYamlKeyValueAsString(yamlMapping2, "template_directory"); + if (templateDirectory == null) { + continue; + } - items.put("template_directory", templateDirectory); + items.put("template_directory", templateDirectory); - String namePrefix = YamlHelper.getYamlKeyValueAsString(yamlMapping2, "name_prefix"); - if (namePrefix != null) { - items.put("name_prefix", namePrefix); - } + String namePrefix = YamlHelper.getYamlKeyValueAsString(yamlMapping2, "name_prefix"); + if (namePrefix != null) { + items.put("name_prefix", namePrefix); + } - configs.put(keyText1, items); + configs.put(keyText1, items); + } } } } + + String templateDirectory = YamlHelper.getYamlKeyValueAsString(yamlMapping, "anonymous_template_directory"); + if (templateDirectory != null) { + anonymousTemplateDirectory.add(templateDirectory); + } } } }; @@ -138,7 +141,7 @@ private static void visitKey(@NotNull YAMLKeyValue yamlKeyValue, @NotNull TreeMa @Override public int getVersion() { - return 1; + return 2; } @Override diff --git a/src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/stubs/indexes/ConfigStubIndexTest.java b/src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/stubs/indexes/ConfigStubIndexTest.java new file mode 100644 index 000000000..1b8219b16 --- /dev/null +++ b/src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/stubs/indexes/ConfigStubIndexTest.java @@ -0,0 +1,40 @@ +package fr.adrienbrault.idea.symfony2plugin.tests.stubs.indexes; + +import fr.adrienbrault.idea.symfony2plugin.stubs.dict.ConfigIndex; +import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.ConfigStubIndex; +import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.ContainerBuilderStubIndex; +import fr.adrienbrault.idea.symfony2plugin.tests.SymfonyLightCodeInsightFixtureTestCase; +import org.jetbrains.annotations.NotNull; + +/** + * @author Daniel Espendiller + */ +public class ConfigStubIndexTest extends SymfonyLightCodeInsightFixtureTestCase { + public void setUp() throws Exception { + super.setUp(); + + myFixture.copyFileToProject("twig_component.yaml"); + } + + public String getTestDataPath() { + return "src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/stubs/indexes/fixtures"; + } + + public void testThatMethodsAreFound() { + assertIndexContains(ConfigStubIndex.KEY, "anonymous_template_directory"); + + assertIndexContainsKeyWithValue( + ConfigStubIndex.KEY, + "anonymous_template_directory", + value -> "anonymous_template_directory".equals(value.getName()) && value.getValues().contains("components/") + ); + + assertIndexContains(ConfigStubIndex.KEY, "anonymous_template_directory"); + + assertIndexContainsKeyWithValue( + ConfigStubIndex.KEY, + "twig_component_defaults", + value -> value.getConfigs().get("App\\Twig\\Components2\\").get("template_directory").contains("components") + ); + } +} diff --git a/src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/stubs/indexes/fixtures/twig_component.yaml b/src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/stubs/indexes/fixtures/twig_component.yaml new file mode 100644 index 000000000..c47136362 --- /dev/null +++ b/src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/stubs/indexes/fixtures/twig_component.yaml @@ -0,0 +1,21 @@ +twig_component: + anonymous_template_directory: 'components/' + defaults: + # Namespace & directory for components + App\Twig\Components\: 'components/' + App\Twig\Foobar\: components + + + # long form + App\Twig\Components2\: + template_directory: components + # component names will have an extra "AppBar:" prefix + # App\Twig\Components2\Alert => AppBar:Alert + # App\Twig\Components2\Button\Primary => AppBar:Button:Primary + name_prefix: AppBar + +when@test: + twig_component: + defaults: + # Namespace & directory for components + App\Twig\WhenSwitch\: 'foobar/'