@Override public Object visit(ASTCompilationUnit cUnit, Object data) { for (Comment comment : cUnit.getComments()) { if (hasTooManyLines(comment)) { addViolationWithMessage( data, cUnit, this.getMessage() + ": Too many lines", comment.getBeginLine(), comment.getEndLine()); } List<Integer> lineNumbers = overLengthLineIndicesIn(comment); if (lineNumbers.isEmpty()) { continue; } for (Integer lineNum : lineNumbers) { addViolationWithMessage( data, cUnit, this.getMessage() + ": Line too long", lineNum, lineNum); } } return super.visit(cUnit, data); }
private List<Integer> overLengthLineIndicesIn(Comment comment) { int maxLength = getProperty(MAX_LINE_LENGTH); List<Integer> indicies = new ArrayList<>(); String[] lines = comment.getImage().split(CR); int offset = comment.getBeginLine(); for (int i = 0; i < lines.length; i++) { String cleaned = withoutCommentMarkup(lines[i]); if (cleaned.length() > maxLength) { indicies.add(i + offset); } } return indicies; }
private boolean hasTooManyLines(Comment comment) { String[] lines = comment.getImage().split(CR); int start = 0; // start from top for (; start < lines.length; start++) { if (hasRealText(lines[start])) { break; } } int end = lines.length - 1; // go up from bottom for (; end > 0; end--) { if (hasRealText(lines[end])) { break; } } int lineCount = end - start + 1; return lineCount > getProperty(MAX_LINES); }