IDEA-251250 add quickfix for JavaDocs annotations for inspection "tag description is missing"

GitOrigin-RevId: 144bc690511b94af09184780d3621ff1e31b9068
This commit is contained in:
Andrey.Cherkasov
2020-10-21 11:33:22 +00:00
committed by intellij-monorepo-bot
parent 27d30a9099
commit f746cdbdc4
15 changed files with 201 additions and 3 deletions
@@ -90,6 +90,10 @@ public class JavaDocLocalInspection extends LocalInspectionTool {
return new AddUnknownTagToCustoms(this, tag);
}
private static LocalQuickFix createRemoveTagWithoutDescriptionFix(@NotNull String tag) {
return new RemoveTagWithoutDescriptionFix(tag);
}
public void setPackageOption(String modifier, String tags) {
PACKAGE_OPTIONS.ACCESS_JAVADOC_REQUIRED_FOR = modifier;
PACKAGE_OPTIONS.REQUIRED_TAGS = tags;
@@ -793,6 +797,34 @@ public class JavaDocLocalInspection extends LocalInspectionTool {
}
}
private static class RemoveTagWithoutDescriptionFix implements LocalQuickFix {
private final String myTag;
RemoveTagWithoutDescriptionFix(String tag) {
myTag = tag;
}
@NotNull
@Override
public String getName() {
return CommonQuickFixBundle.message("fix.remove", myTag);
}
@NotNull
@Override
public String getFamilyName() {
return JavaBundle.message("quickfix.family.remove.javadoc.tag");
}
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiDocTag myTag = PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), PsiDocTag.class);
if (myTag != null) {
myTag.delete();
}
}
}
private final class ProblemHolderImpl implements JavadocHighlightUtil.ProblemHolder {
private final ProblemsHolder myHolder;
private final boolean myOnTheFly;
@@ -844,5 +876,10 @@ public class JavaDocLocalInspection extends LocalInspectionTool {
public LocalQuickFix registerTagFix(@NotNull String tag) {
return createRegisterTagFix(tag);
}
@Override
public LocalQuickFix removeTagWithoutDescriptionFix(@NotNull String tag) {
return createRemoveTagWithoutDescriptionFix(tag);
}
}
}
@@ -45,6 +45,7 @@ public final class JavadocHighlightUtil {
LocalQuickFix addMissingTagFix(@NotNull String tag, @NotNull String value);
LocalQuickFix addMissingParamTagFix(@NotNull String name);
LocalQuickFix registerTagFix(@NotNull String tag);
LocalQuickFix removeTagWithoutDescriptionFix(@NotNull String tag);
}
static boolean isJavaDocRequired(@NotNull JavaDocLocalInspection inspection, @NotNull PsiModifierListOwner element) {
@@ -446,20 +447,25 @@ public final class JavadocHighlightUtil {
if ("return".equals(tag.getName())) {
if (emptyTag(tag)) {
String tagText = "<code>@return</code>";
holder.problem(tag.getNameElement(), JavaBundle.message("inspection.javadoc.method.problem.missing.tag.description", tagText), null);
LocalQuickFix fix = holder.removeTagWithoutDescriptionFix("@return");
holder.problem(tag.getNameElement(), JavaBundle.message("inspection.javadoc.method.problem.missing.tag.description", tagText), fix);
}
}
else if ("throws".equals(tag.getName()) || "exception".equals(tag.getName())) {
if (emptyThrowsTag(tag)) {
String tagText = "<code>" + tag.getName() + "</code>";
holder.problem(tag.getNameElement(), JavaBundle.message("inspection.javadoc.method.problem.missing.tag.description", tagText), null);
PsiDocTagValue valueElement = tag.getValueElement();
String tagTextForFix = "@" + tag.getName() + (valueElement != null ? " " + valueElement.getText() : "");
LocalQuickFix fix = holder.removeTagWithoutDescriptionFix(tagTextForFix);
holder.problem(tag.getNameElement(), JavaBundle.message("inspection.javadoc.method.problem.missing.tag.description", tagText), fix);
}
}
else if ("param".equals(tag.getName())) {
PsiDocTagValue valueElement = tag.getValueElement();
if (valueElement != null && emptyParamTag(tag, valueElement)) {
String tagText = "<code>@param " + valueElement.getText() + "</code>";
holder.problem(valueElement, JavaBundle.message("inspection.javadoc.method.problem.missing.tag.description", tagText), null);
LocalQuickFix fix = holder.removeTagWithoutDescriptionFix("@param " + valueElement.getText());
holder.problem(valueElement, JavaBundle.message("inspection.javadoc.method.problem.missing.tag.description", tagText), fix);
}
}
}
@@ -0,0 +1,11 @@
// "Remove '@exception IllegalArgumentException'" "true"
class Foo {
/**
* @param someInt blah-blah-blah
* @return blah-blah-blah
*/
double foo(int someInt) {
return 3.14;
}
}
@@ -0,0 +1,11 @@
// "Remove '@exception'" "true"
class Foo {
/**
* @param someInt blah-blah-blah
* @return blah-blah-blah
*/
double foo(int someInt) {
return 3.14;
}
}
@@ -0,0 +1,10 @@
// "Remove '@param someInt'" "true"
class Foo {
/**
* @return blah-blah-blah
*/
double foo(int someInt) {
return 3.14;
}
}
@@ -0,0 +1,10 @@
// "Remove '@return'" "true"
class Foo {
/**
* @param someInt blah-blah-blah
*/
double foo(int someInt) {
return 3.14;
}
}
@@ -0,0 +1,11 @@
// "Remove '@throws IllegalArgumentException'" "true"
class Foo {
/**
* @param someInt blah-blah-blah
* @return blah-blah-blah
*/
double foo(int someInt) {
return 3.14;
}
}
@@ -0,0 +1,11 @@
// "Remove '@throws'" "true"
class Foo {
/**
* @param someInt blah-blah-blah
* @return blah-blah-blah
*/
double foo(int someInt) {
return 3.14;
}
}
@@ -0,0 +1,12 @@
// "Remove '@exception IllegalArgumentException'" "true"
class Foo {
/**
* @param someInt blah-blah-blah
* @return blah-blah-blah
* @exception<caret> IllegalArgumentException
*/
double foo(int someInt) {
return 3.14;
}
}
@@ -0,0 +1,12 @@
// "Remove '@exception'" "true"
class Foo {
/**
* @param someInt blah-blah-blah
* @return blah-blah-blah
* @exception<caret>
*/
double foo(int someInt) {
return 3.14;
}
}
@@ -0,0 +1,11 @@
// "Remove '@param someInt'" "true"
class Foo {
/**
* @param someInt<caret>
* @return blah-blah-blah
*/
double foo(int someInt) {
return 3.14;
}
}
@@ -0,0 +1,11 @@
// "Remove '@return'" "true"
class Foo {
/**
* @param someInt blah-blah-blah
* @return<caret>
*/
double foo(int someInt) {
return 3.14;
}
}
@@ -0,0 +1,12 @@
// "Remove '@throws IllegalArgumentException'" "true"
class Foo {
/**
* @param someInt blah-blah-blah
* @return blah-blah-blah
* @throws<caret> IllegalArgumentException
*/
double foo(int someInt) {
return 3.14;
}
}
@@ -0,0 +1,12 @@
// "Remove '@throws'" "true"
class Foo {
/**
* @param someInt blah-blah-blah
* @return blah-blah-blah
* @throws<caret>
*/
double foo(int someInt) {
return 3.14;
}
}
@@ -0,0 +1,21 @@
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.java.codeInsight.intention;
import com.intellij.codeInsight.daemon.LightIntentionActionTestCase;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.javaDoc.JavaDocLocalInspection;
import org.jetbrains.annotations.NotNull;
public class RemoveJavadocTagWithoutDescriptionTest extends LightIntentionActionTestCase {
@Override
protected LocalInspectionTool @NotNull [] configureLocalInspectionTools() {
return new LocalInspectionTool[]{
new JavaDocLocalInspection()
};
}
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/removeJavadocTagWithoutDescription";
}
}