forked from runtimeverification/k
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIndenter.java
More file actions
62 lines (51 loc) · 1.36 KB
/
Indenter.java
File metadata and controls
62 lines (51 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
// Copyright (c) 2016-2019 K Team. All Rights Reserved.
package org.kframework.unparser;
public class Indenter implements Appendable {
private final int indentSize;
private int indentationLevel = 0;
private boolean atNewLine = true;
private final StringBuilder sb = new StringBuilder();
public Indenter(int indentSize) {
this.indentSize = indentSize;
}
public Indenter append(CharSequence str) {
printIndent();
sb.append(str);
return this;
}
public Indenter append(CharSequence str, int start, int end) {
printIndent();
sb.append(str, start, end);
return this;
}
private void printIndent() {
if (atNewLine) {
for (int i = 0; i < indentSize * indentationLevel; i++) {
sb.append(" ");
}
atNewLine = false;
}
}
public Indenter indent() {
indentationLevel++;
return this;
}
public Indenter dedent() {
indentationLevel--;
return this;
}
public Indenter newline() {
sb.append(System.getProperty("line.separator"));
atNewLine = true;
return this;
}
@Override
public String toString() {
return sb.toString();
}
public Indenter append(char c) {
printIndent();
sb.append(c);
return this;
}
}