added inspection and quickfix for incorrect delimiter

GitOrigin-RevId: b249d0ee825f1f5e832655bb0e68305c9512f25e
This commit is contained in:
marcelhillesheim
2021-10-16 02:38:01 +02:00
committed by intellij-monorepo-bot
parent ea1946cbea
commit 0514fad120
6 changed files with 127 additions and 1 deletions

View File

@@ -0,0 +1,80 @@
package ru.adelf.idea.dotenv.inspections;
import com.intellij.codeInspection.*;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import ru.adelf.idea.dotenv.DotEnvFactory;
import ru.adelf.idea.dotenv.psi.DotEnvFile;
import ru.adelf.idea.dotenv.psi.DotEnvTypes;
import ru.adelf.idea.dotenv.psi.impl.DotEnvKeyImpl;
public class IncorrectDelimiterInspection 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 "Incorrect delimiter";
}
@Override
public boolean runForWholeFile() {
return true;
}
@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, DotEnvKeyImpl.class).forEach(key -> {
if (key.getText().contains("-")) {
problemsHolder.registerProblem(key, "Expected: '_' Found: '-'", new ReplaceDelimiterQuickFix());
}
});
return problemsHolder;
}
private static class ReplaceDelimiterQuickFix implements LocalQuickFix {
@NotNull
@Override
public String getName() {
return "Replace delimiter";
}
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
try {
PsiElement psiElement = descriptor.getPsiElement();
PsiElement newPsiElement = DotEnvFactory.createFromText(project, DotEnvTypes.KEY,
psiElement.getText().replace("-","_")+"=dummy");
psiElement.replace(newPsiElement);
} catch (IncorrectOperationException e) {
Logger.getInstance(IncorrectDelimiterInspection.class).error(e);
}
}
@NotNull
public String getFamilyName() {
return getName();
}
}
}

View File

@@ -110,6 +110,14 @@
level="WARNING"
implementationClass="ru.adelf.idea.dotenv.inspections.ExtraBlankLineInspection"/>
<localInspection language="DotEnv"
groupName="DotEnv"
shortName="DotEnvIncorrectDelimiterInspection"
displayName="Incorrect delimiter"
enabledByDefault="true"
level="WARNING"
implementationClass="ru.adelf.idea.dotenv.inspections.IncorrectDelimiterInspection"/>
<lang.syntaxHighlighterFactory language="DotEnv"
implementationClass="ru.adelf.idea.dotenv.DotEnvSyntaxHighlighterFactory"/>

View File

@@ -0,0 +1,5 @@
<html>
<body>
Reports incorrect delimiter in .env files ('-' instead of'_').
</body>
</html>

View File

@@ -1,9 +1,11 @@
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 ru.adelf.idea.dotenv.tests.DotEnvLightCodeInsightFixtureTestCase;
@@ -24,6 +26,8 @@ public class InspectionsTest extends DotEnvLightCodeInsightFixtureTestCase {
return basePath + "dotenv/fixtures";
}
// Test for each Inspection
public void testDuplicateKey() {
doInspectionTest(new DuplicateKeyInspection(), Arrays.asList("DUPLICATE_KEY=test", "DUPLICATE_KEY=test2"));
}
@@ -36,6 +40,22 @@ public class InspectionsTest extends DotEnvLightCodeInsightFixtureTestCase {
doInspectionTest(new ExtraBlankLineInspection(), Collections.singletonList("\n\n\n"));
}
public void testIncorrectDelimiterInspection() {
doInspectionTest(new IncorrectDelimiterInspection(), Collections.singletonList("INCORRECT-DELIMITER"));
}
// Every available quickfix from every inspection is getting applied
public void testQuickFixes() {
myFixture.enableInspections(new SpaceInsideNonQuotedInspection());
myFixture.enableInspections(new ExtraBlankLineInspection());
myFixture.enableInspections(new IncorrectDelimiterInspection());
myFixture.doHighlighting();
List<IntentionAction> intentionActions = myFixture.getAllQuickFixes();
intentionActions.forEach(intentionAction -> myFixture.launchAction(intentionAction));
myFixture.checkResultByFile("quickFix.env");
}
private void doInspectionTest(InspectionProfileEntry entry, List<String> expectedHighlightedText) {
myFixture.enableInspections(entry);

View File

@@ -6,4 +6,6 @@ SPACE_INSIDE_NON_QUOTED=spaces without quotes
# extra blank lines test START
# extra blank lines test END
# extra blank lines test END
INCORRECT-DELIMITER=test-test

View File

@@ -0,0 +1,11 @@
DUPLICATE_KEY=test
DUPLICATE_KEY=test2
SPACE_INSIDE_NON_QUOTED="spaces without quotes"
# extra blank lines test START
# extra blank lines test END
INCORRECT_DELIMITER=test-test