mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-03-22 06:50:54 +07:00
added inspection and quickfix for incorrect delimiter
GitOrigin-RevId: b249d0ee825f1f5e832655bb0e68305c9512f25e
This commit is contained in:
committed by
intellij-monorepo-bot
parent
ea1946cbea
commit
0514fad120
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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"/>
|
||||
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
Reports incorrect delimiter in .env files ('-' instead of'_').
|
||||
</body>
|
||||
</html>
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user