Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package fr.adrienbrault.idea.symfony2plugin.config.yaml.inspection;

import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiFile;
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.yaml.psi.YAMLKeyValue;

/**
* @author Daniel Espendiller <daniel@espendiller.net>
Expand All @@ -14,16 +16,19 @@ public class YamlDuplicateParameterKeyInspection extends YamlDuplicateServiceKey
@NotNull
@Override
public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {

PsiFile psiFile = holder.getFile();
if(!Symfony2ProjectComponent.isEnabled(psiFile.getProject())) {
return super.buildVisitor(holder, isOnTheFly);
}

return new PsiElementVisitor() {
@Override
public void visitFile(PsiFile file) {
visitRoot(file, "parameters", holder);
public void visitElement(@NotNull PsiElement element) {
if (element instanceof YAMLKeyValue yamlKeyValue) {
visitRoot(yamlKeyValue, "parameters", holder);
}

super.visitElement(element);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package fr.adrienbrault.idea.symfony2plugin.config.yaml.inspection;

import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
import fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.yaml.YAMLUtil;
import org.jetbrains.yaml.psi.YAMLCompoundValue;
import org.jetbrains.yaml.psi.YAMLFile;
import org.jetbrains.yaml.psi.YAMLKeyValue;
import org.jetbrains.yaml.psi.YAMLMapping;

Expand All @@ -30,25 +27,41 @@ public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, bool

return new PsiElementVisitor() {
@Override
public void visitFile(PsiFile file) {
visitRoot(file, "services", holder);
public void visitElement(@NotNull PsiElement element) {
if (element instanceof YAMLKeyValue yamlKeyValue) {
visitRoot(yamlKeyValue, "services", holder);
}

super.visitElement(element);
}
};
}

protected void visitRoot(PsiFile psiFile, String rootName, @NotNull ProblemsHolder holder) {
if(!(psiFile instanceof YAMLFile)) {
return;
}
protected void visitRoot(@NotNull YAMLKeyValue yamlKeyValue, String rootName, @NotNull ProblemsHolder holder) {
if (yamlKeyValue.getParent() instanceof YAMLMapping yamlMapping) {
String keyText1 = yamlKeyValue.getKeyText();
if (!keyText1.startsWith("_")) {
if (yamlMapping.getParent() instanceof YAMLKeyValue yamlKeyValue1) {
if (rootName.equals(yamlKeyValue1.getKeyText())) {
int found = 0;
for (YAMLKeyValue keyValue : yamlMapping.getKeyValues()) {
String keyText = keyValue.getKeyText();

YAMLKeyValue yamlKeyValue = YAMLUtil.getQualifiedKeyInFile((YAMLFile) psiFile, rootName);
if(yamlKeyValue == null) {
return;
}
if (keyText1.equals(keyText)) {
found++;
}

YAMLCompoundValue yaml = PsiTreeUtil.findChildOfType(yamlKeyValue, YAMLMapping.class);
if(yaml != null) {
YamlHelper.attachDuplicateKeyInspection(yaml, holder);
if (found == 2) {
final PsiElement keyElement = yamlKeyValue.getKey();
assert keyElement != null;
holder.registerProblem(keyElement, "Symfony: Duplicate key", ProblemHighlightType.GENERIC_ERROR_OR_WARNING);

break;
}
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package fr.adrienbrault.idea.symfony2plugin.tests.config.yaml.inspection;

import fr.adrienbrault.idea.symfony2plugin.tests.SymfonyLightCodeInsightFixtureTestCase;

/**
* @author Daniel Espendiller <daniel@espendiller.net>
*
* @see fr.adrienbrault.idea.symfony2plugin.config.yaml.inspection.YamlDuplicateServiceKeyInspection
*/
public class YamlDuplicateParameterKeyInspectionTest extends SymfonyLightCodeInsightFixtureTestCase {

public void testDuplicateServiceKeyProvidesWarning() {
assertLocalInspectionContains("services.yml", "" +
"parameters:\n" +
" fo<caret>o: \n" +
" foo: ~\n",
"Symfony: Duplicate key"
);

assertLocalInspectionContains("services.yml", "" +
"parameters:\n" +
" foo: \n" +
" f<caret>oo: ~\n",
"Symfony: Duplicate key"
);

assertLocalInspectionNotContains("services.yml", "" +
"parameters:\n" +
" foo1: ~" +
" f<caret>oo: ~\n",
"Symfony: Duplicate key"
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void testDuplicateServiceKeyProvidesWarning() {
" car: car\n" +
" foo: \n" +
" car: car \n",
"Duplicate key"
"Symfony: Duplicate key"
);

assertLocalInspectionContains("routing.yml", "" +
Expand All @@ -25,7 +25,16 @@ public void testDuplicateServiceKeyProvidesWarning() {
" car: car \n" +
" f<caret>oo: \n" +
" car: car",
"Duplicate key"
"Symfony: Duplicate key"
);

assertLocalInspectionNotContains("routing.yml", "" +
"services:\n" +
" foo1: \n" +
" car: car \n" +
" f<caret>oo: \n" +
" car: car",
"Symfony: Duplicate key"
);
}

Expand Down