diff --git a/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties b/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties index 85e09b595ccb..5c766cba8318 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties +++ b/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties @@ -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=Also report when there are no more statements after the 'if' statement html.tag.can.be.javadoc.tag.display.name=... can be replaced with {@code ...} -html.tag.can.be.javadoc.tag.problem.descriptor1=#ref...\\</code\\> can be replaced with '{@code ...}' #loc -html.tag.can.be.javadoc.tag.problem.descriptor2=\\<code\\>...#ref can be replaced with '{@code ...}' #loc +html.tag.can.be.javadoc.tag.problem.descriptor=#ref...\\</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=#ref can use automatic resource management #loc diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/javadoc/HtmlTagCanBeJavadocTagInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/javadoc/HtmlTagCanBeJavadocTagInspection.java index 6912b4a38e73..261382652a47 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/javadoc/HtmlTagCanBeJavadocTagInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/javadoc/HtmlTagCanBeJavadocTagInspection.java @@ -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(""); + 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("", 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, "}", "}")); newCommentText.append('}'); @@ -129,9 +123,10 @@ public class HtmlTagCanBeJavadocTagInspection extends BaseInspection { final String codeText = text.substring(startIndex + 6); newCommentText.append(codeText); //StringUtil.replace(codeText, "}", "}")); - 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("", 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("", offset); + if (endOffset1 >= 0) { + final int startOffset1 = text.indexOf("", offset); + return startOffset1 < 0 || startOffset1 > endOffset1; + } + PsiElement sibling = element.getNextSibling(); + while (sibling != null) { + final String text1 = sibling.getText(); + final int endOffset = text1.indexOf(""); + if (endOffset >= 0) { + final int startOffset = text1.indexOf(""); + return startOffset < 0 || startOffset > endOffset; + } + sibling = sibling.getNextSibling(); + } + return false; + } } } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/javadoc/html_tag_can_be_javadoc_tag/Braces.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/javadoc/html_tag_can_be_javadoc_tag/Braces.after.java new file mode 100644 index 000000000000..70a390aac8f9 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/javadoc/html_tag_can_be_javadoc_tag/Braces.after.java @@ -0,0 +1,10 @@ +package com.siyeh.igfixes.javadoc.html_tag_can_be_javadoc_tag; + +class Braces { + + /** + * {@code if (something) { this.doSomething(); }} + */ + void foo() {} + +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/javadoc/html_tag_can_be_javadoc_tag/Braces.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/javadoc/html_tag_can_be_javadoc_tag/Braces.java new file mode 100644 index 000000000000..9565e0c1a05a --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/javadoc/html_tag_can_be_javadoc_tag/Braces.java @@ -0,0 +1,10 @@ +package com.siyeh.igfixes.javadoc.html_tag_can_be_javadoc_tag; + +class Braces { + + /** + * de>if (something) { this.doSomething(); } + */ + void foo() {} + +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/javadoc/html_tag_can_be_javadoc_tag/Multiline.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/javadoc/html_tag_can_be_javadoc_tag/Multiline.after.java new file mode 100644 index 000000000000..7279e6f0ee07 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/javadoc/html_tag_can_be_javadoc_tag/Multiline.after.java @@ -0,0 +1,10 @@ +class Multiline { + + /** + * {@code + * asdf + * } + */ + void foo1() {} + +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/javadoc/html_tag_can_be_javadoc_tag/Multiline.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/javadoc/html_tag_can_be_javadoc_tag/Multiline.java new file mode 100644 index 000000000000..ad920dddf63e --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/javadoc/html_tag_can_be_javadoc_tag/Multiline.java @@ -0,0 +1,10 @@ +class Multiline { + + /** + * de> + * asdf + * + */ + void foo1() {} + +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/javadoc/html_tag_can_be_javadoc_tag/Second.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/javadoc/html_tag_can_be_javadoc_tag/Second.after.java new file mode 100644 index 000000000000..1added58029a --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/javadoc/html_tag_can_be_javadoc_tag/Second.after.java @@ -0,0 +1,9 @@ +package com.siyeh.igfixes.javadoc.html_tag_can_be_javadoc_tag; + +class Second { + + /** + * {@code } + */ + void foo2() {} +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/javadoc/html_tag_can_be_javadoc_tag/Second.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/javadoc/html_tag_can_be_javadoc_tag/Second.java new file mode 100644 index 000000000000..06eff528c18c --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/javadoc/html_tag_can_be_javadoc_tag/Second.java @@ -0,0 +1,9 @@ +package com.siyeh.igfixes.javadoc.html_tag_can_be_javadoc_tag; + +class Second { + + /** + * ode> + */ + void foo2() {} +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/javadoc/html_tag_can_be_javadoc_tag/HtmlTagCanBeJavadocTag.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/javadoc/html_tag_can_be_javadoc_tag/HtmlTagCanBeJavadocTag.java new file mode 100644 index 000000000000..ee34efca91e3 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/javadoc/html_tag_can_be_javadoc_tag/HtmlTagCanBeJavadocTag.java @@ -0,0 +1,13 @@ +package com.siyeh.igtest.javadoc.html_tag_can_be_javadoc_tag; + +class HtmlTagCanBeJavadocTag { + + /** + * if (something) { this.doSomething(); } + * + * asdf + * + * + */ + void foo() {} +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/javadoc/html_tag_can_be_javadoc_tag/expected.xml b/plugins/InspectionGadgets/test/com/siyeh/igtest/javadoc/html_tag_can_be_javadoc_tag/expected.xml new file mode 100644 index 000000000000..23198af9c01d --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/javadoc/html_tag_can_be_javadoc_tag/expected.xml @@ -0,0 +1,31 @@ + + + + + HtmlTagCanBeJavadocTag.java + 6 + <code>...</code> can be replaced with {@code ...} + <code><code>...&lt;/code&gt;</code> can be replaced with '{@code ...}' #loc + + + + HtmlTagCanBeJavadocTag.java + 7 + <code>...</code> can be replaced with {@code ...} + <code><code>...&lt;/code&gt;</code> can be replaced with '{@code ...}' #loc + + + + HtmlTagCanBeJavadocTag.java + 10 + <code>...</code> can be replaced with {@code ...} + <code><code>...&lt;/code&gt;</code> can be replaced with '{@code ...}' #loc + + + + HtmlTagCanBeJavadocTag.java + 10 + <code>...</code> can be replaced with {@code ...} + <code><code>...&lt;/code&gt;</code> can be replaced with '{@code ...}' #loc + + \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/javadoc/HtmlTagCanBeJavadocTagFixTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/javadoc/HtmlTagCanBeJavadocTagFixTest.java new file mode 100644 index 000000000000..dd27b827b1f8 --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/javadoc/HtmlTagCanBeJavadocTagFixTest.java @@ -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(); } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/javadoc/HtmlTagCanBeJavadocTagInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/javadoc/HtmlTagCanBeJavadocTagInspectionTest.java new file mode 100644 index 000000000000..bbbf536522ce --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/javadoc/HtmlTagCanBeJavadocTagInspectionTest.java @@ -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()); + } +} \ No newline at end of file