Fully implemented the paren pad check

This commit is contained in:
Oliver Burn 2002-10-13 14:05:42 +00:00
parent 5fd3fab179
commit aede54ee77
2 changed files with 124 additions and 1 deletions

View File

@ -20,12 +20,17 @@
package com.puppycrawl.tools.checkstyle.checks;
import com.puppycrawl.tools.checkstyle.JavaTokenTypes;
import com.puppycrawl.tools.checkstyle.PadOption;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.api.Utils;
import org.apache.commons.beanutils.ConversionException;
public class ParenPadCheck
extends Check
{
private PadOption mOption = PadOption.NOSPACE;
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public int[] getDefaultTokens()
{
@ -41,6 +46,59 @@ public class ParenPadCheck
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public void visitToken(DetailAST aAST)
{
// TODO: implement
if (aAST.getType() == JavaTokenTypes.RPAREN) {
processRight(aAST);
}
else {
processLeft(aAST);
}
}
public void setOption(String aOption)
{
mOption = PadOption.decode(aOption);
if (mOption == null) {
throw new ConversionException("unable to parse " + aOption);
}
}
private void processLeft(DetailAST aAST)
{
final String line = getLines()[aAST.getLineNo() - 1];
final int after = aAST.getColumnNo() + 1;
if (after < line.length()) {
if ((PadOption.NOSPACE == mOption)
&& (Character.isWhitespace(line.charAt(after))))
{
log(aAST.getLineNo(), after, "ws.followed", "(");
}
else if ((PadOption.SPACE == mOption)
&& !Character.isWhitespace(line.charAt(after))
&& (line.charAt(after) != ')'))
{
log(aAST.getLineNo(), after, "ws.notFollowed", "(");
}
}
}
private void processRight(DetailAST aAST)
{
final String line = getLines()[aAST.getLineNo() - 1];
final int before = aAST.getColumnNo() - 1;
if (before >= 0) {
if ((PadOption.NOSPACE == mOption)
&& Character.isWhitespace(line.charAt(before))
&& !Utils.whitespaceBefore(before, line))
{
log(aAST.getLineNo(), before, "ws.preceeded", ")");
}
else if ((PadOption.SPACE == mOption)
&& !Character.isWhitespace(line.charAt(before))
&& (line.charAt(before) != '('))
{
log(aAST.getLineNo(), aAST.getColumnNo(),
"ws.notPreceeded", ")");
}
}
}
}

View File

@ -0,0 +1,65 @@
package com.puppycrawl.tools.checkstyle;
import com.puppycrawl.tools.checkstyle.checks.ParenPadCheck;
public class ParenPadCheckTest
extends BaseCheckTestCase
{
public ParenPadCheckTest(String aName)
{
super(aName);
}
public void testDefault()
throws Exception
{
final CheckConfiguration checkConfig = new CheckConfiguration();
checkConfig.setClassname(ParenPadCheck.class.getName());
final Checker c = createChecker(checkConfig);
final String fname = getPath("InputWhitespace.java");
final String[] expected = {
"58:12: '(' is followed by whitespace.",
"58:36: ')' is preceeded with whitespace.",
"74:13: '(' is followed by whitespace.",
"74:18: ')' is preceeded with whitespace.",
};
verify(c, fname, expected);
}
public void testSpace()
throws Exception
{
final CheckConfiguration checkConfig = new CheckConfiguration();
checkConfig.setClassname(ParenPadCheck.class.getName());
checkConfig.addProperty("option", PadOption.SPACE.toString());
final Checker c = createChecker(checkConfig);
final String fname = getPath("InputWhitespace.java");
final String[] expected = {
"29:20: '(' is not followed by whitespace.",
"29:23: ')' is not preceeded with whitespace.",
"37:22: '(' is not followed by whitespace.",
"37:26: ')' is not preceeded with whitespace.",
"41:15: '(' is not followed by whitespace.",
"41:33: ')' is not preceeded with whitespace.",
"76:20: '(' is not followed by whitespace.",
"76:21: ')' is not preceeded with whitespace.",
"87:21: '(' is not followed by whitespace.",
"87:27: ')' is not preceeded with whitespace.",
"88:14: '(' is not followed by whitespace.",
"88:20: ')' is not preceeded with whitespace.",
"89:14: '(' is not followed by whitespace.",
"89:20: ')' is not preceeded with whitespace.",
"90:14: '(' is not followed by whitespace.",
"90:20: ')' is not preceeded with whitespace.",
"97:22: '(' is not followed by whitespace.",
"97:28: ')' is not preceeded with whitespace.",
"98:14: '(' is not followed by whitespace.",
"98:18: ')' is not preceeded with whitespace.",
"150:28: '(' is not followed by whitespace.",
"150:32: ')' is not preceeded with whitespace.",
"153:16: '(' is not followed by whitespace.",
"153:20: ')' is not preceeded with whitespace.",
};
verify(c, fname, expected);
}
}