1 ////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code for adherence to a set of rules.
3 // Copyright (C) 2001-2014 Oliver Burn
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ////////////////////////////////////////////////////////////////////////////////
19
20 package com.puppycrawl.tools.checkstyle.checks.whitespace;
21
22 import com.puppycrawl.tools.checkstyle.api.DetailAST;
23 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
24 import com.puppycrawl.tools.checkstyle.checks.AbstractOptionCheck;
25
26 /**
27 * <p>
28 * Checks line wrapping with separators.
29 * The policy to verify is specified using the {@link WrapOption} class
30 * and defaults to {@link WrapOption#EOL}.
31 * </p>
32 * <p> By default the check will check the following separators:
33 * {@link TokenTypes#DOT DOT},
34 * {@link TokenTypes#COMMA COMMA},
35 * Other acceptable tokens are
36 * {@link TokenTypes#SEMI SEMI},
37 * {@link TokenTypes#ELLIPSIS ELLIPSIS},
38 * {@link TokenTypes#AT AT},
39 * {@link TokenTypes#LPAREN LPAREN},
40 * {@link TokenTypes#RPAREN RPAREN},
41 * {@link TokenTypes#ARRAY_DECLARATOR ARRAY_DECLARATOR},
42 * {@link TokenTypes#RBRACK RBRACK},
43 * </p>
44 * <p>
45 * Code example for comma and dot at the new line:
46 * </p>
47 * <pre>
48 * s
49 * .isEmpty();
50 * foo(i
51 * ,s);
52 * </pre>
53 * <p>
54 * An example of how to configure the check is:
55 * </p>
56 * <pre>
57 * <module name="SeparatorWrap"/>
58 * </pre>
59 * <p>
60 * Code example for comma and dot at the previous line:
61 * </p>
62 * <pre>
63 * s.
64 * isEmpty();
65 * foo(i,
66 * s);
67 * </pre>
68 * <p> An example of how to configure the check for comma at the
69 * new line is:
70 * </p>
71 * <pre>
72 * <module name="SeparatorWrap">
73 * <property name="tokens" value="COMMA"/>
74 * <property name="option" value="nl"/>
75 * </module>
76 * </pre>
77 *
78 * @author maxvetrenko
79 */
80 public class SeparatorWrapCheck
81 extends AbstractOptionCheck<WrapOption>
82 {
83 /**
84 * Sets the comma wrap option to end of the line.
85 */
86 public SeparatorWrapCheck()
87 {
88 super(WrapOption.EOL, WrapOption.class);
89 }
90
91 @Override
92 public int[] getDefaultTokens()
93 {
94 return new int[] {
95 TokenTypes.DOT,
96 TokenTypes.COMMA,
97 };
98 }
99
100 @Override
101 public int[] getAcceptableTokens()
102 {
103 return new int[] {
104 TokenTypes.DOT,
105 TokenTypes.COMMA,
106 TokenTypes.SEMI,
107 TokenTypes.ELLIPSIS,
108 TokenTypes.AT,
109 TokenTypes.LPAREN,
110 TokenTypes.RPAREN,
111 TokenTypes.ARRAY_DECLARATOR,
112 TokenTypes.RBRACK,
113 };
114 }
115
116 @Override
117 public void visitToken(DetailAST ast)
118 {
119 // TODO: It is a copy/paste from OperatorWrapCheck.
120 //It should be fixed in another issue
121 final String text = ast.getText();
122 final int colNo = ast.getColumnNo();
123 final int lineNo = ast.getLineNo();
124 final String currentLine = getLines()[lineNo - 1];
125 final String substringAfterToken =
126 currentLine.substring(colNo + text.length()).trim();
127 final String substringBeforeToken =
128 currentLine.substring(0, colNo).trim();
129 final WrapOption wSp = getAbstractOption();
130
131 if (wSp == WrapOption.EOL
132 && (substringBeforeToken.length() == 0))
133 {
134 log(lineNo, colNo, "line.previous", text);
135 }
136 else if (wSp == WrapOption.NL
137 && substringAfterToken.length() == 0)
138 {
139 log(lineNo, colNo, "line.new", text);
140 }
141 }
142 }