1
ļ»æ// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
3
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4
// software and associated documentation files (the "Software"), to deal in the Software
5
// without restriction, including without limitation the rights to use, copy, modify, merge,
6
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7
// to whom the Software is furnished to do so, subject to the following conditions:
9
// The above copyright notice and this permission notice shall be included in all copies or
10
// substantial portions of the Software.
12
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17
// DEALINGS IN THE SOFTWARE.
22
namespace ICSharpCode.NRefactory.CSharp
25
/// Writes C# code into a TextWriter.
27
public class TextWriterOutputFormatter : IOutputFormatter
29
readonly TextWriter textWriter;
31
bool needsIndent = true;
32
bool isAtStartOfLine = true;
34
public int Indentation {
36
return this.indentation;
39
this.indentation = value;
43
public string IndentationString { get; set; }
45
public TextWriterOutputFormatter(TextWriter textWriter)
47
if (textWriter == null)
48
throw new ArgumentNullException("textWriter");
49
this.textWriter = textWriter;
50
this.IndentationString = "\t";
53
public void WriteIdentifier(string ident)
56
textWriter.Write(ident);
57
isAtStartOfLine = false;
60
public void WriteKeyword(string keyword)
63
textWriter.Write(keyword);
64
isAtStartOfLine = false;
67
public void WriteToken(string token)
70
textWriter.Write(token);
71
isAtStartOfLine = false;
77
textWriter.Write(' ');
80
public void OpenBrace(BraceStyle style)
83
case BraceStyle.DoNotChange:
84
case BraceStyle.EndOfLine:
85
case BraceStyle.BannerStyle:
88
textWriter.Write(' ');
89
textWriter.Write('{');
91
case BraceStyle.EndOfLineWithoutSpace:
93
textWriter.Write('{');
95
case BraceStyle.NextLine:
99
textWriter.Write('{');
102
case BraceStyle.NextLineShifted:
106
textWriter.Write('{');
109
case BraceStyle.NextLineShifted2:
113
textWriter.Write('{');
116
throw new ArgumentOutOfRangeException ();
122
public void CloseBrace(BraceStyle style)
125
case BraceStyle.DoNotChange:
126
case BraceStyle.EndOfLine:
127
case BraceStyle.EndOfLineWithoutSpace:
128
case BraceStyle.NextLine:
131
textWriter.Write('}');
132
isAtStartOfLine = false;
134
case BraceStyle.BannerStyle:
135
case BraceStyle.NextLineShifted:
137
textWriter.Write('}');
138
isAtStartOfLine = false;
141
case BraceStyle.NextLineShifted2:
144
textWriter.Write('}');
145
isAtStartOfLine = false;
149
throw new ArgumentOutOfRangeException ();
153
protected void WriteIndentation()
157
for (int i = 0; i < indentation; i++) {
158
textWriter.Write(this.IndentationString);
163
public void NewLine()
165
textWriter.WriteLine();
167
isAtStartOfLine = true;
175
public void Unindent()
180
public void WriteComment(CommentType commentType, string content)
183
switch (commentType) {
184
case CommentType.SingleLine:
185
textWriter.Write("//");
186
textWriter.WriteLine(content);
188
isAtStartOfLine = true;
190
case CommentType.MultiLine:
191
textWriter.Write("/*");
192
textWriter.Write(content);
193
textWriter.Write("*/");
194
isAtStartOfLine = false;
196
case CommentType.Documentation:
197
textWriter.Write("///");
198
textWriter.WriteLine(content);
200
isAtStartOfLine = true;
203
textWriter.Write(content);
208
public void WritePreProcessorDirective(PreProcessorDirectiveType type, string argument)
210
// pre-processor directive must start on its own line
211
if (!isAtStartOfLine)
214
textWriter.Write('#');
215
textWriter.Write(type.ToString().ToLowerInvariant());
216
if (!string.IsNullOrEmpty(argument)) {
217
textWriter.Write(' ');
218
textWriter.Write(argument);
223
public virtual void StartNode(AstNode node)
225
// Write out the indentation, so that overrides of this method
226
// can rely use the current output length to identify the position of the node
231
public virtual void EndNode(AstNode node)