Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7f715f5e28f461470494d8d75777b450c0465304 (plain) (blame)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*******************************************************************************
 * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik  
 * Rapperswil, University of applied sciences and others
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0 
 * which accompanies this distribution, and is available at 
 * http://www.eclipse.org/legal/epl-v10.html  
 *  
 * Contributors: 
 *     Institute for Software - initial API and implementation
 *     Sergey Prigogin (Google)
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.astwriter;

/**
 * This class is responsible for the string concatenation and the management of
 * the indentations.
 * 
 * @since 5.0
 * @author Emanuel Graf IFS
 */
public class Scribe {
	// Indentation is not necessary since the code is going to be formatted anyway.
	// Preserved because some tests depend on it.
	private static final int INDENTATION_SIZE = 4;
	private final String newLine = System.getProperty("line.separator"); //$NON-NLS-1$
	private StringBuilder buffer = new StringBuilder();
	private int indentationLevel = 0;
	private boolean isAtBeginningOfLine = true;

	private boolean skipLineBreaks;
	private boolean skipSemicolons;

	public String getLineSeparator() {
		return newLine;
	}

	public void newLine() {
		if (!skipLineBreaks) {
			isAtBeginningOfLine = true;
			buffer.append(newLine);
		}
	}

	public boolean isAtBeginningOfLine() {
		return isAtBeginningOfLine;
	}

	private void indent() {
		printSpaces(indentationLevel * INDENTATION_SIZE);
	}

	private void indentIfNewLine() {
		if (isAtBeginningOfLine) {
			isAtBeginningOfLine = false;
			indent();
		}
	}

	public void print(String code) {
		indentIfNewLine();
		buffer.append(code);
	}

	public void println(String code) {
		print(code);
		newLine();
	}

	public void print(String code, String code2) {
		print(code);
		buffer.append(code2);
	}

	public void println(String code, String code2) {
		print(code, code2);
		newLine();
	}

	public void println(String code, char[] code2) {
		print(code);
		buffer.append(code2);
		newLine();
	}

	public void printSpaces(int number) {
		indentIfNewLine();
		for (int i = 0; i < number; ++i) {
			printSpace();
		}
	}

	public void noSemicolon() {
		skipSemicolons = true;
	}

	public void printSemicolon() {
		if (!skipSemicolons) {
			indentIfNewLine();
			buffer.append(';');
		} else {
			skipSemicolons = false;
		}
	}

	@Override
	public String toString() {
		return buffer.toString();
	}

	public void print(char code) {
		indentIfNewLine();
		buffer.append(code);
	}

	public void print(char[] code) {
		indentIfNewLine();
		buffer.append(code);
	}

	public void println(char[] code) {
		print(code);
		newLine();
	}

	public void printStringSpace(String code) {
		print(code);
		printSpace();
	}

	/**
	 * Prints a { to the buffer an increases the indentation level.
	 */
	public void printLBrace() {
		print('{');
		++indentationLevel;
	}

	/**
	 * Prints a } to the buffer an decrease the indentation level.
	 */
	public void printRBrace() {
		--indentationLevel;
		print('}');
	}

	public void incrementIndentationLevel() {
		++indentationLevel;
	}

	public void decrementIndentationLevel() {
		if (indentationLevel > 0) {
			--indentationLevel;
		}
	}

	protected void noNewLines() {
		skipLineBreaks = true;
	}

	protected void newLines() {
		skipLineBreaks = false;
	}

	public void newLine(int i) {
		while (i > 0) {
			newLine();
			--i;
		}
	}

	public void printSpace() {
		buffer.append(' ');
	}

	public void cleanCache() {
		buffer = new StringBuilder();
	}
}

Back to the top