Skip to content

Commit

Permalink
provide Symfony ux "anonymous_template_directory" index
Browse files Browse the repository at this point in the history
  • Loading branch information
Haehnchen committed Dec 12, 2023
1 parent 2a77764 commit 0054084
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.Serializable;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;

/**
Expand All @@ -18,9 +19,13 @@ public class ConfigIndex implements Serializable {
@NotNull
private final TreeMap<String, TreeMap<String, String>> configs;

public ConfigIndex(@NotNull String name, @NotNull TreeMap<String, TreeMap<String, String>> configs) {
@NotNull
private final Set<String> values;

public ConfigIndex(@NotNull String name, @NotNull TreeMap<String, TreeMap<String, String>> configs, @NotNull Set<String> values) {
this.name = name;
this.configs = configs;
this.values = values;
}

@NotNull
Expand All @@ -33,6 +38,11 @@ public Map<String, TreeMap<String, String>> getConfigs() {
return configs;
}

@NotNull
public Set<String> getValues() {
return values;
}

public int hashCode() {
return new HashCodeBuilder()
.append(this.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
Expand Down Expand Up @@ -51,14 +48,13 @@ public ID<String, ConfigIndex> getName() {
}

Map<String, ConfigIndex> map = new HashMap<>();

TreeMap<String, TreeMap<String, String>> configs = new TreeMap<>();
Set<String> 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@")) {
Expand All @@ -67,60 +63,67 @@ public ID<String, ConfigIndex> getName() {
for (YAMLKeyValue yamlKeyValue2 : ((YAMLMapping) value).getKeyValues()) {
String keyText2 = yamlKeyValue2.getKeyText();
if ("twig_component".equals(keyText2)) {
visitKey(yamlKeyValue2, configs);
visitKey(yamlKeyValue2, configs, anonymousTemplateDirectory);
}
}
}
}
}

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<String, TreeMap<String, String>> configs) {
private static void visitKey(@NotNull YAMLKeyValue yamlKeyValue, @NotNull TreeMap<String, TreeMap<String, String>> configs, @NotNull Set<String> 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<String, String> items = new TreeMap<>();
items.put("template_directory", s);
configs.put(keyText1, items);
}
} else if (value2 instanceof YAMLMapping yamlMapping2) {
TreeMap<String, String> items = new TreeMap<>();
items.put("template_directory", s);
configs.put(keyText1, items);
}
} else if (value2 instanceof YAMLMapping yamlMapping2) {
TreeMap<String, String> 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);
}
}
}
};
Expand All @@ -138,7 +141,7 @@ private static void visitKey(@NotNull YAMLKeyValue yamlKeyValue, @NotNull TreeMa

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

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package fr.adrienbrault.idea.symfony2plugin.tests.stubs.indexes;

import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.ConfigStubIndex;
import fr.adrienbrault.idea.symfony2plugin.tests.SymfonyLightCodeInsightFixtureTestCase;

/**
* @author Daniel Espendiller <[email protected]>
*/
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 testIndexingForConfigValues() {
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, "twig_component_defaults");

assertIndexContainsKeyWithValue(
ConfigStubIndex.KEY,
"twig_component_defaults",
value -> value.getConfigs().get("App\\Twig\\Components2\\").get("template_directory").contains("components")
);
}
}
Original file line number Diff line number Diff line change
@@ -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/'

0 comments on commit 0054084

Please sign in to comment.