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

« back to all changes in this revision

Viewing changes to external/mono-addins/Mono.Addins.Setup/Mono.Addins.Setup/TextFormatter.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
// 
 
2
// TextFormatter.cs
 
3
//  
 
4
// Author:
 
5
//       Lluis Sanchez Gual <lluis@novell.com>
 
6
// 
 
7
// Copyright (c) 2009 Novell, Inc (http://www.novell.com)
 
8
// 
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
 
 
27
using System;
 
28
using System.Text;
 
29
 
 
30
namespace Mono.Addins.Setup
 
31
{
 
32
        enum WrappingType
 
33
        {
 
34
                None,
 
35
                Char,
 
36
                Word,
 
37
                WordChar
 
38
        }
 
39
        
 
40
        class TextFormatter
 
41
        {
 
42
                string indentString = "";
 
43
                string formattedIndentString;
 
44
                int indentColumnWidth;
 
45
                string paragFormattedIndentString;
 
46
                int paragIndentColumnWidth;
 
47
                int leftMargin;
 
48
                int paragraphStartMargin;
 
49
                WrappingType wrap;
 
50
                int tabWidth;
 
51
                bool tabsAsSpaces;
 
52
                
 
53
                public int MaxColumns { get; set; }
 
54
                
 
55
                StringBuilder builder = new StringBuilder ();
 
56
                StringBuilder currentWord = new StringBuilder ();
 
57
                int curCol;
 
58
                bool lineStart = true;
 
59
                bool lastWasSeparator;
 
60
                bool paragraphStart = true;
 
61
                int wordLevel;
 
62
                
 
63
                public TextFormatter ()
 
64
                {
 
65
                        MaxColumns = 80;
 
66
                        TabWidth = 4;
 
67
                }
 
68
                
 
69
                public int TabWidth {
 
70
                        get { return tabWidth; }
 
71
                        set {
 
72
                                tabWidth = value;
 
73
                                formattedIndentString = null;
 
74
                        }
 
75
                }
 
76
                
 
77
                public string IndentString {
 
78
                        get { return indentString; }
 
79
                        set {
 
80
                                if (value == null)
 
81
                                        throw new ArgumentNullException ("value");
 
82
                                indentString = value;
 
83
                                formattedIndentString = null;
 
84
                        }
 
85
                }
 
86
                
 
87
                public int LeftMargin {
 
88
                        get {
 
89
                                return leftMargin;
 
90
                        }
 
91
                        set {
 
92
                                leftMargin = value;
 
93
                                formattedIndentString = null;
 
94
                        }
 
95
                }
 
96
                
 
97
                public int ParagraphStartMargin {
 
98
                        get { return paragraphStartMargin; }
 
99
                        set {
 
100
                                paragraphStartMargin = value;
 
101
                                formattedIndentString = null;
 
102
                        }
 
103
                }
 
104
                
 
105
                public WrappingType Wrap {
 
106
                        get {
 
107
                                return wrap;
 
108
                        }
 
109
                        set {
 
110
                                if (wrap != value) {
 
111
                                        AppendCurrentWord ('x');
 
112
                                        wrap = value;
 
113
                                }
 
114
                        }
 
115
                }
 
116
                
 
117
                public bool TabsAsSpaces {
 
118
                        get { return tabsAsSpaces; }
 
119
                        set {
 
120
                                tabsAsSpaces = value;
 
121
                                formattedIndentString = null;
 
122
                        }
 
123
                }
 
124
                
 
125
                string FormattedIndentString {
 
126
                        get {
 
127
                                if (formattedIndentString == null)
 
128
                                        CreateIndentString ();
 
129
                                return formattedIndentString;
 
130
                        }
 
131
                }
 
132
                
 
133
                int IndentColumnWidth {
 
134
                        get {
 
135
                                if (formattedIndentString == null)
 
136
                                        CreateIndentString ();
 
137
                                return indentColumnWidth;
 
138
                        }
 
139
                }
 
140
                
 
141
                string ParagFormattedIndentString {
 
142
                        get {
 
143
                                if (formattedIndentString == null)
 
144
                                        CreateIndentString ();
 
145
                                return paragFormattedIndentString;
 
146
                        }
 
147
                }
 
148
                
 
149
                int ParagIndentColumnWidth {
 
150
                        get {
 
151
                                if (formattedIndentString == null)
 
152
                                        CreateIndentString ();
 
153
                                return paragIndentColumnWidth;
 
154
                        }
 
155
                }
 
156
                
 
157
                public void Clear ()
 
158
                {
 
159
                        builder = new StringBuilder ();
 
160
                        currentWord = new StringBuilder ();
 
161
                        curCol = 0;
 
162
                        lineStart = true;
 
163
                        paragraphStart = true;
 
164
                        lastWasSeparator = false;
 
165
                }
 
166
                
 
167
                public void AppendWord (string text)
 
168
                {
 
169
                        BeginWord ();
 
170
                        Append (text);
 
171
                        EndWord ();
 
172
                }
 
173
                
 
174
                public void Append (string text)
 
175
                {
 
176
                        if (string.IsNullOrEmpty (text))
 
177
                                return;
 
178
                        
 
179
                        if (builder.Length == 0) {
 
180
                                curCol = IndentColumnWidth;
 
181
                                lineStart = true;
 
182
                                paragraphStart = true;
 
183
                        }
 
184
                        
 
185
                        if (Wrap == WrappingType.None || Wrap == WrappingType.Char) {
 
186
                                AppendChars (text, Wrap == WrappingType.Char);
 
187
                                return;
 
188
                        }
 
189
                        
 
190
                        int n = 0;
 
191
                        
 
192
                        while (n < text.Length)
 
193
                        {
 
194
                                int sn = n;
 
195
                                bool foundSpace = false;
 
196
                                while (n < text.Length && !foundSpace) {
 
197
                                        if ((char.IsWhiteSpace (text [n]) && wordLevel == 0) || text [n] == '\n')
 
198
                                                foundSpace = true;
 
199
                                        else
 
200
                                                n++;
 
201
                                }
 
202
                                
 
203
                                if (n != sn)
 
204
                                        currentWord.Append (text.Substring (sn, n - sn));
 
205
                                if (foundSpace) {
 
206
                                        AppendCurrentWord (text[n]);
 
207
                                        n++;
 
208
                                }
 
209
                        }
 
210
                }
 
211
                
 
212
                public void AppendLine ()
 
213
                {
 
214
                        AppendCurrentWord ('x');
 
215
                        AppendChar ('\n', false);
 
216
                }
 
217
                
 
218
                public void BeginWord ()
 
219
                {
 
220
                        wordLevel++;
 
221
                }
 
222
                
 
223
                public void EndWord ()
 
224
                {
 
225
                        if (wordLevel == 0)
 
226
                                throw new InvalidOperationException ("Missing BeginWord call");
 
227
                        wordLevel--;
 
228
                        
 
229
                        char lastChar = 'x';
 
230
                        if (currentWord.Length > 0) {
 
231
                                lastChar = currentWord [currentWord.Length - 1];
 
232
                                if (char.IsWhiteSpace (lastChar))
 
233
                                        currentWord.Remove (currentWord.Length - 1, 1);
 
234
                        }
 
235
                        AppendCurrentWord (lastChar);
 
236
                }
 
237
                
 
238
                public void FlushWord ()
 
239
                {
 
240
                        AppendCurrentWord ('x');
 
241
                        if (curCol > MaxColumns)
 
242
                                AppendSoftBreak ();
 
243
                }
 
244
                
 
245
                public override string ToString ()
 
246
                {
 
247
                        if (currentWord.Length > 0)
 
248
                                AppendCurrentWord ('x');
 
249
                        return builder.ToString ();
 
250
                }
 
251
                
 
252
                void AppendChars (string s, bool wrapChars)
 
253
                {
 
254
                        foreach (char c in s)
 
255
                                AppendChar (c, wrapChars);
 
256
                }
 
257
                
 
258
                void AppendSoftBreak ()
 
259
                {
 
260
                        AppendChar ('\n', true);
 
261
                        paragraphStart = false;
 
262
                        curCol = IndentColumnWidth;
 
263
                }
 
264
                
 
265
                void AppendChar (char c, bool wrapChars)
 
266
                {
 
267
                        if (c == '\n') {
 
268
                                lineStart = true;
 
269
                                paragraphStart = true;
 
270
                                builder.Append (c);
 
271
                                curCol = ParagIndentColumnWidth;
 
272
                                lastWasSeparator = false;
 
273
                                return;
 
274
                        }
 
275
                        else if (lineStart) {
 
276
                                if (paragraphStart)
 
277
                                        builder.Append (ParagFormattedIndentString);
 
278
                                else
 
279
                                        builder.Append (FormattedIndentString);
 
280
                                lineStart = false;
 
281
                                paragraphStart = false;
 
282
                                lastWasSeparator = false;
 
283
                        }
 
284
                        if (wrapChars && curCol >= MaxColumns) {
 
285
                                AppendSoftBreak ();
 
286
                                if (!char.IsWhiteSpace (c))
 
287
                                        AppendChar (c, false);
 
288
                                return;
 
289
                        }
 
290
                        
 
291
                        if (c == '\t') {
 
292
                                int tw = GetTabWidth (curCol);
 
293
                                if (TabsAsSpaces)
 
294
                                        builder.Append (' ', tw);
 
295
                                else
 
296
                                        builder.Append (c);
 
297
                                curCol += tw;
 
298
                        }
 
299
                        else {
 
300
                                builder.Append (c);
 
301
                                curCol++;
 
302
                        }
 
303
                }
 
304
                
 
305
                void AppendCurrentWord (char separatorChar)
 
306
                {
 
307
                        if (currentWord.Length == 0)
 
308
                                return;
 
309
                        if (Wrap == WrappingType.Word || Wrap == WrappingType.WordChar) {
 
310
                                if (curCol + currentWord.Length > MaxColumns) {
 
311
                                        // If the last char was a word separator, remove it
 
312
                                        if (lastWasSeparator)
 
313
                                                builder.Remove (builder.Length - 1, 1);
 
314
                                        if (!lineStart)
 
315
                                                AppendSoftBreak ();
 
316
                                }
 
317
                        }
 
318
                        AppendChars (currentWord.ToString (), Wrap == WrappingType.WordChar);
 
319
                        if (char.IsWhiteSpace (separatorChar) || (separatorChar == '\n' && !lineStart)) {
 
320
                                lastWasSeparator = true;
 
321
                                AppendChar (separatorChar, true);
 
322
                        } else
 
323
                                lastWasSeparator = false;
 
324
                        currentWord = new StringBuilder ();
 
325
                }
 
326
                
 
327
                int GetTabWidth (int startCol)
 
328
                {
 
329
                        int res = startCol % TabWidth;
 
330
                        if (res == 0)
 
331
                                return TabWidth;
 
332
                        else
 
333
                                return TabWidth - res;
 
334
                }
 
335
                
 
336
                void CreateIndentString ()
 
337
                {
 
338
                        StringBuilder sb = new StringBuilder ();
 
339
                        indentColumnWidth = AddIndentString (sb, indentString);
 
340
                        
 
341
                        paragFormattedIndentString = sb.ToString () + new string (' ', paragraphStartMargin);
 
342
                        paragIndentColumnWidth = indentColumnWidth + paragraphStartMargin;
 
343
                        
 
344
                        if (LeftMargin > 0) {
 
345
                                sb.Append (' ', LeftMargin);
 
346
                                indentColumnWidth += LeftMargin;
 
347
                        }
 
348
                        formattedIndentString = sb.ToString ();
 
349
 
 
350
                        if (paragraphStart)
 
351
                                curCol = paragIndentColumnWidth;
 
352
                        else if (lineStart)
 
353
                                curCol = indentColumnWidth;
 
354
                }
 
355
                
 
356
                int AddIndentString (StringBuilder sb, string txt)
 
357
                {
 
358
                        if (string.IsNullOrEmpty (txt))
 
359
                                return 0;
 
360
                        int count = 0;
 
361
                        foreach (char c in txt) {
 
362
                                if (c == '\t') {
 
363
                                        int tw = GetTabWidth (count);
 
364
                                        count += tw;
 
365
                                        if (TabsAsSpaces)
 
366
                                                sb.Append (' ', tw);
 
367
                                        else
 
368
                                                sb.Append (c);
 
369
                                }
 
370
                                else {
 
371
                                        sb.Append (c);
 
372
                                        count++;
 
373
                                }
 
374
                        }
 
375
                        return count;
 
376
                }
 
377
        }
 
378
}