IDEA-89627 (Inspection "<code>...</code> can be replaced with {@code}" doesn't work with embedded curly braces.)

This commit is contained in:
Bas Leijdekkers
2012-08-06 17:37:59 +02:00
parent d159e9a282
commit 7710ee1316
12 changed files with 184 additions and 44 deletions
@@ -1867,8 +1867,7 @@ unqualified.inner.class.access.option=Ignore references to local inner classes
try.with.identical.catches.quickfix=Collapse catch blocks into multi-catch
confusing.else.option=<html>Also report when there are no more statements after the 'if' statement</html>
html.tag.can.be.javadoc.tag.display.name=<code>...</code> can be replaced with {@code ...}
html.tag.can.be.javadoc.tag.problem.descriptor1=<code>#ref...\\&lt;/code\\&gt;</code> can be replaced with '{@code ...}' #loc
html.tag.can.be.javadoc.tag.problem.descriptor2=<code>\\&lt;code\\&gt;...#ref</code> can be replaced with '{@code ...}' #loc
html.tag.can.be.javadoc.tag.problem.descriptor=<code>#ref...\\&lt;/code\\&gt;</code> can be replaced with '{@code ...}' #loc
html.tag.can.be.javadoc.tag.quickfix=Replace with '{@code ...}'
try.finally.can.be.try.with.resources.display.name='try finally' replaceable with 'try' with resources
try.finally.can.be.try.with.resources.problem.descriptor=<code>#ref</code> can use automatic resource management #loc
@@ -1,5 +1,5 @@
/*
* Copyright 2011 Bas Leijdekkers
* Copyright 2011-2012 Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -41,32 +41,22 @@ public class HtmlTagCanBeJavadocTagInspection extends BaseInspection {
@NotNull
@Override
public String getDisplayName() {
return InspectionGadgetsBundle.message(
"html.tag.can.be.javadoc.tag.display.name");
return InspectionGadgetsBundle.message("html.tag.can.be.javadoc.tag.display.name");
}
@NotNull
@Override
protected String buildErrorString(Object... infos) {
final boolean startTag = ((Boolean)infos[0]).booleanValue();
if (startTag) {
return InspectionGadgetsBundle.message(
"html.tag.can.be.javadoc.tag.problem.descriptor1");
}
else {
return InspectionGadgetsBundle.message(
"html.tag.can.be.javadoc.tag.problem.descriptor2");
}
return InspectionGadgetsBundle.message("html.tag.can.be.javadoc.tag.problem.descriptor");
}
@Override
protected InspectionGadgetsFix buildFix(Object... infos) {
final int offset = ((Integer)infos[1]).intValue();
final int offset = ((Integer)infos[0]).intValue();
return new HtmlTagCanBeJavaDocTagFix(offset);
}
private static class HtmlTagCanBeJavaDocTagFix
extends InspectionGadgetsFix {
private static class HtmlTagCanBeJavaDocTagFix extends InspectionGadgetsFix {
private final int startIndex;
@@ -74,6 +64,7 @@ public class HtmlTagCanBeJavadocTagInspection extends BaseInspection {
this.startIndex = startIndex;
}
@Override
@NotNull
public String getName() {
return InspectionGadgetsBundle.message(
@@ -81,36 +72,40 @@ public class HtmlTagCanBeJavadocTagInspection extends BaseInspection {
}
@Override
protected void doFix(Project project, ProblemDescriptor descriptor)
throws IncorrectOperationException {
protected void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException {
final PsiElement element = descriptor.getPsiElement();
final PsiDocComment comment =
PsiTreeUtil.getParentOfType(element, PsiDocComment.class);
final PsiDocComment comment = PsiTreeUtil.getParentOfType(element, PsiDocComment.class);
if (comment == null) {
return;
}
@NonNls final StringBuilder newCommentText = new StringBuilder();
buildNewCommentText(comment, element, newCommentText);
final PsiElementFactory factory =
JavaPsiFacade.getElementFactory(project);
final PsiDocComment newComment =
factory.createDocCommentFromText(newCommentText.toString());
buildNewCommentText(comment, element, false, newCommentText);
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
final PsiDocComment newComment = factory.createDocCommentFromText(newCommentText.toString());
comment.replace(newComment);
}
private void buildNewCommentText(PsiElement element,
PsiElement elementToReplace,
@NonNls StringBuilder newCommentText) {
private boolean buildNewCommentText(PsiElement element, PsiElement elementToReplace, boolean missingEndTag,
@NonNls StringBuilder newCommentText) {
final PsiElement[] children = element.getChildren();
if (children.length != 0) {
for (PsiElement child : children) {
buildNewCommentText(child, elementToReplace,
newCommentText);
missingEndTag = buildNewCommentText(child, elementToReplace, missingEndTag, newCommentText);
}
return;
return missingEndTag;
}
@NonNls final String text = element.getText();
if (element != elementToReplace) {
if (missingEndTag) {
final int endIndex = text.indexOf("</code>");
if (endIndex >= 0) {
final String codeText = text.substring(0, endIndex);
newCommentText.append(codeText);
newCommentText.append('}');
newCommentText.append(text.substring(endIndex + 7));
return false;
}
}
newCommentText.append(text);
}
else {
@@ -118,8 +113,7 @@ public class HtmlTagCanBeJavadocTagInspection extends BaseInspection {
newCommentText.append("{@code ");
final int endIndex = text.indexOf("</code>", startIndex);
if (endIndex >= 0) {
final String codeText =
text.substring(startIndex + 6, endIndex);
final String codeText = text.substring(startIndex + 6, endIndex);
newCommentText.append(codeText);
//StringUtil.replace(codeText, "}", "&#125;"));
newCommentText.append('}');
@@ -129,9 +123,10 @@ public class HtmlTagCanBeJavadocTagInspection extends BaseInspection {
final String codeText = text.substring(startIndex + 6);
newCommentText.append(codeText);
//StringUtil.replace(codeText, "}", "&#125;"));
newCommentText.append('}');
return true;
}
}
return missingEndTag;
}
}
@@ -140,8 +135,7 @@ public class HtmlTagCanBeJavadocTagInspection extends BaseInspection {
return new HtmlTagCanBeJavaDocTagVisitor();
}
private static class HtmlTagCanBeJavaDocTagVisitor
extends BaseInspectionVisitor {
private static class HtmlTagCanBeJavaDocTagVisitor extends BaseInspectionVisitor {
@Override
public void visitDocToken(PsiDocToken token) {
@@ -160,16 +154,31 @@ public class HtmlTagCanBeJavadocTagInspection extends BaseInspection {
if (startIndex < 0) {
return;
}
registerErrorAtOffset(token, startIndex, 6, Boolean.TRUE,
Integer.valueOf(startIndex));
final int endIndex = text.indexOf("</code>", startIndex);
if (endIndex < 0) {
return;
if (hasMatchingCloseTag(token, startIndex + 6)) {
registerErrorAtOffset(token, startIndex, 6, Integer.valueOf(startIndex));
}
registerErrorAtOffset(token, endIndex, 7, Boolean.FALSE,
Integer.valueOf(startIndex));
startIndex++;
}
}
private static boolean hasMatchingCloseTag(PsiElement element, int offset) {
final String text = element.getText();
final int endOffset1 = text.indexOf("</code>", offset);
if (endOffset1 >= 0) {
final int startOffset1 = text.indexOf("<code>", offset);
return startOffset1 < 0 || startOffset1 > endOffset1;
}
PsiElement sibling = element.getNextSibling();
while (sibling != null) {
final String text1 = sibling.getText();
final int endOffset = text1.indexOf("</code>");
if (endOffset >= 0) {
final int startOffset = text1.indexOf("<code>");
return startOffset < 0 || startOffset > endOffset;
}
sibling = sibling.getNextSibling();
}
return false;
}
}
}
@@ -0,0 +1,10 @@
package com.siyeh.igfixes.javadoc.html_tag_can_be_javadoc_tag;
class Braces {
/**
* {@code if (something) { this.doSomething(); }}
*/
void foo() {}
}
@@ -0,0 +1,10 @@
package com.siyeh.igfixes.javadoc.html_tag_can_be_javadoc_tag;
class Braces {
/**
* <co<caret>de>if (something) { this.doSomething(); }</code>
*/
void foo() {}
}
@@ -0,0 +1,10 @@
class Multiline {
/**
* {@code
* asdf
* }
*/
void foo1() {}
}
@@ -0,0 +1,10 @@
class Multiline {
/**
* <co<caret>de>
* asdf
* </code>
*/
void foo1() {}
}
@@ -0,0 +1,9 @@
package com.siyeh.igfixes.javadoc.html_tag_can_be_javadoc_tag;
class Second {
/**
* <code></code>{@code }
*/
void foo2() {}
}
@@ -0,0 +1,9 @@
package com.siyeh.igfixes.javadoc.html_tag_can_be_javadoc_tag;
class Second {
/**
* <code></code><c<caret>ode></code>
*/
void foo2() {}
}
@@ -0,0 +1,13 @@
package com.siyeh.igtest.javadoc.html_tag_can_be_javadoc_tag;
class HtmlTagCanBeJavadocTag {
/**
* <code>if (something) { this.doSomething(); }</code>
* <code>
* asdf
* </code>
* <code></code><code></code>
*/
void foo() {}
}
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>HtmlTagCanBeJavadocTag.java</file>
<line>6</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">&lt;code&gt;...&lt;/code&gt; can be replaced with {@code ...}</problem_class>
<description>&lt;code&gt;&lt;code&gt;...&amp;lt;/code&amp;gt;&lt;/code&gt; can be replaced with '{@code ...}' #loc</description>
</problem>
<problem>
<file>HtmlTagCanBeJavadocTag.java</file>
<line>7</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">&lt;code&gt;...&lt;/code&gt; can be replaced with {@code ...}</problem_class>
<description>&lt;code&gt;&lt;code&gt;...&amp;lt;/code&amp;gt;&lt;/code&gt; can be replaced with '{@code ...}' #loc</description>
</problem>
<problem>
<file>HtmlTagCanBeJavadocTag.java</file>
<line>10</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">&lt;code&gt;...&lt;/code&gt; can be replaced with {@code ...}</problem_class>
<description>&lt;code&gt;&lt;code&gt;...&amp;lt;/code&amp;gt;&lt;/code&gt; can be replaced with '{@code ...}' #loc</description>
</problem>
<problem>
<file>HtmlTagCanBeJavadocTag.java</file>
<line>10</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">&lt;code&gt;...&lt;/code&gt; can be replaced with {@code ...}</problem_class>
<description>&lt;code&gt;&lt;code&gt;...&amp;lt;/code&amp;gt;&lt;/code&gt; can be replaced with '{@code ...}' #loc</description>
</problem>
</problems>
@@ -0,0 +1,20 @@
package com.siyeh.ig.fixes.javadoc;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.IGQuickFixesTestCase;
import com.siyeh.ig.javadoc.HtmlTagCanBeJavadocTagInspection;
public class HtmlTagCanBeJavadocTagFixTest extends IGQuickFixesTestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
myFixture.enableInspections(new HtmlTagCanBeJavadocTagInspection());
myRelativePath = "javadoc/html_tag_can_be_javadoc_tag";
myDefaultHint = InspectionGadgetsBundle.message("html.tag.can.be.javadoc.tag.quickfix");
}
public void testBraces() { doTest(); }
public void testSecond() { doTest(); }
public void testMultiline() { doTest(); }
}
@@ -0,0 +1,10 @@
package com.siyeh.ig.javadoc;
import com.siyeh.ig.IGInspectionTestCase;
public class HtmlTagCanBeJavadocTagInspectionTest extends IGInspectionTestCase {
public void test() throws Exception {
doTest("com/siyeh/igtest/javadoc/html_tag_can_be_javadoc_tag", new HtmlTagCanBeJavadocTagInspection());
}
}