-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptBuilder.cs
More file actions
129 lines (111 loc) · 2.98 KB
/
Copy pathScriptBuilder.cs
File metadata and controls
129 lines (111 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (c) Rotorz Limited. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root.
using System.Collections.Generic;
using System.Text;
using UnityEngine;
namespace ScriptTemplates {
/// <summary>
/// Helps you to build script files with support for automatically indenting source code.
/// </summary>
public sealed class ScriptBuilder {
private StringBuilder _sb = new StringBuilder();
private int _indentLevel = 0;
private string _indent = "";
private string _indentChars = "\t";
private string _indentCharsNewLine = "\n";
/// <summary>
/// Gets or sets sequence of characters to use when indenting text.
/// </summary>
/// <remarks>
/// <para>Changing this value will not affect text which has already been appended.</para>
/// </remarks>
public string IndentChars {
get { return _indentChars; }
set {
int restoreIndent = _indentLevel;
_indentLevel = -1;
_indentChars = value;
IndentLevel = restoreIndent;
}
}
/// <summary>
/// Gets or sets current indent level within script.
/// </summary>
public int IndentLevel {
get { return _indentLevel; }
set {
value = Mathf.Max(0, value);
if (value != _indentLevel) {
if (value < _indentLevel)
_sb.Length -= _indentChars.Length;
_indentLevel = value;
_indent = "";
for (int i = 0; i < value; ++i)
_indent += _indentChars;
_indentCharsNewLine = "\n" + _indent;
}
}
}
/// <summary>
/// Clear output and start over.
/// </summary>
/// <remarks>
/// <para>This method also resets indention to 0.</para>
/// </remarks>
public void Clear() {
_sb.Length = 0;
IndentLevel = 0;
}
/// <summary>
/// Append text to script and begin new line.
/// </summary>
/// <param name="text">Text.</param>
public void AppendLine(string text) {
Append(text);
_sb.Append(_indentCharsNewLine);
}
/// <summary>
/// Append blank line to script.
/// </summary>
public void AppendLine() {
_sb.Append(_indentCharsNewLine);
}
/// <summary>
/// Append text to script.
/// </summary>
/// <param name="text">Text.</param>
public void Append(string text) {
_sb.Append(text.Replace("\n", _indentCharsNewLine));
}
/// <summary>
/// Begin namespace scope and automatically indent.
/// </summary>
/// <param name="text">Text.</param>
public void BeginNamespace(string text) {
Append(text);
_sb.AppendLine();
++IndentLevel;
_sb.Append(_indentCharsNewLine);
}
/// <summary>
/// End namespace scope and unindent.
/// </summary>
/// <param name="text">Text.</param>
public void EndNamespace(string text) {
--IndentLevel;
_sb.Append(text.Replace("\n", _indentCharsNewLine));
_sb.AppendLine();
_sb.Append(_indent);
AppendLine();
}
/// <summary>
/// Get generated source code as string.
/// </summary>
/// <returns>
/// The string.
/// </returns>
public override string ToString() {
return _sb.ToString().Trim() + "\n";
}
}
}