diff --git a/plugins/env-files-support/src/main/java/ru/adelf/idea/dotenv/inspections/LeadingCharacterInspection.java b/plugins/env-files-support/src/main/java/ru/adelf/idea/dotenv/inspections/LeadingCharacterInspection.java new file mode 100644 index 000000000000..0d010981b697 --- /dev/null +++ b/plugins/env-files-support/src/main/java/ru/adelf/idea/dotenv/inspections/LeadingCharacterInspection.java @@ -0,0 +1,49 @@ +package ru.adelf.idea.dotenv.inspections; + +import com.intellij.codeInspection.InspectionManager; +import com.intellij.codeInspection.LocalInspectionTool; +import com.intellij.codeInspection.ProblemDescriptor; +import com.intellij.codeInspection.ProblemsHolder; +import com.intellij.psi.PsiFile; +import com.intellij.psi.util.PsiTreeUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import ru.adelf.idea.dotenv.psi.DotEnvFile; +import ru.adelf.idea.dotenv.psi.DotEnvKey; + +public class LeadingCharacterInspection extends LocalInspectionTool { + // Change the display name within the plugin.xml + // This needs to be here as otherwise the tests will throw errors. + @NotNull + @Override + public String getDisplayName() { + return "Invalid leading character"; + } + + @Nullable + @Override + public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) { + if (!(file instanceof DotEnvFile)) { + return null; + } + + return analyzeFile(file, manager, isOnTheFly).getResultsArray(); + } + + @NotNull + private ProblemsHolder analyzeFile(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) { + ProblemsHolder problemsHolder = new ProblemsHolder(manager, file, isOnTheFly); + + PsiTreeUtil.findChildrenOfType(file, DotEnvKey.class).forEach(dotEnvKey -> { + // Also accepts lower case chars as keys with lower case chars are handled by another inspection + // same for dash (-> IncorrectDelimiter + if (!dotEnvKey.getText().matches("[A-Za-z_-].*")){ + problemsHolder.registerProblem(dotEnvKey, + "Invalid first char for a key. Only A-Z and '_' are allowed."); + } + }); + + return problemsHolder; + } + +} diff --git a/plugins/env-files-support/src/main/resources/META-INF/plugin.xml b/plugins/env-files-support/src/main/resources/META-INF/plugin.xml index cc455e50a3ac..e718839d3aa3 100644 --- a/plugins/env-files-support/src/main/resources/META-INF/plugin.xml +++ b/plugins/env-files-support/src/main/resources/META-INF/plugin.xml @@ -118,6 +118,14 @@ level="WARNING" implementationClass="ru.adelf.idea.dotenv.inspections.IncorrectDelimiterInspection"/> + + diff --git a/plugins/env-files-support/src/main/resources/inspectionDescriptions/DotEnvLeadingCharacterInspection.html b/plugins/env-files-support/src/main/resources/inspectionDescriptions/DotEnvLeadingCharacterInspection.html new file mode 100644 index 000000000000..caf6447197f5 --- /dev/null +++ b/plugins/env-files-support/src/main/resources/inspectionDescriptions/DotEnvLeadingCharacterInspection.html @@ -0,0 +1,5 @@ + + +Reports keys with invalid leading character in .env files. Only A-Z and '_' are allowed as first char of a key. + + \ No newline at end of file diff --git a/plugins/env-files-support/src/test/java/ru/adelf/idea/dotenv/tests/dotenv/InspectionsTest.java b/plugins/env-files-support/src/test/java/ru/adelf/idea/dotenv/tests/dotenv/InspectionsTest.java index 8d55be221140..f13880f2e284 100644 --- a/plugins/env-files-support/src/test/java/ru/adelf/idea/dotenv/tests/dotenv/InspectionsTest.java +++ b/plugins/env-files-support/src/test/java/ru/adelf/idea/dotenv/tests/dotenv/InspectionsTest.java @@ -3,10 +3,10 @@ package ru.adelf.idea.dotenv.tests.dotenv; import com.intellij.codeInsight.daemon.impl.HighlightInfo; import com.intellij.codeInsight.intention.IntentionAction; import com.intellij.codeInspection.InspectionProfileEntry; -import ru.adelf.idea.dotenv.inspections.DuplicateKeyInspection; -import ru.adelf.idea.dotenv.inspections.ExtraBlankLineInspection; -import ru.adelf.idea.dotenv.inspections.IncorrectDelimiterInspection; -import ru.adelf.idea.dotenv.inspections.SpaceInsideNonQuotedInspection; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import ru.adelf.idea.dotenv.inspections.*; import ru.adelf.idea.dotenv.tests.DotEnvLightCodeInsightFixtureTestCase; import java.util.ArrayList; @@ -14,6 +14,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +@RunWith(JUnit4.class) public class InspectionsTest extends DotEnvLightCodeInsightFixtureTestCase { @Override @@ -28,23 +29,33 @@ public class InspectionsTest extends DotEnvLightCodeInsightFixtureTestCase { // Test for each Inspection + @Test public void testDuplicateKey() { doInspectionTest(new DuplicateKeyInspection(), Arrays.asList("DUPLICATE_KEY=test", "DUPLICATE_KEY=test2")); } + @Test public void testSpaceInsideNonQuoted() { doInspectionTest(new SpaceInsideNonQuotedInspection(), Collections.singletonList("spaces without quotes")); } + @Test public void testExtraBlankLine() { doInspectionTest(new ExtraBlankLineInspection(), Collections.singletonList("\n\n\n")); } + @Test public void testIncorrectDelimiterInspection() { doInspectionTest(new IncorrectDelimiterInspection(), Collections.singletonList("INCORRECT-DELIMITER")); } + @Test + public void testLeadingCharacterInspection() { + doInspectionTest(new LeadingCharacterInspection(), Collections.singletonList("*LEADING_CHARACTER")); + } + // Every available quickfix from every inspection is getting applied + @Test public void testQuickFixes() { myFixture.enableInspections(new SpaceInsideNonQuotedInspection()); myFixture.enableInspections(new ExtraBlankLineInspection()); diff --git a/plugins/env-files-support/src/test/java/ru/adelf/idea/dotenv/tests/dotenv/fixtures/inspections.env b/plugins/env-files-support/src/test/java/ru/adelf/idea/dotenv/tests/dotenv/fixtures/inspections.env index 46a4f60baff3..73bc8688847e 100644 --- a/plugins/env-files-support/src/test/java/ru/adelf/idea/dotenv/tests/dotenv/fixtures/inspections.env +++ b/plugins/env-files-support/src/test/java/ru/adelf/idea/dotenv/tests/dotenv/fixtures/inspections.env @@ -8,4 +8,6 @@ SPACE_INSIDE_NON_QUOTED=spaces without quotes # extra blank lines test END -INCORRECT-DELIMITER=test-test \ No newline at end of file +INCORRECT-DELIMITER=test-test + +*LEADING_CHARACTER=test \ No newline at end of file diff --git a/plugins/env-files-support/src/test/java/ru/adelf/idea/dotenv/tests/dotenv/fixtures/quickFix.env b/plugins/env-files-support/src/test/java/ru/adelf/idea/dotenv/tests/dotenv/fixtures/quickFix.env index d7b46e610019..a25044ee5f6a 100644 --- a/plugins/env-files-support/src/test/java/ru/adelf/idea/dotenv/tests/dotenv/fixtures/quickFix.env +++ b/plugins/env-files-support/src/test/java/ru/adelf/idea/dotenv/tests/dotenv/fixtures/quickFix.env @@ -7,4 +7,6 @@ SPACE_INSIDE_NON_QUOTED="spaces without quotes" # extra blank lines test END -INCORRECT_DELIMITER=test-test \ No newline at end of file +INCORRECT_DELIMITER=test-test + +*LEADING_CHARACTER=test \ No newline at end of file