-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRuntimeTextTemplate1.vb
252 lines (251 loc) · 11 KB
/
RuntimeTextTemplate1.vb
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
Imports System.Linq
Imports System.Text
Imports System.Collections.Generic
Imports System
'------------------------------------------------------------------------------
'<auto-generated>
' This code was generated by a tool.
' Runtime Version: 14.0.0.0
'
' Changes to this file may cause incorrect behavior and will be lost if
' the code is regenerated.
'</auto-generated>
'------------------------------------------------------------------------------
Namespace My.Templates
'''<summary>
'''Class to produce the template output
'''</summary>
<Global.System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "14.0.0.0")> _
Partial Public Class RuntimeTextTemplate1
Inherits RuntimeTextTemplate1Base
'''<summary>
'''Create the template output
'''</summary>
Public Overridable Function TransformText() As String
Return Me.GenerationEnvironment.ToString
End Function
End Class
#Region "Base class"
'''<summary>
'''Base class for this transformation
'''</summary>
<Global.System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "14.0.0.0")> _
Public Class RuntimeTextTemplate1Base
#Region "Fields"
Private generationEnvironmentField As Global.System.Text.StringBuilder
Private errorsField As Global.System.CodeDom.Compiler.CompilerErrorCollection
Private indentLengthsField As Global.System.Collections.Generic.List(Of Integer)
Private currentIndentField As String = ""
Private endsWithNewline As Boolean
Private sessionField As Global.System.Collections.Generic.IDictionary(Of String, Object)
#End Region
#Region "Properties"
'''<summary>
'''The string builder that generation-time code is using to assemble generated output
'''</summary>
Protected Property GenerationEnvironment() As System.Text.StringBuilder
Get
If (Me.generationEnvironmentField Is Nothing) Then
Me.generationEnvironmentField = New Global.System.Text.StringBuilder()
End If
Return Me.generationEnvironmentField
End Get
Set
Me.generationEnvironmentField = value
End Set
End Property
'''<summary>
'''The error collection for the generation process
'''</summary>
Public ReadOnly Property Errors() As System.CodeDom.Compiler.CompilerErrorCollection
Get
If (Me.errorsField Is Nothing) Then
Me.errorsField = New Global.System.CodeDom.Compiler.CompilerErrorCollection()
End If
Return Me.errorsField
End Get
End Property
'''<summary>
'''A list of the lengths of each indent that was added with PushIndent
'''</summary>
Private ReadOnly Property indentLengths() As System.Collections.Generic.List(Of Integer)
Get
If (Me.indentLengthsField Is Nothing) Then
Me.indentLengthsField = New Global.System.Collections.Generic.List(Of Integer)()
End If
Return Me.indentLengthsField
End Get
End Property
'''<summary>
'''Gets the current indent we use when adding lines to the output
'''</summary>
Public ReadOnly Property CurrentIndent() As String
Get
Return Me.currentIndentField
End Get
End Property
'''<summary>
'''Current transformation session
'''</summary>
Public Overridable Property Session() As Global.System.Collections.Generic.IDictionary(Of String, Object)
Get
Return Me.sessionField
End Get
Set
Me.sessionField = value
End Set
End Property
#End Region
#Region "Transform-time helpers"
'''<summary>
'''Write text directly into the generated output
'''</summary>
Public Overloads Sub Write(ByVal textToAppend As String)
If String.IsNullOrEmpty(textToAppend) Then
Return
End If
'If we're starting off, or if the previous text ended with a newline,
'we have to append the current indent first.
If ((Me.GenerationEnvironment.Length = 0) _
OrElse Me.endsWithNewline) Then
Me.GenerationEnvironment.Append(Me.currentIndentField)
Me.endsWithNewline = false
End If
'Check if the current text ends with a newline
If textToAppend.EndsWith(Global.System.Environment.NewLine, Global.System.StringComparison.CurrentCulture) Then
Me.endsWithNewline = true
End If
'This is an optimization. If the current indent is "", then we don't have to do any
'of the more complex stuff further down.
If (Me.currentIndentField.Length = 0) Then
Me.GenerationEnvironment.Append(textToAppend)
Return
End If
'Everywhere there is a newline in the text, add an indent after it
textToAppend = textToAppend.Replace(Global.System.Environment.NewLine, (Global.System.Environment.NewLine + Me.currentIndentField))
'If the text ends with a newline, then we should strip off the indent added at the very end
'because the appropriate indent will be added when the next time Write() is called
If Me.endsWithNewline Then
Me.GenerationEnvironment.Append(textToAppend, 0, (textToAppend.Length - Me.currentIndentField.Length))
Else
Me.GenerationEnvironment.Append(textToAppend)
End If
End Sub
'''<summary>
'''Write text directly into the generated output
'''</summary>
Public Overloads Sub WriteLine(ByVal textToAppend As String)
Me.Write(textToAppend)
Me.GenerationEnvironment.AppendLine
Me.endsWithNewline = true
End Sub
'''<summary>
'''Write formatted text directly into the generated output
'''</summary>
Public Overloads Sub Write(ByVal format As String, <System.ParamArrayAttribute()> ByVal args() As Object)
Me.Write(String.Format(Global.System.Globalization.CultureInfo.CurrentCulture, format, args))
End Sub
'''<summary>
'''Write formatted text directly into the generated output
'''</summary>
Public Overloads Sub WriteLine(ByVal format As String, <System.ParamArrayAttribute()> ByVal args() As Object)
Me.WriteLine(String.Format(Global.System.Globalization.CultureInfo.CurrentCulture, format, args))
End Sub
'''<summary>
'''Raise an error
'''</summary>
Public Sub [Error](ByVal message As String)
Dim [error] As System.CodeDom.Compiler.CompilerError = New Global.System.CodeDom.Compiler.CompilerError()
[error].ErrorText = message
Me.Errors.Add([error])
End Sub
'''<summary>
'''Raise a warning
'''</summary>
Public Sub Warning(ByVal message As String)
Dim [error] As System.CodeDom.Compiler.CompilerError = New Global.System.CodeDom.Compiler.CompilerError()
[error].ErrorText = message
[error].IsWarning = true
Me.Errors.Add([error])
End Sub
'''<summary>
'''Increase the indent
'''</summary>
Public Sub PushIndent(ByVal indent As String)
If (indent = Nothing) Then
Throw New Global.System.ArgumentNullException("indent")
End If
Me.currentIndentField = (Me.currentIndentField + indent)
Me.indentLengths.Add(indent.Length)
End Sub
'''<summary>
'''Remove the last indent that was added with PushIndent
'''</summary>
Public Function PopIndent() As String
Dim returnValue As String = ""
If (Me.indentLengths.Count > 0) Then
Dim indentLength As Integer = Me.indentLengths((Me.indentLengths.Count - 1))
Me.indentLengths.RemoveAt((Me.indentLengths.Count - 1))
If (indentLength > 0) Then
returnValue = Me.currentIndentField.Substring((Me.currentIndentField.Length - indentLength))
Me.currentIndentField = Me.currentIndentField.Remove((Me.currentIndentField.Length - indentLength))
End If
End If
Return returnValue
End Function
'''<summary>
'''Remove any indentation
'''</summary>
Public Sub ClearIndent()
Me.indentLengths.Clear
Me.currentIndentField = ""
End Sub
#End Region
#Region "ToString Helpers"
'''<summary>
'''Utility class to produce culture-oriented representation of an object as a string.
'''</summary>
Public Class ToStringInstanceHelper
Private formatProviderField As System.IFormatProvider = Global.System.Globalization.CultureInfo.InvariantCulture
'''<summary>
'''Gets or sets format provider to be used by ToStringWithCulture method.
'''</summary>
Public Property FormatProvider() As System.IFormatProvider
Get
Return Me.formatProviderField
End Get
Set
If (Not (value) Is Nothing) Then
Me.formatProviderField = value
End If
End Set
End Property
'''<summary>
'''This is called from the compile/run appdomain to convert objects within an expression block to a string
'''</summary>
Public Function ToStringWithCulture(ByVal objectToConvert As Object) As String
If (objectToConvert Is Nothing) Then
Throw New Global.System.ArgumentNullException("objectToConvert")
End If
Dim t As System.Type = objectToConvert.GetType
Dim method As System.Reflection.MethodInfo = t.GetMethod("ToString", New System.Type() {GetType(System.IFormatProvider)})
If (method Is Nothing) Then
Return objectToConvert.ToString
Else
Return CType(method.Invoke(objectToConvert, New Object() {Me.formatProviderField }),String)
End If
End Function
End Class
Private toStringHelperField As ToStringInstanceHelper = New ToStringInstanceHelper()
'''<summary>
'''Helper to produce culture-oriented representation of an object as a string
'''</summary>
Public ReadOnly Property ToStringHelper() As ToStringInstanceHelper
Get
Return Me.toStringHelperField
End Get
End Property
#End Region
End Class
#End Region
End Namespace