1 | // Copyright (C) 2008 Google Inc. |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package com.google.caja.render; |
16 | |
17 | /** |
18 | * Encapsulates information necessary to decide how to indent a line-break at a |
19 | * point in a stream of tokens. |
20 | */ |
21 | final class Indent { |
22 | final int spaces; |
23 | /** |
24 | * Are we in a () or [] block. |
25 | * Semicolons are not treated as statement separators in that context. |
26 | */ |
27 | final boolean parenthetical; |
28 | /** |
29 | * True iff we are in a statement. |
30 | * E.g. in {@code |
31 | * var foo = x |
32 | * + y; |
33 | * } |
34 | * the <code>+</code> is indented past the beginning of the previous line |
35 | * because we have not yet seen a semicolon. |
36 | */ |
37 | final boolean inStatement; |
38 | |
39 | Indent(int spaces, boolean parenthetical) { |
40 | this(spaces, parenthetical, false); |
41 | } |
42 | |
43 | Indent(int spaces, boolean parenthetical, boolean inStatement) { |
44 | this.spaces = spaces; |
45 | this.parenthetical = parenthetical; |
46 | this.inStatement = inStatement; |
47 | } |
48 | |
49 | int getIndentLevel() { |
50 | return this.spaces + (this.inStatement ? 2 : 0); |
51 | } |
52 | |
53 | Indent withInStatement(boolean inStatement) { |
54 | inStatement = inStatement && !parenthetical; |
55 | if (inStatement == this.inStatement) { return this; } |
56 | return new Indent(this.spaces, this.parenthetical, inStatement); |
57 | } |
58 | |
59 | @Override |
60 | public String toString() { |
61 | return "[Indent sp=" + spaces + ", paren=" + parenthetical + ", ins=" |
62 | + inStatement + "]"; |
63 | } |
64 | } |