summaryrefslogtreecommitdiff
path: root/platform/script-debugger/protocol/protocol-reader/src/org/jetbrains/protocolReader/TextOutput.java
blob: 77d4bd1e275fa0162a23e904deadc4f3182bdafc (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package org.jetbrains.protocolReader;

import org.jetbrains.annotations.Nullable;

import java.util.Arrays;

public class TextOutput {
  public static final char[] EMPTY_CHARS = new char[0];
  private int identLevel;
  private final static int indentGranularity = 2;
  private char[][] indents = {EMPTY_CHARS};
  private boolean justNewLined;
  private final StringBuilder out;

  public TextOutput(StringBuilder out) {
    this.out = out;
  }

  public StringBuilder getOut() {
    return out;
  }

  public TextOutput indentIn() {
    ++identLevel;
    if (identLevel >= indents.length) {
      // Cache a new level of indentation string.
      char[] newIndentLevel = new char[identLevel * indentGranularity];
      Arrays.fill(newIndentLevel, ' ');
      char[][] newIndents = new char[indents.length + 1][];
      System.arraycopy(indents, 0, newIndents, 0, indents.length);
      newIndents[identLevel] = newIndentLevel;
      indents = newIndents;
    }
    return this;
  }

  public TextOutput indentOut() {
    --identLevel;
    return this;
  }

  public TextOutput newLine() {
    out.append('\n');
    justNewLined = true;
    return this;
  }

  public TextOutput append(double value) {
    maybeIndent();
    out.append(value);
    return this;
  }

  public TextOutput append(boolean value) {
    maybeIndent();
    out.append(value);
    return this;
  }

  public TextOutput append(int value) {
    maybeIndent();
    out.append(value);
    return this;
  }

  public TextOutput append(char c) {
    maybeIndent();
    out.append(c);
    return this;
  }

  public void append(char[] s) {
    maybeIndent();
    out.append(s);
  }

  public TextOutput append(CharSequence s) {
    maybeIndent();
    out.append(s);
    return this;
  }

  public TextOutput append(CharSequence s, int start) {
    maybeIndent();
    out.append(s, start, s.length());
    return this;
  }

  public TextOutput openBlock() {
    openBlock(true);
    return this;
  }

  public void openBlock(boolean addNewLine) {
    space().append('{');
    if (addNewLine) {
      newLine();
    }
    indentIn();
  }

  public void closeBlock() {
    indentOut().newLine().append('}');
  }

  public TextOutput comma() {
    return append(',').append(' ');
  }

  public TextOutput space() {
    return append(' ');
  }

  public TextOutput semi() {
    return append(';');
  }

  public TextOutput doc(@Nullable String description) {
    if (description == null) {
      return this;
    }
    return append("/**").newLine().append(" * ").append(description).newLine().append(" */").newLine();
  }

  public TextOutput quote(CharSequence s) {
    return append('"').append(s).append('"');
  }

  public void maybeIndent() {
    if (justNewLined) {
      out.append(indents[identLevel]);
      justNewLined = false;
    }
  }
}