Added inspection and quickfix for lines with trailing whitespace (only for values without quotes)

GitOrigin-RevId: 4e159cdff71a299732a3791da46695a20e062ce9
This commit is contained in:
marcelhillesheim
2021-10-21 17:32:25 +02:00
committed by intellij-monorepo-bot
parent 41bb4a91c7
commit 8c123235ba
6 changed files with 104 additions and 0 deletions
@@ -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.openapi.util.TextRange;
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.DotEnvValue;
public class TrailingWhitespaceInspection 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 "Value has trailing whitespace";
}
@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, DotEnvValue.class).forEach(dotEnvValue -> {
if (dotEnvValue.getText().matches(".*[ \\t]+")) {
problemsHolder.registerProblem(dotEnvValue,
new TextRange(dotEnvValue.getText().stripTrailing().length(), dotEnvValue.getText().length()),
"Line has trailing whitespace.",
new TrailingWhitespaceInspection.RemoveTrailingWhitespaceQuickFix()
);
}
});
return problemsHolder;
}
private static class RemoveTrailingWhitespaceQuickFix implements LocalQuickFix {
@NotNull
@Override
public String getName() {
return "Remove trailing whitespace";
}
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
try {
PsiElement psiElement = descriptor.getPsiElement();
PsiElement newPsiElement = DotEnvFactory.createFromText(project, DotEnvTypes.VALUE,
"DUMMMY_KEY="+psiElement.getText().stripTrailing());
psiElement.replace(newPsiElement);
} catch (IncorrectOperationException e) {
Logger.getInstance(IncorrectDelimiterInspection.class).error(e);
}
}
@NotNull
public String getFamilyName() {
return getName();
}
}
}
@@ -134,6 +134,13 @@
level="WARNING"
implementationClass="ru.adelf.idea.dotenv.inspections.LowercaseKeyInspection"/>
<localInspection language="DotEnv"
groupName="DotEnv"
shortName="DotEnvTrailingWhitespace"
displayName="Value has trailing whitespac"
enabledByDefault="true"
level="WARNING" implementationClass="ru.adelf.idea.dotenv.inspections.TrailingWhitespaceInspection"/>
<lang.syntaxHighlighterFactory language="DotEnv"
implementationClass="ru.adelf.idea.dotenv.DotEnvSyntaxHighlighterFactory"/>
@@ -0,0 +1,5 @@
<html>
<body>
Reports trailing whitespaces.
</body>
</html>
@@ -59,6 +59,11 @@ public class InspectionsTest extends DotEnvLightCodeInsightFixtureTestCase {
doInspectionTest(new LowercaseKeyInspection(), Collections.singletonList("lower_case_KEY"));
}
@Test
public void testTrailingWhitespace() {
doInspectionTest(new TrailingWhitespaceInspection(), Arrays.asList(" ", " "));
}
// Every available quickfix from every inspection is getting applied
@Test
public void testQuickFixes() {
@@ -66,6 +71,7 @@ public class InspectionsTest extends DotEnvLightCodeInsightFixtureTestCase {
myFixture.enableInspections(new ExtraBlankLineInspection());
myFixture.enableInspections(new IncorrectDelimiterInspection());
myFixture.enableInspections(new LowercaseKeyInspection());
myFixture.enableInspections(new TrailingWhitespaceInspection());
myFixture.doHighlighting();
List<IntentionAction> intentionActions = myFixture.getAllQuickFixes();
@@ -12,4 +12,7 @@ INCORRECT-DELIMITER=test-test
*LEADING_CHARACTER=test
TRAILING_WHITESPACE_SPACE=space
TRAILING_WHITESPACE_TAB=tab
lower_case_KEY=test
@@ -11,4 +11,7 @@ INCORRECT_DELIMITER=test-test
*LEADING_CHARACTER=test
TRAILING_WHITESPACE_SPACE=space
TRAILING_WHITESPACE_TAB=tab
LOWER_CASE_KEY=test