2
// <copyright see="prj:///doc/copyright.txt"/>
3
// <license see="prj:///doc/license.txt"/>
4
// <owner name="Mike KrĆ¼ger" email="mike@icsharpcode.net"/>
5
// <version>$Revision: 4482 $</version>
9
using System.Collections;
12
namespace ICSharpCode.OldNRefactory.PrettyPrinter
15
/// Base class of output formatters.
17
public abstract class AbstractOutputFormatter : IOutputFormatter
19
StringBuilder text = new StringBuilder();
21
int indentationLevel = 0;
23
bool doNewLine = true;
24
AbstractPrettyPrintOptions prettyPrintOptions;
26
public bool IsInMemberBody { get; set; }
28
public int IndentationLevel {
30
return indentationLevel;
33
indentationLevel = value;
39
return text.ToString();
43
public int TextLength {
50
public bool DoIndent {
59
public bool DoNewLine {
68
protected AbstractOutputFormatter(AbstractPrettyPrintOptions prettyPrintOptions)
70
this.prettyPrintOptions = prettyPrintOptions;
73
internal bool isIndented = false;
78
while (indent < prettyPrintOptions.IndentSize * indentationLevel) {
79
char ch = prettyPrintOptions.IndentationChar;
80
if (ch == '\t' && indent + prettyPrintOptions.TabSize > prettyPrintOptions.IndentSize * indentationLevel) {
85
indent += prettyPrintOptions.TabSize;
106
internal int lastLineStart = 0;
107
internal int lineBeforeLastStart = 0;
109
public bool LastCharacterIsNewLine {
111
return text.Length == lastLineStart;
115
public bool LastCharacterIsWhiteSpace {
117
return text.Length == 0 || char.IsWhiteSpace(text[text.Length - 1]);
121
public virtual void NewLine()
124
if (!LastCharacterIsNewLine) {
125
lineBeforeLastStart = lastLineStart;
127
text.Append(prettyPrintOptions.EolMarker);
128
lastLineStart = text.Length;
133
public virtual void EndFile()
137
protected void WriteLineInPreviousLine(string txt, bool forceWriteInPreviousBlock)
139
WriteInPreviousLine(txt + prettyPrintOptions.EolMarker, forceWriteInPreviousBlock);
141
protected void WriteLineInPreviousLine(string txt, bool forceWriteInPreviousBlock, bool indent)
143
WriteInPreviousLine(txt + prettyPrintOptions.EolMarker, forceWriteInPreviousBlock, indent);
146
protected void WriteInPreviousLine(string txt, bool forceWriteInPreviousBlock)
148
WriteInPreviousLine(txt, forceWriteInPreviousBlock, true);
150
protected void WriteInPreviousLine(string txt, bool forceWriteInPreviousBlock, bool indent)
152
if (txt.Length == 0) return;
154
bool lastCharacterWasNewLine = LastCharacterIsNewLine;
155
if (lastCharacterWasNewLine) {
156
if (forceWriteInPreviousBlock == false) {
157
if (indent && txt != prettyPrintOptions.EolMarker) Indent();
159
lineBeforeLastStart = lastLineStart;
160
lastLineStart = text.Length;
163
lastLineStart = lineBeforeLastStart;
165
string lastLine = text.ToString(lastLineStart, text.Length - lastLineStart);
166
text.Remove(lastLineStart, text.Length - lastLineStart);
168
if (forceWriteInPreviousBlock) ++indentationLevel;
170
if (forceWriteInPreviousBlock) --indentationLevel;
173
lineBeforeLastStart = lastLineStart;
174
lastLineStart = text.Length;
175
text.Append(lastLine);
176
if (lastCharacterWasNewLine) {
177
lineBeforeLastStart = lastLineStart;
178
lastLineStart = text.Length;
184
/// Prints a text that cannot be inserted before using WriteInPreviousLine
185
/// into the current line
187
protected void PrintSpecialText(string specialText)
189
lineBeforeLastStart = text.Length;
190
text.Append(specialText);
191
lastLineStart = text.Length;
195
public void PrintTokenList(ArrayList tokenList)
197
foreach (int token in tokenList) {
203
public abstract void PrintComment(Comment comment, bool forceWriteInPreviousBlock);
205
public virtual void PrintPreprocessingDirective(PreprocessingDirective directive, bool forceWriteInPreviousBlock)
207
if (!directive.Expression.IsNull) {
208
CSharpOutputVisitor visitor = new CSharpOutputVisitor();
209
directive.Expression.AcceptVisitor(visitor, null);
210
WriteLineInPreviousLine(directive.Cmd + " " + visitor.Text, forceWriteInPreviousBlock);
211
} else if (string.IsNullOrEmpty(directive.Arg))
212
WriteLineInPreviousLine(directive.Cmd, forceWriteInPreviousBlock);
214
WriteLineInPreviousLine(directive.Cmd + " " + directive.Arg, forceWriteInPreviousBlock);
217
public void PrintBlankLine(bool forceWriteInPreviousBlock)
219
WriteInPreviousLine(prettyPrintOptions.EolMarker, forceWriteInPreviousBlock);
222
public abstract void PrintToken(int token);
224
public void PrintText(string text)
226
this.text.Append(text);
230
public abstract void PrintIdentifier(string identifier);