Updates referencesource to .NET 4.7
[mono-project.git] / mcs / class / referencesource / System / compmod / system / codedom / compiler / IndentTextWriter.cs
blobbe86a9c176bd40fcf4beff7c9b9ba698f4c2dc00
1 //------------------------------------------------------------------------------
2 // <copyright file="IndentTextWriter.cs" company="Microsoft">
3 //
4 // <OWNER>Microsoft</OWNER>
5 // Copyright (c) Microsoft Corporation. All rights reserved.
6 // </copyright>
7 //------------------------------------------------------------------------------
9 namespace System.CodeDom.Compiler {
11 using System.Diagnostics;
12 using System;
13 using System.IO;
14 using System.Text;
15 using System.Security.Permissions;
16 using System.Globalization;
18 /// <devdoc>
19 /// <para>Provides a text writer that can indent new lines by a tabString token.</para>
20 /// </devdoc>
21 public class IndentedTextWriter : TextWriter {
22 private TextWriter writer;
23 private int indentLevel;
24 private bool tabsPending;
25 private string tabString;
27 /// <devdoc>
28 /// <para>[To be supplied.]</para>
29 /// </devdoc>
30 public const string DefaultTabString = " ";
32 /// <devdoc>
33 /// <para>
34 /// Initializes a new instance of <see cref='System.CodeDom.Compiler.IndentedTextWriter'/> using the specified
35 /// text writer and default tab string.
36 /// </para>
37 /// </devdoc>
38 public IndentedTextWriter(TextWriter writer) : this(writer, DefaultTabString) {
41 /// <devdoc>
42 /// <para>
43 /// Initializes a new instance of <see cref='System.CodeDom.Compiler.IndentedTextWriter'/> using the specified
44 /// text writer and tab string.
45 /// </para>
46 /// </devdoc>
47 public IndentedTextWriter(TextWriter writer, string tabString): base(CultureInfo.InvariantCulture) {
48 this.writer = writer;
49 this.tabString = tabString;
50 indentLevel = 0;
51 tabsPending = false;
54 /// <devdoc>
55 /// <para>[To be supplied.]</para>
56 /// </devdoc>
57 public override Encoding Encoding {
58 get {
59 return writer.Encoding;
63 /// <devdoc>
64 /// <para>
65 /// Gets or sets the new line character to use.
66 /// </para>
67 /// </devdoc>
68 public override string NewLine {
69 get {
70 return writer.NewLine;
73 set {
74 writer.NewLine = value;
78 /// <devdoc>
79 /// <para>
80 /// Gets or sets the number of spaces to indent.
81 /// </para>
82 /// </devdoc>
83 public int Indent {
84 get {
85 return indentLevel;
87 set {
88 Debug.Assert(value >= 0, "Bogus Indent... probably caused by mismatched Indent++ and Indent--");
89 if (value < 0) {
90 value = 0;
92 indentLevel = value;
96 /// <devdoc>
97 /// <para>
98 /// Gets or sets the TextWriter to use.
99 /// </para>
100 /// </devdoc>
101 public TextWriter InnerWriter {
102 get {
103 return writer;
107 internal string TabString {
108 get { return tabString; }
111 /// <devdoc>
112 /// <para>
113 /// Closes the document being written to.
114 /// </para>
115 /// </devdoc>
116 public override void Close() {
117 writer.Close();
120 /// <devdoc>
121 /// <para>[To be supplied.]</para>
122 /// </devdoc>
123 public override void Flush() {
124 writer.Flush();
127 /// <devdoc>
128 /// <para>[To be supplied.]</para>
129 /// </devdoc>
130 protected virtual void OutputTabs() {
131 if (tabsPending) {
132 for (int i=0; i < indentLevel; i++) {
133 writer.Write(tabString);
135 tabsPending = false;
139 /// <devdoc>
140 /// <para>
141 /// Writes a string
142 /// to the text stream.
143 /// </para>
144 /// </devdoc>
145 public override void Write(string s) {
146 OutputTabs();
147 writer.Write(s);
150 /// <devdoc>
151 /// <para>
152 /// Writes the text representation of a Boolean value to the text stream.
153 /// </para>
154 /// </devdoc>
155 public override void Write(bool value) {
156 OutputTabs();
157 writer.Write(value);
160 /// <devdoc>
161 /// <para>
162 /// Writes a character to the text stream.
163 /// </para>
164 /// </devdoc>
165 public override void Write(char value) {
166 OutputTabs();
167 writer.Write(value);
170 /// <devdoc>
171 /// <para>
172 /// Writes a
173 /// character array to the text stream.
174 /// </para>
175 /// </devdoc>
176 public override void Write(char[] buffer) {
177 OutputTabs();
178 writer.Write(buffer);
181 /// <devdoc>
182 /// <para>
183 /// Writes a subarray
184 /// of characters to the text stream.
185 /// </para>
186 /// </devdoc>
187 public override void Write(char[] buffer, int index, int count) {
188 OutputTabs();
189 writer.Write(buffer, index, count);
192 /// <devdoc>
193 /// <para>
194 /// Writes the text representation of a Double to the text stream.
195 /// </para>
196 /// </devdoc>
197 public override void Write(double value) {
198 OutputTabs();
199 writer.Write(value);
202 /// <devdoc>
203 /// <para>
204 /// Writes the text representation of
205 /// a Single to the text
206 /// stream.
207 /// </para>
208 /// </devdoc>
209 public override void Write(float value) {
210 OutputTabs();
211 writer.Write(value);
214 /// <devdoc>
215 /// <para>
216 /// Writes the text representation of an integer to the text stream.
217 /// </para>
218 /// </devdoc>
219 public override void Write(int value) {
220 OutputTabs();
221 writer.Write(value);
224 /// <devdoc>
225 /// <para>
226 /// Writes the text representation of an 8-byte integer to the text stream.
227 /// </para>
228 /// </devdoc>
229 public override void Write(long value) {
230 OutputTabs();
231 writer.Write(value);
234 /// <devdoc>
235 /// <para>
236 /// Writes the text representation of an object
237 /// to the text stream.
238 /// </para>
239 /// </devdoc>
240 public override void Write(object value) {
241 OutputTabs();
242 writer.Write(value);
245 /// <devdoc>
246 /// <para>
247 /// Writes out a formatted string, using the same semantics as specified.
248 /// </para>
249 /// </devdoc>
250 public override void Write(string format, object arg0) {
251 OutputTabs();
252 writer.Write(format, arg0);
255 /// <devdoc>
256 /// <para>
257 /// Writes out a formatted string,
258 /// using the same semantics as specified.
259 /// </para>
260 /// </devdoc>
261 public override void Write(string format, object arg0, object arg1) {
262 OutputTabs();
263 writer.Write(format, arg0, arg1);
266 /// <devdoc>
267 /// <para>
268 /// Writes out a formatted string,
269 /// using the same semantics as specified.
270 /// </para>
271 /// </devdoc>
272 public override void Write(string format, params object[] arg) {
273 OutputTabs();
274 writer.Write(format, arg);
277 /// <devdoc>
278 /// <para>
279 /// Writes the specified
280 /// string to a line without tabs.
281 /// </para>
282 /// </devdoc>
283 public void WriteLineNoTabs(string s) {
284 writer.WriteLine(s);
287 /// <devdoc>
288 /// <para>
289 /// Writes the specified string followed by
290 /// a line terminator to the text stream.
291 /// </para>
292 /// </devdoc>
293 public override void WriteLine(string s) {
294 OutputTabs();
295 writer.WriteLine(s);
296 tabsPending = true;
299 /// <devdoc>
300 /// <para>
301 /// Writes a line terminator.
302 /// </para>
303 /// </devdoc>
304 public override void WriteLine() {
305 OutputTabs();
306 writer.WriteLine();
307 tabsPending = true;
310 /// <devdoc>
311 /// <para>
312 /// Writes the text representation of a Boolean followed by a line terminator to
313 /// the text stream.
314 /// </para>
315 /// </devdoc>
316 public override void WriteLine(bool value) {
317 OutputTabs();
318 writer.WriteLine(value);
319 tabsPending = true;
322 /// <devdoc>
323 /// <para>[To be supplied.]</para>
324 /// </devdoc>
325 public override void WriteLine(char value) {
326 OutputTabs();
327 writer.WriteLine(value);
328 tabsPending = true;
331 /// <devdoc>
332 /// <para>[To be supplied.]</para>
333 /// </devdoc>
334 public override void WriteLine(char[] buffer) {
335 OutputTabs();
336 writer.WriteLine(buffer);
337 tabsPending = true;
340 /// <devdoc>
341 /// <para>[To be supplied.]</para>
342 /// </devdoc>
343 public override void WriteLine(char[] buffer, int index, int count) {
344 OutputTabs();
345 writer.WriteLine(buffer, index, count);
346 tabsPending = true;
349 /// <devdoc>
350 /// <para>[To be supplied.]</para>
351 /// </devdoc>
352 public override void WriteLine(double value) {
353 OutputTabs();
354 writer.WriteLine(value);
355 tabsPending = true;
358 /// <devdoc>
359 /// <para>[To be supplied.]</para>
360 /// </devdoc>
361 public override void WriteLine(float value) {
362 OutputTabs();
363 writer.WriteLine(value);
364 tabsPending = true;
367 /// <devdoc>
368 /// <para>[To be supplied.]</para>
369 /// </devdoc>
370 public override void WriteLine(int value) {
371 OutputTabs();
372 writer.WriteLine(value);
373 tabsPending = true;
376 /// <devdoc>
377 /// <para>[To be supplied.]</para>
378 /// </devdoc>
379 public override void WriteLine(long value) {
380 OutputTabs();
381 writer.WriteLine(value);
382 tabsPending = true;
385 /// <devdoc>
386 /// <para>[To be supplied.]</para>
387 /// </devdoc>
388 public override void WriteLine(object value) {
389 OutputTabs();
390 writer.WriteLine(value);
391 tabsPending = true;
394 /// <devdoc>
395 /// <para>[To be supplied.]</para>
396 /// </devdoc>
397 public override void WriteLine(string format, object arg0) {
398 OutputTabs();
399 writer.WriteLine(format, arg0);
400 tabsPending = true;
403 /// <devdoc>
404 /// <para>[To be supplied.]</para>
405 /// </devdoc>
406 public override void WriteLine(string format, object arg0, object arg1) {
407 OutputTabs();
408 writer.WriteLine(format, arg0, arg1);
409 tabsPending = true;
412 /// <devdoc>
413 /// <para>[To be supplied.]</para>
414 /// </devdoc>
415 public override void WriteLine(string format, params object[] arg) {
416 OutputTabs();
417 writer.WriteLine(format, arg);
418 tabsPending = true;
421 /// <devdoc>
422 /// <para>[To be supplied.]</para>
423 /// </devdoc>
424 [CLSCompliant(false)]
425 public override void WriteLine(UInt32 value) {
426 OutputTabs();
427 writer.WriteLine(value);
428 tabsPending = true;
431 internal void InternalOutputTabs() {
432 for (int i=0; i < indentLevel; i++) {
433 writer.Write(tabString);
438 internal class Indentation {
439 private IndentedTextWriter writer;
440 private int indent;
441 private string s;
443 internal Indentation(IndentedTextWriter writer, int indent) {
444 this.writer = writer;
445 this.indent = indent;
446 s = null;
449 internal string IndentationString {
450 get {
451 if ( s == null) {
452 string tabString = writer.TabString;
453 StringBuilder sb = new StringBuilder(indent * tabString.Length);
454 for( int i = 0; i < indent; i++) {
455 sb.Append(tabString);
457 s = sb.ToString();
459 return s;