~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to contrib/NRefactory/Project/Src/PrettyPrinter/AbstractOutputFormatter.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// <file>
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>
6
 
// </file>
7
 
 
8
 
using System;
9
 
using System.Collections;
10
 
using System.Text;
11
 
 
12
 
namespace ICSharpCode.OldNRefactory.PrettyPrinter
13
 
{
14
 
        /// <summary>
15
 
        /// Base class of output formatters.
16
 
        /// </summary>
17
 
        public abstract class AbstractOutputFormatter : IOutputFormatter
18
 
        {
19
 
                StringBuilder text = new StringBuilder();
20
 
                
21
 
                int           indentationLevel = 0;
22
 
                bool          indent         = true;
23
 
                bool          doNewLine      = true;
24
 
                AbstractPrettyPrintOptions prettyPrintOptions;
25
 
                
26
 
                public bool IsInMemberBody { get; set; }
27
 
                
28
 
                public int IndentationLevel {
29
 
                        get {
30
 
                                return indentationLevel;
31
 
                        }
32
 
                        set {
33
 
                                indentationLevel = value;
34
 
                        }
35
 
                }
36
 
                
37
 
                public string Text {
38
 
                        get {
39
 
                                return text.ToString();
40
 
                        }
41
 
                }
42
 
                
43
 
                public int TextLength {
44
 
                        get {
45
 
                                return text.Length;
46
 
                        }
47
 
                }
48
 
                
49
 
                
50
 
                public bool DoIndent {
51
 
                        get {
52
 
                                return indent;
53
 
                        }
54
 
                        set {
55
 
                                indent = value;
56
 
                        }
57
 
                }
58
 
                
59
 
                public bool DoNewLine {
60
 
                        get {
61
 
                                return doNewLine;
62
 
                        }
63
 
                        set {
64
 
                                doNewLine = value;
65
 
                        }
66
 
                }
67
 
                
68
 
                protected AbstractOutputFormatter(AbstractPrettyPrintOptions prettyPrintOptions)
69
 
                {
70
 
                        this.prettyPrintOptions = prettyPrintOptions;
71
 
                }
72
 
                
73
 
                internal bool isIndented = false;
74
 
                public void Indent()
75
 
                {
76
 
                        if (DoIndent) {
77
 
                                int indent = 0;
78
 
                                while (indent < prettyPrintOptions.IndentSize * indentationLevel) {
79
 
                                        char ch = prettyPrintOptions.IndentationChar;
80
 
                                        if (ch == '\t' && indent + prettyPrintOptions.TabSize > prettyPrintOptions.IndentSize * indentationLevel) {
81
 
                                                ch = ' ';
82
 
                                        }
83
 
                                        text.Append(ch);
84
 
                                        if (ch == '\t') {
85
 
                                                indent += prettyPrintOptions.TabSize;
86
 
                                        } else {
87
 
                                                ++indent;
88
 
                                        }
89
 
                                }
90
 
                                isIndented = true;
91
 
                        }
92
 
                }
93
 
                
94
 
                public void Reset ()
95
 
                {
96
 
                        text.Length = 0;
97
 
                        isIndented = false;
98
 
                }
99
 
                
100
 
                public void Space()
101
 
                {
102
 
                        text.Append(' ');
103
 
                        isIndented = false;
104
 
                }
105
 
                
106
 
                internal int lastLineStart = 0;
107
 
                internal int lineBeforeLastStart = 0;
108
 
                
109
 
                public bool LastCharacterIsNewLine {
110
 
                        get {
111
 
                                return text.Length == lastLineStart;
112
 
                        }
113
 
                }
114
 
                
115
 
                public bool LastCharacterIsWhiteSpace {
116
 
                        get {
117
 
                                return text.Length == 0 || char.IsWhiteSpace(text[text.Length - 1]);
118
 
                        }
119
 
                }
120
 
                
121
 
                public virtual void NewLine()
122
 
                {
123
 
                        if (DoNewLine) {
124
 
                                if (!LastCharacterIsNewLine) {
125
 
                                        lineBeforeLastStart = lastLineStart;
126
 
                                }
127
 
                                text.Append(prettyPrintOptions.EolMarker);
128
 
                                lastLineStart = text.Length;
129
 
                                isIndented = false;
130
 
                        }
131
 
                }
132
 
                
133
 
                public virtual void EndFile()
134
 
                {
135
 
                }
136
 
                
137
 
                protected void WriteLineInPreviousLine(string txt, bool forceWriteInPreviousBlock)
138
 
                {
139
 
                        WriteInPreviousLine(txt + prettyPrintOptions.EolMarker, forceWriteInPreviousBlock);
140
 
                }
141
 
                protected void WriteLineInPreviousLine(string txt, bool forceWriteInPreviousBlock, bool indent)
142
 
                {
143
 
                        WriteInPreviousLine(txt + prettyPrintOptions.EolMarker, forceWriteInPreviousBlock, indent);
144
 
                }
145
 
                
146
 
                protected void WriteInPreviousLine(string txt, bool forceWriteInPreviousBlock)
147
 
                {
148
 
                        WriteInPreviousLine(txt, forceWriteInPreviousBlock, true);
149
 
                }
150
 
                protected void WriteInPreviousLine(string txt, bool forceWriteInPreviousBlock, bool indent)
151
 
                {
152
 
                        if (txt.Length == 0) return;
153
 
                        
154
 
                        bool lastCharacterWasNewLine = LastCharacterIsNewLine;
155
 
                        if (lastCharacterWasNewLine) {
156
 
                                if (forceWriteInPreviousBlock == false) {
157
 
                                        if (indent && txt != prettyPrintOptions.EolMarker) Indent();
158
 
                                        text.Append(txt);
159
 
                                        lineBeforeLastStart = lastLineStart;
160
 
                                        lastLineStart = text.Length;
161
 
                                        return;
162
 
                                }
163
 
                                lastLineStart = lineBeforeLastStart;
164
 
                        }
165
 
                        string lastLine = text.ToString(lastLineStart, text.Length - lastLineStart);
166
 
                        text.Remove(lastLineStart, text.Length - lastLineStart);
167
 
                        if (indent) {
168
 
                                if (forceWriteInPreviousBlock) ++indentationLevel;
169
 
                                Indent();
170
 
                                if (forceWriteInPreviousBlock) --indentationLevel;
171
 
                        }
172
 
                        text.Append(txt);
173
 
                        lineBeforeLastStart = lastLineStart;
174
 
                        lastLineStart = text.Length;
175
 
                        text.Append(lastLine);
176
 
                        if (lastCharacterWasNewLine) {
177
 
                                lineBeforeLastStart = lastLineStart;
178
 
                                lastLineStart = text.Length;
179
 
                        }
180
 
                        isIndented = false;
181
 
                }
182
 
                
183
 
                /// <summary>
184
 
                /// Prints a text that cannot be inserted before using WriteInPreviousLine
185
 
                /// into the current line
186
 
                /// </summary>
187
 
                protected void PrintSpecialText(string specialText)
188
 
                {
189
 
                        lineBeforeLastStart = text.Length;
190
 
                        text.Append(specialText);
191
 
                        lastLineStart = text.Length;
192
 
                        isIndented = false;
193
 
                }
194
 
                
195
 
                public void PrintTokenList(ArrayList tokenList)
196
 
                {
197
 
                        foreach (int token in tokenList) {
198
 
                                PrintToken(token);
199
 
                                Space();
200
 
                        }
201
 
                }
202
 
                
203
 
                public abstract void PrintComment(Comment comment, bool forceWriteInPreviousBlock);
204
 
                
205
 
                public virtual void PrintPreprocessingDirective(PreprocessingDirective directive, bool forceWriteInPreviousBlock)
206
 
                {
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);
213
 
                        else
214
 
                                WriteLineInPreviousLine(directive.Cmd + " " + directive.Arg, forceWriteInPreviousBlock);
215
 
                }
216
 
                
217
 
                public void PrintBlankLine(bool forceWriteInPreviousBlock)
218
 
                {
219
 
                        WriteInPreviousLine(prettyPrintOptions.EolMarker, forceWriteInPreviousBlock);
220
 
                }
221
 
                
222
 
                public abstract void PrintToken(int token);
223
 
                
224
 
                public void PrintText(string text)
225
 
                {
226
 
                        this.text.Append(text);
227
 
                        isIndented = false;
228
 
                }
229
 
                
230
 
                public abstract void PrintIdentifier(string identifier);
231
 
        }
232
 
}