1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.checks.whitespace;
21
22 import java.util.Arrays;
23 import java.util.Locale;
24
25 import com.puppycrawl.tools.checkstyle.StatelessCheck;
26 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
27 import com.puppycrawl.tools.checkstyle.api.DetailAST;
28 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
29 import com.puppycrawl.tools.checkstyle.utils.CodePointUtil;
30 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 @StatelessCheck
71 public class SeparatorWrapCheck
72 extends AbstractCheck {
73
74
75
76
77
78 public static final String MSG_LINE_PREVIOUS = "line.previous";
79
80
81
82
83
84 public static final String MSG_LINE_NEW = "line.new";
85
86
87 private WrapOption option = WrapOption.EOL;
88
89
90
91
92
93
94
95
96 public void setOption(String optionStr) {
97 option = WrapOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH));
98 }
99
100 @Override
101 public int[] getDefaultTokens() {
102 return new int[] {
103 TokenTypes.DOT,
104 TokenTypes.COMMA,
105 };
106 }
107
108 @Override
109 public int[] getAcceptableTokens() {
110 return new int[] {
111 TokenTypes.DOT,
112 TokenTypes.COMMA,
113 TokenTypes.SEMI,
114 TokenTypes.ELLIPSIS,
115 TokenTypes.AT,
116 TokenTypes.LPAREN,
117 TokenTypes.RPAREN,
118 TokenTypes.ARRAY_DECLARATOR,
119 TokenTypes.RBRACK,
120 TokenTypes.METHOD_REF,
121 };
122 }
123
124 @Override
125 public int[] getRequiredTokens() {
126 return CommonUtil.EMPTY_INT_ARRAY;
127 }
128
129 @Override
130 public void visitToken(DetailAST ast) {
131 final String text = ast.getText();
132 final int colNo = ast.getColumnNo();
133 final int lineNo = ast.getLineNo();
134 final int[] currentLine = getLineCodePoints(lineNo - 1);
135 final boolean isLineEmptyAfterToken = CodePointUtil.isBlank(
136 Arrays.copyOfRange(currentLine, colNo + text.length(), currentLine.length)
137 );
138 final boolean isLineEmptyBeforeToken = CodePointUtil.isBlank(
139 Arrays.copyOfRange(currentLine, 0, colNo)
140 );
141
142 if (option == WrapOption.NL
143 && isLineEmptyAfterToken) {
144 log(ast, MSG_LINE_NEW, text);
145 }
146 else if (option == WrapOption.EOL
147 && isLineEmptyBeforeToken) {
148 log(ast, MSG_LINE_PREVIOUS, text);
149 }
150 }
151
152 }