1
ļ»æ// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
5
using System.Collections;
8
namespace ICSharpCode.NRefactory.VB.PrettyPrinter
11
/// Base class of output formatters.
13
public abstract class AbstractOutputFormatter : IOutputFormatter
15
StringBuilder text = new StringBuilder();
17
int indentationLevel = 0;
19
bool doNewLine = true;
20
AbstractPrettyPrintOptions prettyPrintOptions;
22
public bool IsInMemberBody { get; set; }
24
public int IndentationLevel {
26
return indentationLevel;
29
indentationLevel = value;
35
return text.ToString();
39
public int TextLength {
46
public bool DoIndent {
55
public bool DoNewLine {
64
protected AbstractOutputFormatter(AbstractPrettyPrintOptions prettyPrintOptions)
66
this.prettyPrintOptions = prettyPrintOptions;
69
internal bool isIndented = false;
74
while (indent < prettyPrintOptions.IndentSize * indentationLevel) {
75
char ch = prettyPrintOptions.IndentationChar;
76
if (ch == '\t' && indent + prettyPrintOptions.TabSize > prettyPrintOptions.IndentSize * indentationLevel) {
81
indent += prettyPrintOptions.TabSize;
102
internal int lastLineStart = 0;
103
internal int lineBeforeLastStart = 0;
105
public bool LastCharacterIsNewLine {
107
return text.Length == lastLineStart;
111
public bool LastCharacterIsWhiteSpace {
113
return text.Length == 0 || char.IsWhiteSpace(text[text.Length - 1]);
117
public virtual void NewLine()
120
if (!LastCharacterIsNewLine) {
121
lineBeforeLastStart = lastLineStart;
124
lastLineStart = text.Length;
129
public virtual void EndFile()
133
protected void WriteLineInPreviousLine(string txt, bool forceWriteInPreviousBlock)
135
WriteInPreviousLine(txt + Environment.NewLine, forceWriteInPreviousBlock);
137
protected void WriteLineInPreviousLine(string txt, bool forceWriteInPreviousBlock, bool indent)
139
WriteInPreviousLine(txt + Environment.NewLine, forceWriteInPreviousBlock, indent);
142
protected void WriteInPreviousLine(string txt, bool forceWriteInPreviousBlock)
144
WriteInPreviousLine(txt, forceWriteInPreviousBlock, true);
146
protected void WriteInPreviousLine(string txt, bool forceWriteInPreviousBlock, bool indent)
148
if (txt.Length == 0) return;
150
bool lastCharacterWasNewLine = LastCharacterIsNewLine;
151
if (lastCharacterWasNewLine) {
152
if (forceWriteInPreviousBlock == false) {
153
if (indent && txt != Environment.NewLine) Indent();
155
lineBeforeLastStart = lastLineStart;
156
lastLineStart = text.Length;
159
lastLineStart = lineBeforeLastStart;
161
string lastLine = text.ToString(lastLineStart, text.Length - lastLineStart);
162
text.Remove(lastLineStart, text.Length - lastLineStart);
163
if (indent && txt != Environment.NewLine) {
164
if (forceWriteInPreviousBlock) ++indentationLevel;
166
if (forceWriteInPreviousBlock) --indentationLevel;
169
lineBeforeLastStart = lastLineStart;
170
lastLineStart = text.Length;
171
text.Append(lastLine);
172
if (lastCharacterWasNewLine) {
173
lineBeforeLastStart = lastLineStart;
174
lastLineStart = text.Length;
180
/// Prints a text that cannot be inserted before using WriteInPreviousLine
181
/// into the current line
183
protected void PrintSpecialText(string specialText)
185
lineBeforeLastStart = text.Length;
186
text.Append(specialText);
187
lastLineStart = text.Length;
191
public void PrintTokenList(ArrayList tokenList)
193
foreach (int token in tokenList) {
199
// public abstract void PrintComment(Comment comment, bool forceWriteInPreviousBlock);
201
// public virtual void PrintPreprocessingDirective(PreprocessingDirective directive, bool forceWriteInPreviousBlock)
203
// if (!directive.Expression.IsNull) {
204
//// CSharpOutputVisitor visitor = new CSharpOutputVisitor();
205
//// directive.Expression.AcceptVisitor(visitor, null);
206
//// WriteLineInPreviousLine(directive.Cmd + " " + visitor.Text, forceWriteInPreviousBlock);
207
// } else if (string.IsNullOrEmpty(directive.Arg))
208
// WriteLineInPreviousLine(directive.Cmd, forceWriteInPreviousBlock);
210
// WriteLineInPreviousLine(directive.Cmd + " " + directive.Arg, forceWriteInPreviousBlock);
213
public void PrintBlankLine(bool forceWriteInPreviousBlock)
215
WriteInPreviousLine(Environment.NewLine, forceWriteInPreviousBlock);
218
public abstract void PrintToken(int token);
220
public void PrintText(string text)
222
this.text.Append(text);
226
public abstract void PrintIdentifier(string identifier);