5
// Lluis Sanchez Gual <lluis@novell.com>
7
// Copyright (c) 2009 Novell, Inc (http://www.novell.com)
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:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
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
30
namespace Mono.Addins.Setup
42
string indentString = "";
43
string formattedIndentString;
44
int indentColumnWidth;
45
string paragFormattedIndentString;
46
int paragIndentColumnWidth;
48
int paragraphStartMargin;
53
public int MaxColumns { get; set; }
55
StringBuilder builder = new StringBuilder ();
56
StringBuilder currentWord = new StringBuilder ();
58
bool lineStart = true;
59
bool lastWasSeparator;
60
bool paragraphStart = true;
63
public TextFormatter ()
70
get { return tabWidth; }
73
formattedIndentString = null;
77
public string IndentString {
78
get { return indentString; }
81
throw new ArgumentNullException ("value");
83
formattedIndentString = null;
87
public int LeftMargin {
93
formattedIndentString = null;
97
public int ParagraphStartMargin {
98
get { return paragraphStartMargin; }
100
paragraphStartMargin = value;
101
formattedIndentString = null;
105
public WrappingType Wrap {
111
AppendCurrentWord ('x');
117
public bool TabsAsSpaces {
118
get { return tabsAsSpaces; }
120
tabsAsSpaces = value;
121
formattedIndentString = null;
125
string FormattedIndentString {
127
if (formattedIndentString == null)
128
CreateIndentString ();
129
return formattedIndentString;
133
int IndentColumnWidth {
135
if (formattedIndentString == null)
136
CreateIndentString ();
137
return indentColumnWidth;
141
string ParagFormattedIndentString {
143
if (formattedIndentString == null)
144
CreateIndentString ();
145
return paragFormattedIndentString;
149
int ParagIndentColumnWidth {
151
if (formattedIndentString == null)
152
CreateIndentString ();
153
return paragIndentColumnWidth;
159
builder = new StringBuilder ();
160
currentWord = new StringBuilder ();
163
paragraphStart = true;
164
lastWasSeparator = false;
167
public void AppendWord (string text)
174
public void Append (string text)
176
if (string.IsNullOrEmpty (text))
179
if (builder.Length == 0) {
180
curCol = IndentColumnWidth;
182
paragraphStart = true;
185
if (Wrap == WrappingType.None || Wrap == WrappingType.Char) {
186
AppendChars (text, Wrap == WrappingType.Char);
192
while (n < text.Length)
195
bool foundSpace = false;
196
while (n < text.Length && !foundSpace) {
197
if ((char.IsWhiteSpace (text [n]) && wordLevel == 0) || text [n] == '\n')
204
currentWord.Append (text.Substring (sn, n - sn));
206
AppendCurrentWord (text[n]);
212
public void AppendLine ()
214
AppendCurrentWord ('x');
215
AppendChar ('\n', false);
218
public void BeginWord ()
223
public void EndWord ()
226
throw new InvalidOperationException ("Missing BeginWord call");
230
if (currentWord.Length > 0) {
231
lastChar = currentWord [currentWord.Length - 1];
232
if (char.IsWhiteSpace (lastChar))
233
currentWord.Remove (currentWord.Length - 1, 1);
235
AppendCurrentWord (lastChar);
238
public void FlushWord ()
240
AppendCurrentWord ('x');
241
if (curCol > MaxColumns)
245
public override string ToString ()
247
if (currentWord.Length > 0)
248
AppendCurrentWord ('x');
249
return builder.ToString ();
252
void AppendChars (string s, bool wrapChars)
254
foreach (char c in s)
255
AppendChar (c, wrapChars);
258
void AppendSoftBreak ()
260
AppendChar ('\n', true);
261
paragraphStart = false;
262
curCol = IndentColumnWidth;
265
void AppendChar (char c, bool wrapChars)
269
paragraphStart = true;
271
curCol = ParagIndentColumnWidth;
272
lastWasSeparator = false;
275
else if (lineStart) {
277
builder.Append (ParagFormattedIndentString);
279
builder.Append (FormattedIndentString);
281
paragraphStart = false;
282
lastWasSeparator = false;
284
if (wrapChars && curCol >= MaxColumns) {
286
if (!char.IsWhiteSpace (c))
287
AppendChar (c, false);
292
int tw = GetTabWidth (curCol);
294
builder.Append (' ', tw);
305
void AppendCurrentWord (char separatorChar)
307
if (currentWord.Length == 0)
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);
318
AppendChars (currentWord.ToString (), Wrap == WrappingType.WordChar);
319
if (char.IsWhiteSpace (separatorChar) || (separatorChar == '\n' && !lineStart)) {
320
lastWasSeparator = true;
321
AppendChar (separatorChar, true);
323
lastWasSeparator = false;
324
currentWord = new StringBuilder ();
327
int GetTabWidth (int startCol)
329
int res = startCol % TabWidth;
333
return TabWidth - res;
336
void CreateIndentString ()
338
StringBuilder sb = new StringBuilder ();
339
indentColumnWidth = AddIndentString (sb, indentString);
341
paragFormattedIndentString = sb.ToString () + new string (' ', paragraphStartMargin);
342
paragIndentColumnWidth = indentColumnWidth + paragraphStartMargin;
344
if (LeftMargin > 0) {
345
sb.Append (' ', LeftMargin);
346
indentColumnWidth += LeftMargin;
348
formattedIndentString = sb.ToString ();
351
curCol = paragIndentColumnWidth;
353
curCol = indentColumnWidth;
356
int AddIndentString (StringBuilder sb, string txt)
358
if (string.IsNullOrEmpty (txt))
361
foreach (char c in txt) {
363
int tw = GetTabWidth (count);