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

« back to all changes in this revision

Viewing changes to contrib/ICSharpCode.NRefactory.CSharp/OutputVisitor/TextWriterOutputFormatter.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
2
 
// 
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:
8
 
// 
9
 
// The above copyright notice and this permission notice shall be included in all copies or
10
 
// substantial portions of the Software.
11
 
// 
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.
18
 
 
19
 
using System;
20
 
using System.IO;
21
 
 
22
 
namespace ICSharpCode.NRefactory.CSharp
23
 
{
24
 
        /// <summary>
25
 
        /// Writes C# code into a TextWriter.
26
 
        /// </summary>
27
 
        public class TextWriterOutputFormatter : IOutputFormatter
28
 
        {
29
 
                readonly TextWriter textWriter;
30
 
                int indentation;
31
 
                bool needsIndent = true;
32
 
                bool isAtStartOfLine = true;
33
 
 
34
 
                public int Indentation {
35
 
                        get {
36
 
                                return this.indentation;
37
 
                        }
38
 
                        set {
39
 
                                this.indentation = value;
40
 
                        }
41
 
                }
42
 
                
43
 
                public string IndentationString { get; set; }
44
 
                
45
 
                public TextWriterOutputFormatter(TextWriter textWriter)
46
 
                {
47
 
                        if (textWriter == null)
48
 
                                throw new ArgumentNullException("textWriter");
49
 
                        this.textWriter = textWriter;
50
 
                        this.IndentationString = "\t";
51
 
                }
52
 
                
53
 
                public void WriteIdentifier(string ident)
54
 
                {
55
 
                        WriteIndentation();
56
 
                        textWriter.Write(ident);
57
 
                        isAtStartOfLine = false;
58
 
                }
59
 
                
60
 
                public void WriteKeyword(string keyword)
61
 
                {
62
 
                        WriteIndentation();
63
 
                        textWriter.Write(keyword);
64
 
                        isAtStartOfLine = false;
65
 
                }
66
 
                
67
 
                public void WriteToken(string token)
68
 
                {
69
 
                        WriteIndentation();
70
 
                        textWriter.Write(token);
71
 
                        isAtStartOfLine = false;
72
 
                }
73
 
                
74
 
                public void Space()
75
 
                {
76
 
                        WriteIndentation();
77
 
                        textWriter.Write(' ');
78
 
                }
79
 
                
80
 
                public void OpenBrace(BraceStyle style)
81
 
                {
82
 
                        switch (style) {
83
 
                                case BraceStyle.DoNotChange:
84
 
                                case BraceStyle.EndOfLine:
85
 
                                case BraceStyle.BannerStyle:
86
 
                                        WriteIndentation();
87
 
                                        if (!isAtStartOfLine)
88
 
                                                textWriter.Write(' ');
89
 
                                        textWriter.Write('{');
90
 
                                        break;
91
 
                                case BraceStyle.EndOfLineWithoutSpace:
92
 
                                        WriteIndentation();
93
 
                                        textWriter.Write('{');
94
 
                                        break;
95
 
                                case BraceStyle.NextLine:
96
 
                                        if (!isAtStartOfLine)
97
 
                                                NewLine();
98
 
                                        WriteIndentation();
99
 
                                        textWriter.Write('{');
100
 
                                        break;
101
 
                                        
102
 
                                case BraceStyle.NextLineShifted:
103
 
                                        NewLine ();
104
 
                                        Indent();
105
 
                                        WriteIndentation();
106
 
                                        textWriter.Write('{');
107
 
                                        NewLine();
108
 
                                        return;
109
 
                                case BraceStyle.NextLineShifted2:
110
 
                                        NewLine ();
111
 
                                        Indent();
112
 
                                        WriteIndentation();
113
 
                                        textWriter.Write('{');
114
 
                                        break;
115
 
                                default:
116
 
                                        throw new ArgumentOutOfRangeException ();
117
 
                        }
118
 
                        Indent();
119
 
                        NewLine();
120
 
                }
121
 
                
122
 
                public void CloseBrace(BraceStyle style)
123
 
                {
124
 
                        switch (style) {
125
 
                                case BraceStyle.DoNotChange:
126
 
                                case BraceStyle.EndOfLine:
127
 
                                case BraceStyle.EndOfLineWithoutSpace:
128
 
                                case BraceStyle.NextLine:
129
 
                                        Unindent();
130
 
                                        WriteIndentation();
131
 
                                        textWriter.Write('}');
132
 
                                        isAtStartOfLine = false;
133
 
                                        break;
134
 
                                case BraceStyle.BannerStyle:
135
 
                                case BraceStyle.NextLineShifted:
136
 
                                        WriteIndentation();
137
 
                                        textWriter.Write('}');
138
 
                                        isAtStartOfLine = false;
139
 
                                        Unindent();
140
 
                                        break;
141
 
                                case BraceStyle.NextLineShifted2:
142
 
                                        Unindent();
143
 
                                        WriteIndentation();
144
 
                                        textWriter.Write('}');
145
 
                                        isAtStartOfLine = false;
146
 
                                        Unindent();
147
 
                                        break;
148
 
                                default:
149
 
                                        throw new ArgumentOutOfRangeException ();
150
 
                        }
151
 
                }
152
 
                
153
 
                protected void WriteIndentation()
154
 
                {
155
 
                        if (needsIndent) {
156
 
                                needsIndent = false;
157
 
                                for (int i = 0; i < indentation; i++) {
158
 
                                        textWriter.Write(this.IndentationString);
159
 
                                }
160
 
                        }
161
 
                }
162
 
                
163
 
                public void NewLine()
164
 
                {
165
 
                        textWriter.WriteLine();
166
 
                        needsIndent = true;
167
 
                        isAtStartOfLine = true;
168
 
                }
169
 
                
170
 
                public void Indent()
171
 
                {
172
 
                        indentation++;
173
 
                }
174
 
                
175
 
                public void Unindent()
176
 
                {
177
 
                        indentation--;
178
 
                }
179
 
                
180
 
                public void WriteComment(CommentType commentType, string content)
181
 
                {
182
 
                        WriteIndentation();
183
 
                        switch (commentType) {
184
 
                                case CommentType.SingleLine:
185
 
                                        textWriter.Write("//");
186
 
                                        textWriter.WriteLine(content);
187
 
                                        needsIndent = true;
188
 
                                        isAtStartOfLine = true;
189
 
                                        break;
190
 
                                case CommentType.MultiLine:
191
 
                                        textWriter.Write("/*");
192
 
                                        textWriter.Write(content);
193
 
                                        textWriter.Write("*/");
194
 
                                        isAtStartOfLine = false;
195
 
                                        break;
196
 
                                case CommentType.Documentation:
197
 
                                        textWriter.Write("///");
198
 
                                        textWriter.WriteLine(content);
199
 
                                        needsIndent = true;
200
 
                                        isAtStartOfLine = true;
201
 
                                        break;
202
 
                                default:
203
 
                                        textWriter.Write(content);
204
 
                                        break;
205
 
                        }
206
 
                }
207
 
                
208
 
                public void WritePreProcessorDirective(PreProcessorDirectiveType type, string argument)
209
 
                {
210
 
                        // pre-processor directive must start on its own line
211
 
                        if (!isAtStartOfLine)
212
 
                                NewLine();
213
 
                        WriteIndentation();
214
 
                        textWriter.Write('#');
215
 
                        textWriter.Write(type.ToString().ToLowerInvariant());
216
 
                        if (!string.IsNullOrEmpty(argument)) {
217
 
                                textWriter.Write(' ');
218
 
                                textWriter.Write(argument);
219
 
                        }
220
 
                        NewLine();
221
 
                }
222
 
                
223
 
                public virtual void StartNode(AstNode node)
224
 
                {
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
227
 
                        // in the output.
228
 
                        WriteIndentation();
229
 
                }
230
 
                
231
 
                public virtual void EndNode(AstNode node)
232
 
                {
233
 
                }
234
 
        }
235
 
}