Skip to content

Commit

Permalink
Merge pull request #2362 from Haehnchen/feature/constant-inspection-l…
Browse files Browse the repository at this point in the history
…anguage

split ContainerConstantInspection into language implementations to reduce wall time calls
  • Loading branch information
Haehnchen committed Apr 27, 2024
2 parents 2d25b90 + f485517 commit d27a256
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,63 @@
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.lang.Language;
import com.intellij.lang.xml.XMLLanguage;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.xml.XmlText;
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
import fr.adrienbrault.idea.symfony2plugin.config.xml.XmlHelper;
import fr.adrienbrault.idea.symfony2plugin.dic.container.util.ServiceContainerUtil;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.yaml.YAMLLanguage;
import org.jetbrains.yaml.psi.YAMLScalar;

/**
* @author Daniel Espendiller <[email protected]>
*/
public class ContainerConstantInspection extends LocalInspectionTool {

public static final String MESSAGE = "Symfony: constant not found";

@NotNull
public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {
return new PsiElementVisitor() {
@Override
public void visitElement(@NotNull PsiElement element) {
Language language = element.getLanguage();
public static class MyYamlLocalInspectionTool extends LocalInspectionTool {
@Override
public @NotNull PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
Project project = holder.getProject();
if (!Symfony2ProjectComponent.isEnabled(project)) {
return super.buildVisitor(holder, isOnTheFly);
}

if (language == YAMLLanguage.INSTANCE) {
return new PsiElementVisitor() {
@Override
public void visitElement(@NotNull PsiElement element) {
if (element instanceof YAMLScalar yamlScalar) {
visitYamlElement(yamlScalar, holder);
}
} else if(language == XMLLanguage.INSTANCE) {
visitXmlElement(element, holder);

super.visitElement(element);
}
};
}
}

super.visitElement(element);
public static class MyXmlLocalInspectionTool extends LocalInspectionTool {
@Override
public @NotNull PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
Project project = holder.getProject();
if (!Symfony2ProjectComponent.isEnabled(project)) {
return super.buildVisitor(holder, isOnTheFly);
}
};

return new PsiElementVisitor() {
@Override
public void visitElement(@NotNull PsiElement element) {
visitXmlElement(element, holder);
super.visitElement(element);
}
};
}
}
private void visitYamlElement(@NotNull YAMLScalar psiElement, @NotNull ProblemsHolder holder) {

private static void visitYamlElement(@NotNull YAMLScalar psiElement, @NotNull ProblemsHolder holder) {
String textValue = psiElement.getTextValue();
if(textValue.startsWith("!php/const:")) {
String constantName = textValue.substring(11);
Expand All @@ -51,7 +69,7 @@ private void visitYamlElement(@NotNull YAMLScalar psiElement, @NotNull ProblemsH
}
}

private void visitXmlElement(@NotNull PsiElement psiElement, @NotNull ProblemsHolder holder) {
private static void visitXmlElement(@NotNull PsiElement psiElement, @NotNull ProblemsHolder holder) {
if(!XmlHelper.getArgumentValueWithTypePattern("constant").accepts(psiElement)) {
return;
}
Expand Down
15 changes: 12 additions & 3 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -434,10 +434,19 @@
language="PHP"
implementationClass="fr.adrienbrault.idea.symfony2plugin.codeInspection.form.FormTypeAsClassConstantInspection"/>

<localInspection groupPath="Symfony" shortName="ContainerConstant" displayName="Constant not found"
<localInspection groupPath="Symfony" shortName="ContainerConstantXml" displayName="Constant not found in XML definition"
groupName="Service"
enabledByDefault="true" level="WARNING"
implementationClass="fr.adrienbrault.idea.symfony2plugin.dic.inspection.ContainerConstantInspection"/>
enabledByDefault="true"
level="WARNING"
language="XML"
implementationClass="fr.adrienbrault.idea.symfony2plugin.dic.inspection.ContainerConstantInspection$MyXmlLocalInspectionTool"/>

<localInspection groupPath="Symfony" shortName="ContainerConstantYaml" displayName="Constant not found in yaml definition"
groupName="Service"
enabledByDefault="true"
level="WARNING"
language="yaml"
implementationClass="fr.adrienbrault.idea.symfony2plugin.dic.inspection.ContainerConstantInspection$MyYamlLocalInspectionTool"/>

<localInspection groupPath="Symfony" shortName="TwigTranslationDomain" displayName="Twig: Missing translation domain"
groupName="Translation"
Expand Down

0 comments on commit d27a256

Please sign in to comment.