-
-
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.
provide Symfony ux "anonymous_template_directory" index
- Loading branch information
Showing
4 changed files
with
114 additions
and
40 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]> | ||
|
@@ -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@")) { | ||
|
@@ -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); | ||
} | ||
} | ||
} | ||
}; | ||
|
@@ -138,7 +141,7 @@ private static void visitKey(@NotNull YAMLKeyValue yamlKeyValue, @NotNull TreeMa | |
|
||
@Override | ||
public int getVersion() { | ||
return 1; | ||
return 2; | ||
} | ||
|
||
@Override | ||
|
40 changes: 40 additions & 0 deletions
40
...est/java/fr/adrienbrault/idea/symfony2plugin/tests/stubs/indexes/ConfigStubIndexTest.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,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 <[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 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") | ||
); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...java/fr/adrienbrault/idea/symfony2plugin/tests/stubs/indexes/fixtures/twig_component.yaml
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 @@ | ||
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/' |