3232
3333@ Rule (
3434 key = LineLengthCheck .CHECK_KEY ,
35- priority = Priority .MINOR ,
3635 name = "Lines should not be too long" ,
36+ priority = Priority .MINOR ,
3737 tags = Tags .CONVENTION
3838)
3939@ SqaleConstantRemediation ("1min" )
@@ -42,6 +42,8 @@ public class LineLengthCheck extends SquidCheck<Grammar> implements AstAndTokenV
4242 public static final String CHECK_KEY = "LineLength" ;
4343 private static final int DEFAULT_MAXIMUM_LINE_LENGTH = 120 ;
4444
45+ private Token previousToken ;
46+
4547 @ RuleProperty (
4648 key = "maximumLineLength" ,
4749 defaultValue = "" + DEFAULT_MAXIMUM_LINE_LENGTH )
@@ -51,8 +53,6 @@ public int getMaximumLineLength() {
5153 return maximumLineLength ;
5254 }
5355
54- private Token previousToken ;
55-
5656 @ Override
5757 public void visitFile (AstNode astNode ) {
5858 previousToken = null ;
@@ -65,26 +65,28 @@ public void leaveFile(AstNode astNode) {
6565
6666 @ Override
6767 public void visitToken (Token token ) {
68- if (!token .isGeneratedCode ()) {
69- if (previousToken != null && previousToken .getLine () != token .getLine ()) {
70- // Note that AbstractLineLengthCheck doesn't support tokens which span multiple lines - see SONARPLUGINS-2025
71- String [] lines = previousToken .getValue ().split ("\r ?\n |\r " , -1 );
72- int length = previousToken .getColumn ();
73- for (int line = 0 ; line < lines .length ; line ++) {
74- length += lines [line ].length ();
75- if (length > getMaximumLineLength ()) {
76- // Note that method from AbstractLineLengthCheck generates other message - see SONARPLUGINS-1809
77- getContext ().createLineViolation (this ,
78- "The line contains {0,number,integer} characters which is greater than {1,number,integer} authorized." ,
79- previousToken .getLine (),
80- length ,
81- getMaximumLineLength ());
82- }
83- length = 0 ;
68+ if (token .isGeneratedCode ()) {
69+ return ;
70+ }
71+
72+ if (previousToken != null && previousToken .getLine () != token .getLine ()) {
73+ // Note that AbstractLineLengthCheck doesn't support tokens which span multiple lines - see SONARPLUGINS-2025
74+ String [] lines = previousToken .getValue ().split ("\r ?\n |\r " , -1 );
75+ int length = previousToken .getColumn ();
76+ for (String line : lines ) {
77+ length += line .length ();
78+ if (length > getMaximumLineLength ()) {
79+ // Note that method from AbstractLineLengthCheck generates other message - see SONARPLUGINS-1809
80+ getContext ().createLineViolation (this ,
81+ "The line contains {0,number,integer} characters which is greater than {1,number,integer} authorized." ,
82+ previousToken .getLine (),
83+ length ,
84+ getMaximumLineLength ());
8485 }
86+ length = 0 ;
8587 }
86- previousToken = token ;
8788 }
89+ previousToken = token ;
8890 }
8991
9092}
0 commit comments