Added inspection and quickfix for keys with lowercase chars

GitOrigin-RevId: 5813c4fed8adb6fff41b25506bd1fef5bcda7727
This commit is contained in:
marcelhillesheim
2021-10-20 16:05:59 +02:00
committed by intellij-monorepo-bot
parent 490aa2c815
commit 41bb4a91c7
6 changed files with 101 additions and 2 deletions
@@ -0,0 +1,76 @@
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.*;
public class LowercaseKeyInspection 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 "Key uses lowercase chars";
}
@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 -> {
if (dotEnvKey.getText().matches(".*[a-z].*")) {
problemsHolder.registerProblem(dotEnvKey,
"Key uses lowercase chars. Only keys with uppercase chars are allowed.",
new ForceUppercaseQuickFix()
);
}
});
return problemsHolder;
}
private static class ForceUppercaseQuickFix implements LocalQuickFix {
@NotNull
@Override
public String getName() {
return "Change to uppercase";
}
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
try {
PsiElement psiElement = descriptor.getPsiElement();
PsiElement newPsiElement = DotEnvFactory.createFromText(project, DotEnvTypes.KEY,
psiElement.getText().toUpperCase() +"=dummy");
psiElement.replace(newPsiElement);
} catch (IncorrectOperationException e) {
Logger.getInstance(IncorrectDelimiterInspection.class).error(e);
}
}
@NotNull
public String getFamilyName() {
return getName();
}
}
}
@@ -126,6 +126,14 @@
level="WARNING"
implementationClass="ru.adelf.idea.dotenv.inspections.LeadingCharacterInspection"/>
<localInspection language="DotEnv"
groupName="DotEnv"
shortName="DotEnvLowercaseKeyInspection"
displayName="Key uses lowercase chars"
enabledByDefault="true"
level="WARNING"
implementationClass="ru.adelf.idea.dotenv.inspections.LowercaseKeyInspection"/>
<lang.syntaxHighlighterFactory language="DotEnv"
implementationClass="ru.adelf.idea.dotenv.DotEnvSyntaxHighlighterFactory"/>
@@ -0,0 +1,5 @@
<html>
<body>
Reports keys, which use lowercase chars. Keys should be written in uppercase.
</body>
</html>
@@ -54,12 +54,18 @@ public class InspectionsTest extends DotEnvLightCodeInsightFixtureTestCase {
doInspectionTest(new LeadingCharacterInspection(), Collections.singletonList("*LEADING_CHARACTER"));
}
@Test
public void testLowercaseKeyInspection() {
doInspectionTest(new LowercaseKeyInspection(), Collections.singletonList("lower_case_KEY"));
}
// Every available quickfix from every inspection is getting applied
@Test
public void testQuickFixes() {
myFixture.enableInspections(new SpaceInsideNonQuotedInspection());
myFixture.enableInspections(new ExtraBlankLineInspection());
myFixture.enableInspections(new IncorrectDelimiterInspection());
myFixture.enableInspections(new LowercaseKeyInspection());
myFixture.doHighlighting();
List<IntentionAction> intentionActions = myFixture.getAllQuickFixes();
@@ -10,4 +10,6 @@ SPACE_INSIDE_NON_QUOTED=spaces without quotes
INCORRECT-DELIMITER=test-test
*LEADING_CHARACTER=test
*LEADING_CHARACTER=test
lower_case_KEY=test
@@ -9,4 +9,6 @@ SPACE_INSIDE_NON_QUOTED="spaces without quotes"
INCORRECT_DELIMITER=test-test
*LEADING_CHARACTER=test
*LEADING_CHARACTER=test
LOWER_CASE_KEY=test