Java: add fix to remove extra semicolon between import statements

GitOrigin-RevId: 3d390b9bbca1432e1a320201870e2c61edd07b23
This commit is contained in:
Bas Leijdekkers
2023-09-19 17:05:44 +00:00
committed by intellij-monorepo-bot
parent 1658427c0a
commit 295ce7b01b
6 changed files with 19 additions and 13 deletions
@@ -3209,14 +3209,14 @@ public final class HighlightUtil {
return null;
}
static HighlightInfo.Builder checkLoneSemicolonBetweenPackageStatements(@NotNull PsiJavaToken token, IElementType type, @NotNull LanguageLevel level) {
if (type == JavaTokenType.SEMICOLON
&& level.isAtLeast(LanguageLevel.JDK_21)
&& token.getParent() instanceof PsiImportList
&& PsiUtil.isFollowedByImport(token)) {
static HighlightInfo.Builder checkExtraSemicolonBetweenImportStatements(@NotNull PsiJavaToken token,
IElementType type,
@NotNull LanguageLevel level) {
if (type == JavaTokenType.SEMICOLON && level.isAtLeast(LanguageLevel.JDK_21) && PsiUtil.isFollowedByImport(token)) {
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR)
.range(token.getTextRange())
.descriptionAndTooltip(JavaErrorBundle.message("error.extra.semicolons.between.import.statements.not.allowed"));
.range(token)
.registerFix(QuickFixFactory.getInstance().createDeleteFix(token), null, null, null, null)
.descriptionAndTooltip(JavaErrorBundle.message("error.extra.semicolons.between.import.statements.not.allowed"));
}
return null;
}
@@ -613,7 +613,7 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
}
if (!hasErrorResults()) {
add(HighlightUtil.checkLoneSemicolonBetweenPackageStatements(token, type, myLanguageLevel));
add(HighlightUtil.checkExtraSemicolonBetweenImportStatements(token, type, myLanguageLevel));
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2021 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2023 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -118,7 +118,7 @@ public class UnnecessarySemicolonInspection extends BaseInspection implements Cl
private void checkTopLevelSemicolons(PsiElement element) {
for (PsiElement sibling = element.getFirstChild(); sibling != null; sibling = PsiTreeUtil.skipWhitespacesAndCommentsForward(sibling)) {
if (sibling instanceof PsiErrorElement) return;
if (PsiUtil.isJavaToken(sibling, JavaTokenType.SEMICOLON) && !(sibling.getParent() instanceof PsiImportList && PsiUtil.isFollowedByImport(sibling))) {
if (PsiUtil.isJavaToken(sibling, JavaTokenType.SEMICOLON) && !PsiUtil.isFollowedByImport(sibling)) {
registerError(sibling);
}
}
@@ -45,6 +45,7 @@ element.variable=variable
element.throws.list=throws list
element.extends.list=extends list
element.type.arguments=type arguments
element.type.semicolon=semicolon
element.receiver.parameter=receiver parameter
element.method.call=method call
@@ -111,8 +111,9 @@ public class ForwardCompatibilityInspection extends AbstractBaseJavaLocalInspect
@Override
public void visitJavaToken(@NotNull PsiJavaToken token) {
super.visitJavaToken(token);
if (languageLevel.isLessThan(LanguageLevel.JDK_21) && token.getParent() instanceof PsiImportList
&& token.getTokenType() == JavaTokenType.SEMICOLON && PsiUtil.isFollowedByImport(token)) {
if (languageLevel.isLessThan(LanguageLevel.JDK_21) &&
token.getTokenType() == JavaTokenType.SEMICOLON &&
PsiUtil.isFollowedByImport(token)) {
String message = JavaErrorBundle.message("redundant.semicolon.warn");
holder.registerProblem(token, message, new UnnecessarySemicolonInspection.UnnecessarySemicolonFix());
}
@@ -43,7 +43,8 @@ public enum JavaElementKind {
EXTENDS_LIST("element.extends.list"),
RECEIVER_PARAMETER("element.receiver.parameter"),
METHOD_CALL("element.method.call"),
TYPE_ARGUMENTS("element.type.arguments");
TYPE_ARGUMENTS("element.type.arguments"),
SEMICOLON("element.type.semicolon");
private final @PropertyKey(resourceBundle = JavaPsiBundle.BUNDLE) String propertyKey;
@@ -191,6 +192,9 @@ public enum JavaElementKind {
if (element instanceof PsiSnippetDocTagBody) {
return SNIPPET_BODY;
}
if (PsiUtil.isJavaToken(element, JavaTokenType.SEMICOLON)) {
return SEMICOLON;
}
return UNKNOWN;
}
}