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

« back to all changes in this revision

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