Implemented 885993 (Wrap all comments into TextBlocks).
Draft implementation for 744970 (Forbid endline comments)
This commit is contained in:
parent
9696781072
commit
803dafc96a
|
|
@ -45,9 +45,10 @@ class Comment implements TextBlock
|
|||
* @param aText the lines that make up the comment.
|
||||
* @param aFirstCol number of the first column of the comment.
|
||||
* @param aLastLine number of the last line of the comment.
|
||||
* @param aLastCol number of the last column of the comment.
|
||||
*/
|
||||
public Comment(final String[] aText, final int aFirstCol,
|
||||
final int aLastLine)
|
||||
final int aLastLine, final int aLastCol)
|
||||
{
|
||||
mText = new String[aText.length];
|
||||
for (int i = 0; i < mText.length; i++) {
|
||||
|
|
@ -56,7 +57,7 @@ class Comment implements TextBlock
|
|||
mFirstLine = aLastLine - mText.length + 1;
|
||||
mLastLine = aLastLine;
|
||||
mFirstCol = aFirstCol;
|
||||
mLastCol = mText[mText.length - 1].length() - 1;
|
||||
mLastCol = aLastCol;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -99,8 +99,11 @@ public final class FileContents implements CommentListener
|
|||
**/
|
||||
public void reportCppComment(int aStartLineNo, int aStartColNo)
|
||||
{
|
||||
final String cmt = mLines[aStartLineNo - 1].substring(aStartColNo);
|
||||
mCPlusPlusComments.put(new Integer(aStartLineNo), cmt);
|
||||
final String line = mLines[aStartLineNo - 1];
|
||||
final String[] txt = new String[] {line.substring(aStartColNo)};
|
||||
final Comment comment =
|
||||
new Comment(txt, aStartColNo, aStartLineNo, line.length() - 1);
|
||||
mCPlusPlusComments.put(new Integer(aStartLineNo), comment);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -125,23 +128,23 @@ public final class FileContents implements CommentListener
|
|||
{
|
||||
final String[] cc = extractCComment(aStartLineNo, aStartColNo,
|
||||
aEndLineNo, aEndColNo);
|
||||
final Comment comment = new Comment(cc, aStartColNo, aEndLineNo,
|
||||
aEndColNo);
|
||||
|
||||
// save the comment
|
||||
final Integer key = new Integer(aStartLineNo);
|
||||
if (mCComments.containsKey(key)) {
|
||||
final List entries = (List) mCComments.get(key);
|
||||
entries.add(cc);
|
||||
entries.add(comment);
|
||||
}
|
||||
else {
|
||||
final List entries = new ArrayList();
|
||||
entries.add(cc);
|
||||
entries.add(comment);
|
||||
mCComments.put(key, entries);
|
||||
}
|
||||
|
||||
// Remember if possible Javadoc comment
|
||||
if (mLines[aStartLineNo - 1].indexOf("/**", aStartColNo) != -1) {
|
||||
Comment comment = new Comment(cc, aStartColNo, aEndLineNo);
|
||||
|
||||
mJavadocComments.put(new Integer(aEndLineNo - 1), comment);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ import java.util.Iterator;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.api.TextBlock;
|
||||
import com.puppycrawl.tools.checkstyle.api.DetailAST;
|
||||
import com.puppycrawl.tools.checkstyle.api.FileContents;
|
||||
|
||||
|
|
@ -85,7 +86,7 @@ public class TodoCommentCheck
|
|||
final Map comments = aContents.getCppComments();
|
||||
for (final Iterator it = comments.keySet().iterator(); it.hasNext();) {
|
||||
final Integer key = (Integer) it.next();
|
||||
final String cmt = (String) comments.get(key);
|
||||
final String cmt = ((TextBlock) comments.get(key)).getText()[0];
|
||||
if (getRegexp().match(cmt)) {
|
||||
log(key.intValue(), "todo.match", getFormat());
|
||||
}
|
||||
|
|
@ -105,7 +106,7 @@ public class TodoCommentCheck
|
|||
final List lineComments = (List) allComments.get(key);
|
||||
final Iterator lineIter = lineComments.iterator();
|
||||
while (lineIter.hasNext()) {
|
||||
final String[] cmt = (String[]) lineIter.next();
|
||||
final String[] cmt = ((TextBlock) lineIter.next()).getText();
|
||||
for (int i = 0; i < cmt.length; i++) {
|
||||
if (getRegexp().match(cmt[i])) {
|
||||
log(key.intValue() + i, "todo.match", getFormat());
|
||||
|
|
|
|||
|
|
@ -0,0 +1,108 @@
|
|||
////////////////////////////////////////////////////////////////////////////////
|
||||
// checkstyle: Checks Java source code for adherence to a set of rules.
|
||||
// Copyright (C) 2001-2004 Oliver Burn
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
package com.puppycrawl.tools.checkstyle.checks;
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.api.DetailAST;
|
||||
import com.puppycrawl.tools.checkstyle.api.TextBlock;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.beanutils.ConversionException;
|
||||
import org.apache.regexp.RE;
|
||||
|
||||
/**
|
||||
* The check.
|
||||
* @author o_sukhodolsky
|
||||
*/
|
||||
public class TrailingCommentCheck extends AbstractFormatCheck
|
||||
{
|
||||
/** default format for allowed blank line. */
|
||||
private static final String DEFAULT_FORMAT = "^[\\s\\}\\);]*$";
|
||||
/**
|
||||
* Creates new instance of the check.
|
||||
* @throws ConversionException unable to parse DEFAULT_FORMAT.
|
||||
*/
|
||||
public TrailingCommentCheck() throws ConversionException
|
||||
{
|
||||
super(DEFAULT_FORMAT);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public int[] getDefaultTokens()
|
||||
{
|
||||
return new int[0];
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public void visitToken(DetailAST aAST)
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public void beginTree(DetailAST aRootAST)
|
||||
{
|
||||
final RE blankLinePattern = getRegexp();
|
||||
final Map cppComments = getFileContents().getCppComments();
|
||||
final Map cComments = getFileContents().getCComments();
|
||||
final Set lines = new HashSet();
|
||||
lines.addAll(cppComments.keySet());
|
||||
lines.addAll(cComments.keySet());
|
||||
|
||||
final Iterator linesIter = lines.iterator();
|
||||
while (linesIter.hasNext()) {
|
||||
final Integer lineNo = (Integer) linesIter.next();
|
||||
// I don't want handle several comments on one line :(
|
||||
// Perhaps I'm wrong :)
|
||||
if (cppComments.containsKey(lineNo)
|
||||
&& cComments.containsKey(lineNo)
|
||||
|| cComments.containsKey(lineNo)
|
||||
&& ((List) cComments.get(lineNo)).size() > 1)
|
||||
{
|
||||
log(lineNo.intValue(), "Too many comments.");
|
||||
continue;
|
||||
}
|
||||
|
||||
final String line = getLines()[lineNo.intValue() - 1];
|
||||
String lineBefore = "";
|
||||
String lineAfter = "";
|
||||
if (cppComments.containsKey(lineNo)) {
|
||||
final TextBlock comment = (TextBlock) cppComments.get(lineNo);
|
||||
lineBefore = line.substring(0, comment.getStartColNo());
|
||||
}
|
||||
else if (cComments.containsKey(lineNo)) {
|
||||
final List commentList = (List) cComments.get(lineNo);
|
||||
final TextBlock comment =
|
||||
(TextBlock) commentList.iterator().next();
|
||||
lineBefore = line.substring(0, comment.getStartColNo());
|
||||
if (comment.getText().length == 1) {
|
||||
lineAfter = line.substring(comment.getEndColNo() + 1);
|
||||
}
|
||||
}
|
||||
lineAfter = lineAfter.trim();
|
||||
if (!blankLinePattern.match(lineBefore) || !"".equals(lineAfter)) {
|
||||
log(lineNo.intValue(), "trailing.comments");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -25,3 +25,4 @@ descendant.token.min=Count of {0} for ''{2}'' descendant ''{3}'' is less than mi
|
|||
descendant.token.max=Count of {0} for ''{2}'' descendant ''{3}'' exceeds maximum count {1}.
|
||||
|
||||
final.parameter=Parameter {0} should be final.
|
||||
trailing.comments=Don''t use trailing comments.
|
||||
|
|
|
|||
|
|
@ -0,0 +1,18 @@
|
|||
public class InputTrailingComment {
|
||||
int i; // don't use trailing comments :)
|
||||
// it fine to have comment w/o any statement
|
||||
/* good c-style comment. */
|
||||
int j; /* bad c-style comment. */
|
||||
void method1() { /* some c-style multi-line
|
||||
comment*/
|
||||
Runnable r = (new Runnable() {
|
||||
public void run() {
|
||||
}
|
||||
}); /* we should allow this */
|
||||
} // we should allow this
|
||||
/*
|
||||
Let's check multi-line comments.
|
||||
*/
|
||||
/* c-style */ // we do not allow several comments on one line
|
||||
/* c-style 1 */ /*c-style 2 */
|
||||
}
|
||||
|
|
@ -24,6 +24,7 @@ public class AllTests {
|
|||
suite.addTest(new TestSuite(NewlineAtEndOfFileCheckTest.class));
|
||||
suite.addTest(new TestSuite(RedundantModifierTest.class));
|
||||
suite.addTest(new TestSuite(TodoCommentCheckTest.class));
|
||||
suite.addTest(new TestSuite(TrailingCommentCheckTest.class));
|
||||
suite.addTest(new TestSuite(TranslationCheckTest.class));
|
||||
suite.addTest(new TestSuite(UncommentedMainCheckTest.class));
|
||||
suite.addTest(new TestSuite(UpperEllCheckTest.class));
|
||||
|
|
|
|||
|
|
@ -0,0 +1,23 @@
|
|||
package com.puppycrawl.tools.checkstyle.checks;
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.BaseCheckTestCase;
|
||||
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
|
||||
|
||||
public class TrailingCommentCheckTest extends BaseCheckTestCase
|
||||
{
|
||||
public void testDefaultTokens()
|
||||
throws Exception
|
||||
{
|
||||
final DefaultConfiguration checkConfig =
|
||||
createCheckConfig(TrailingCommentCheck.class);
|
||||
// checkConfig.addAttribute("tokens", "CTOR_DEF");
|
||||
final String[] expected = {
|
||||
"2: Don't use trailing comments.",
|
||||
"5: Don't use trailing comments.",
|
||||
"6: Don't use trailing comments.",
|
||||
"16: Too many comments.",
|
||||
"17: Too many comments.",
|
||||
};
|
||||
verify(checkConfig, getPath("InputTrailingComment.java"), expected);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue