-
-
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 #2259 from Haehnchen/feature/index-twig-component-ns
add Twig component namespace config index
- Loading branch information
Showing
6 changed files
with
291 additions
and
5 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
src/main/java/fr/adrienbrault/idea/symfony2plugin/stubs/dict/ConfigIndex.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,49 @@ | ||
package fr.adrienbrault.idea.symfony2plugin.stubs.dict; | ||
|
||
import org.apache.commons.lang3.builder.HashCodeBuilder; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.io.Serializable; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.TreeMap; | ||
|
||
/** | ||
* @author Daniel Espendiller <[email protected]> | ||
*/ | ||
public class ConfigIndex implements Serializable { | ||
@NotNull | ||
private final String name; | ||
|
||
@NotNull | ||
private final TreeMap<String, TreeMap<String, String>> configs; | ||
|
||
public ConfigIndex(@NotNull String name, @NotNull TreeMap<String, TreeMap<String, String>> configs) { | ||
this.name = name; | ||
this.configs = configs; | ||
} | ||
|
||
@NotNull | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
@NotNull | ||
public Map<String, TreeMap<String, String>> getConfigs() { | ||
return configs; | ||
} | ||
|
||
public int hashCode() { | ||
return new HashCodeBuilder() | ||
.append(this.name) | ||
.append(this.configs.hashCode()) | ||
.toHashCode(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
return obj instanceof ConfigIndex && | ||
Objects.equals(((ConfigIndex) obj).name, this.name) && | ||
Objects.equals(((ConfigIndex) obj).configs, this.configs); | ||
} | ||
} |
173 changes: 173 additions & 0 deletions
173
src/main/java/fr/adrienbrault/idea/symfony2plugin/stubs/indexes/ConfigStubIndex.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,173 @@ | ||
package fr.adrienbrault.idea.symfony2plugin.stubs.indexes; | ||
|
||
import com.intellij.openapi.vfs.VfsUtil; | ||
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 fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent; | ||
import fr.adrienbrault.idea.symfony2plugin.stubs.dict.ConfigIndex; | ||
import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.externalizer.ObjectStreamDataExternalizer; | ||
import fr.adrienbrault.idea.symfony2plugin.util.ProjectUtil; | ||
import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils; | ||
import fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.yaml.YAMLFileType; | ||
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; | ||
|
||
/** | ||
* @author Daniel Espendiller <[email protected]> | ||
*/ | ||
public class ConfigStubIndex extends FileBasedIndexExtension<String, ConfigIndex> { | ||
public static final ID<String, ConfigIndex> KEY = ID.create("fr.adrienbrault.idea.symfony2plugin.config_stub_index"); | ||
private final KeyDescriptor<String> myKeyDescriptor = new EnumeratorStringDescriptor(); | ||
private static final int MAX_FILE_BYTE_SIZE = 2097152; | ||
private static final ObjectStreamDataExternalizer<ConfigIndex> EXTERNALIZER = new ObjectStreamDataExternalizer<>(); | ||
|
||
@NotNull | ||
@Override | ||
public ID<String, ConfigIndex> getName() { | ||
return KEY; | ||
} | ||
|
||
@Override | ||
public @NotNull DataIndexer<String, ConfigIndex, FileContent> getIndexer() { | ||
return new DataIndexer<>() { | ||
@Override | ||
public @NotNull Map<String, ConfigIndex> map(@NotNull FileContent inputData) { | ||
if (!(inputData.getPsiFile() instanceof YAMLFile yamlFile) || | ||
!Symfony2ProjectComponent.isEnabledForIndex(yamlFile.getProject()) || | ||
!isValidForIndex(inputData, yamlFile) | ||
) { | ||
return Collections.emptyMap(); | ||
} | ||
|
||
Map<String, ConfigIndex> map = new HashMap<>(); | ||
|
||
TreeMap<String, TreeMap<String, String>> configs = new TreeMap<>(); | ||
|
||
for (YAMLKeyValue yamlKeyValue : YamlHelper.getTopLevelKeyValues(yamlFile)) { | ||
|
||
String keyText = yamlKeyValue.getKeyText(); | ||
if ("twig_component".equals(keyText)) { | ||
visitKey(yamlKeyValue, configs); | ||
} | ||
|
||
if (keyText.startsWith("when@")) { | ||
YAMLValue value = yamlKeyValue.getValue(); | ||
if (value instanceof YAMLMapping) { | ||
for (YAMLKeyValue yamlKeyValue2 : ((YAMLMapping) value).getKeyValues()) { | ||
String keyText2 = yamlKeyValue2.getKeyText(); | ||
if ("twig_component".equals(keyText2)) { | ||
visitKey(yamlKeyValue2, configs); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (!configs.isEmpty()) { | ||
map.put("twig_component_defaults", new ConfigIndex("twig_component_defaults", configs)); | ||
} | ||
|
||
return map; | ||
} | ||
|
||
private static void visitKey(@NotNull YAMLKeyValue yamlKeyValue, @NotNull TreeMap<String, TreeMap<String, String>> configs) { | ||
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)) { | ||
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; | ||
} | ||
|
||
items.put("template_directory", templateDirectory); | ||
|
||
String namePrefix = YamlHelper.getYamlKeyValueAsString(yamlMapping2, "name_prefix"); | ||
if (namePrefix != null) { | ||
items.put("name_prefix", namePrefix); | ||
} | ||
|
||
configs.put(keyText1, items); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public @NotNull KeyDescriptor<String> getKeyDescriptor() { | ||
return this.myKeyDescriptor; | ||
} | ||
|
||
@Override | ||
public @NotNull DataExternalizer<ConfigIndex> getValueExternalizer() { | ||
return EXTERNALIZER; | ||
} | ||
|
||
@Override | ||
public int getVersion() { | ||
return 1; | ||
} | ||
|
||
@Override | ||
public FileBasedIndex.@NotNull InputFilter getInputFilter() { | ||
return virtualFile -> virtualFile.getFileType() == YAMLFileType.YML; | ||
} | ||
|
||
|
||
@Override | ||
public boolean dependsOnFileContent() { | ||
return true; | ||
} | ||
|
||
private static boolean isValidForIndex(FileContent inputData, PsiFile psiFile) { | ||
String fileName = psiFile.getName(); | ||
if(fileName.startsWith(".") || fileName.endsWith("Test")) { | ||
return false; | ||
} | ||
|
||
// is Test file in path name | ||
String relativePath = VfsUtil.getRelativePath(inputData.getFile(), ProjectUtil.getProjectDir(inputData.getProject()), '/'); | ||
if(relativePath != null && (relativePath.contains("/Test/") || relativePath.contains("/Tests/") || relativePath.contains("/Fixture/") || relativePath.contains("/Fixtures/"))) { | ||
return false; | ||
} | ||
|
||
if(inputData.getFile().getLength() > MAX_FILE_BYTE_SIZE) { | ||
return false; | ||
} | ||
|
||
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
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
21 changes: 21 additions & 0 deletions
21
src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/util/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/' |