1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2026 the original author or authors.
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.indentation;
21
22 import java.util.function.IntFunction;
23
24 import com.puppycrawl.tools.checkstyle.api.DetailAST;
25
26 /**
27 * Immutable view of everything an indentation handler needs from the surrounding
28 * {@code IndentationCheck}, so handlers do not hold a direct reference back to
29 * the check.
30 */
31 public final class IndentationContext {
32
33 /** Message key for an indentation error. */
34 /* package */ static final String MSG_ERROR = "indentation.error";
35 /** Message key for a multi-level indentation error. */
36 /* package */ static final String MSG_ERROR_MULTI = "indentation.error.multi";
37 /** Message key for a child indentation error. */
38 /* package */ static final String MSG_CHILD_ERROR = "indentation.child.error";
39 /** Message key for a multi-level child indentation error. */
40 /* package */ static final String MSG_CHILD_ERROR_MULTI = "indentation.child.error.multi";
41
42 /** Basic offset property value. */
43 private final int basicOffset;
44 /** Brace adjustment property value. */
45 private final int braceAdjustment;
46 /** Case indent property value. */
47 private final int caseIndent;
48 /** Throws indent property value. */
49 private final int throwsIndent;
50 /** Array initialization indent property value. */
51 private final int arrayInitIndent;
52 /** Line wrapping indentation property value. */
53 private final int lineWrappingIndentation;
54 /** Force-strict-condition property value. */
55 private final boolean forceStrictCondition;
56 /** Tab width used for expanded-tabs column calculations. */
57 private final int indentationTabWidth;
58 /** Provides a source line by zero-based index. */
59 private final IntFunction<String> lineProvider;
60 /** Factory that creates handlers for AST nodes. */
61 private final HandlerFactory handlerFactory;
62 /** Shared line-wrapping handler used by expression handlers. */
63 private final LineWrappingHandler lineWrappingHandler;
64 /** Sink for indentation violations. */
65 private final IndentationLogger logger;
66
67 /**
68 * Construct a context with all values handlers need for one file.
69 *
70 * @param basicOffset basic offset property
71 * @param braceAdjustment brace adjustment property
72 * @param caseIndent case indent property
73 * @param throwsIndent throws indent property
74 * @param arrayInitIndent array-init indent property
75 * @param lineWrappingIndentation line-wrapping indent property
76 * @param forceStrictCondition force-strict-condition property
77 * @param indentationTabWidth tab width for expanded-tabs math
78 * @param lineProvider zero-based source line accessor
79 * @param handlerFactory factory for AST handlers
80 * @param lineWrappingHandler shared line-wrapping handler
81 * @param logger sink for violations
82 */
83 // -@cs[ParameterNumber] bundling ctor for a value-object context
84 /* package */ IndentationContext(int basicOffset, int braceAdjustment, int caseIndent,
85 int throwsIndent,
86 int arrayInitIndent, int lineWrappingIndentation,
87 boolean forceStrictCondition, int indentationTabWidth,
88 IntFunction<String> lineProvider, HandlerFactory handlerFactory,
89 LineWrappingHandler lineWrappingHandler, IndentationLogger logger) {
90 this.basicOffset = basicOffset;
91 this.braceAdjustment = braceAdjustment;
92 this.caseIndent = caseIndent;
93 this.throwsIndent = throwsIndent;
94 this.arrayInitIndent = arrayInitIndent;
95 this.lineWrappingIndentation = lineWrappingIndentation;
96 this.forceStrictCondition = forceStrictCondition;
97 this.indentationTabWidth = indentationTabWidth;
98 this.lineProvider = lineProvider;
99 this.handlerFactory = handlerFactory;
100 this.lineWrappingHandler = lineWrappingHandler;
101 this.logger = logger;
102 }
103
104 /**
105 * Get the basic offset property.
106 *
107 * @return basic offset
108 */
109 /* package */ int getBasicOffset() {
110 return basicOffset;
111 }
112
113 /**
114 * Get the brace adjustment property.
115 *
116 * @return brace adjustment
117 */
118 /* package */ int getBraceAdjustment() {
119 return braceAdjustment;
120 }
121
122 /**
123 * Get the case indent property.
124 *
125 * @return case indent
126 */
127 /* package */ int getCaseIndent() {
128 return caseIndent;
129 }
130
131 /**
132 * Get the throws indent property.
133 *
134 * @return throws indent
135 */
136 /* package */ int getThrowsIndent() {
137 return throwsIndent;
138 }
139
140 /**
141 * Get the array-init indent property.
142 *
143 * @return array-init indent
144 */
145 /* package */ int getArrayInitIndent() {
146 return arrayInitIndent;
147 }
148
149 /**
150 * Get the line-wrapping indentation property.
151 *
152 * @return line-wrapping indent
153 */
154 /* package */ int getLineWrappingIndentation() {
155 return lineWrappingIndentation;
156 }
157
158 /**
159 * Get the force-strict-condition property.
160 *
161 * @return true when strict line-wrap indent is enforced
162 */
163 /* package */ boolean isForceStrictCondition() {
164 return forceStrictCondition;
165 }
166
167 /**
168 * Get the tab width used for expanded-tabs column math.
169 *
170 * @return tab width
171 */
172 /* package */ int getIndentationTabWidth() {
173 return indentationTabWidth;
174 }
175
176 /**
177 * Get a source line by zero-based index.
178 *
179 * @param lineIndex zero-based line index
180 * @return the source line
181 */
182 /* package */ String getLine(int lineIndex) {
183 return lineProvider.apply(lineIndex);
184 }
185
186 /**
187 * Get the shared handler factory.
188 *
189 * @return handler factory
190 */
191 /* package */ HandlerFactory getHandlerFactory() {
192 return handlerFactory;
193 }
194
195 /**
196 * Get the shared line-wrapping handler.
197 *
198 * @return line-wrapping handler
199 */
200 /* package */ LineWrappingHandler getLineWrappingHandler() {
201 return lineWrappingHandler;
202 }
203
204 /**
205 * Report an indentation violation for the given AST.
206 *
207 * @param ast the AST that caused the violation
208 * @param messageKey the message key
209 * @param args message arguments
210 */
211 /* package */ void indentationLog(DetailAST ast, String messageKey, Object... args) {
212 logger.log(ast, messageKey, args);
213 }
214
215 }