Repository navigation
Expand file tree
/
Copy pathUtils.vb
More file actions
339 lines (292 loc) · 13.9 KB
/
Copy pathUtils.vb
File metadata and controls
339 lines (292 loc) · 13.9 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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
Imports System.Runtime.CompilerServices
Imports System.Text
Imports System.Xml.Linq
Imports Microsoft.CodeAnalysis
Imports Microsoft.CodeAnalysis.Editor.UnitTests.Extensions
Imports Microsoft.CodeAnalysis.Text
Imports Microsoft.CodeAnalysis.VisualBasic
Imports Microsoft.CodeAnalysis.VisualBasic.Symbols
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests
Module Utils
Friend Function ParseCode(code As String) As SyntaxTree
Dim text = SourceText.From(code)
Return SyntaxFactory.ParseSyntaxTree(text)
End Function
Friend Function StringFromLines(ParamArray lines As String()) As String
Return String.Join(Environment.NewLine, lines)
End Function
Friend Function ParseLines(ParamArray lines As String()) As SyntaxTree
Dim code = StringFromLines(lines)
Return ParseCode(code)
End Function
Friend Function ParseExpression(expr As String) As SyntaxTree
Dim format =
"Class C1 " & vbCrLf &
" Sub S1()" & vbCrLf &
" Dim x = {0}" & vbCrLf &
" End Sub" & vbCrLf &
"End Class"
Dim code = String.Format(format, expr)
Return ParseCode(code)
End Function
Friend Function ParseStatement(statement As String) As SyntaxTree
Dim format = StringFromLines(
"Class C1",
" Sub S1()",
" {0}",
" End Sub",
"End Class")
Dim code = String.Format(format, statement)
Return ParseCode(code)
End Function
''' <summary>
''' DFS search to find the first node of a given type.
''' </summary>
<Extension()>
Friend Function FindFirstNodeOfType(Of T As SyntaxNode)(node As SyntaxNode) As T
If TypeOf (node) Is T Then
Return CType(node, T)
End If
For Each child In node.ChildNodesAndTokens()
If child.IsNode Then
Dim foundNode = child.AsNode().FindFirstNodeOfType(Of T)()
If foundNode IsNot Nothing Then
Return foundNode
End If
End If
Next
Return Nothing
End Function
<Extension()>
Friend Function DigToNthNodeOfType(Of T As SyntaxNode)(node As SyntaxNode, index As Integer) As T
Return node.ChildNodesAndTokens().Where(Function(n) n.IsNode).
Select(Function(n) n.AsNode()).
OfType(Of T).ElementAt(index)
End Function
<Extension()>
Friend Function DigToFirstNodeOfType(Of T As SyntaxNode)(node As SyntaxNode) As T
Return node.ChildNodesAndTokens().Where(Function(n) n.IsNode).
Select(Function(n) n.AsNode()).
OfType(Of T).First()
End Function
<Extension()>
Friend Function DigToFirstNodeOfType(Of T As SyntaxNode)(syntaxTree As SyntaxTree) As T
Return syntaxTree.GetRoot().DigToFirstNodeOfType(Of T)()
End Function
<Extension()>
Friend Function DigToLastNodeOfType(Of T As SyntaxNode)(node As SyntaxNode) As T
Return node.ChildNodesAndTokens().Where(Function(n) n.IsNode).
Select(Function(n) n.AsNode()).
OfType(Of T).Last()
End Function
<Extension()>
Friend Function DigToFirstTypeBlock(syntaxTree As SyntaxTree) As TypeBlockSyntax
Return syntaxTree.GetRoot().ChildNodesAndTokens().Where(Function(n) n.IsNode).
Select(Function(n) n.AsNode()).
OfType(Of TypeBlockSyntax).First()
End Function
<Extension()>
Friend Function DigToFirstNamespace(syntaxTree As SyntaxTree) As NamespaceBlockSyntax
Return syntaxTree.GetRoot().ChildNodesAndTokens().Where(Function(n) n.IsNode).
Select(Function(n) n.AsNode()).
OfType(Of NamespaceBlockSyntax).First()
End Function
Friend Class TreeNodePair(Of T As SyntaxNode)
Private ReadOnly _tree As SyntaxTree
Private ReadOnly _node As T
Sub New(syntaxTree As SyntaxTree, node As T)
Contract.ThrowIfNull(syntaxTree)
Contract.ThrowIfNull(node)
_tree = syntaxTree
_node = node
End Sub
ReadOnly Property Tree As SyntaxTree
Get
Return _tree
End Get
End Property
ReadOnly Property Node As T
Get
Return _node
End Get
End Property
End Class
Friend Class TreeNodePair
Friend Shared Function Create(Of T As SyntaxNode)(syntaxTree As SyntaxTree, node As T) As TreeNodePair(Of T)
Return New TreeNodePair(Of T)(syntaxTree, node)
End Function
End Class
Private Function SplitIntoLines(
text As String,
separator As String,
Optional removeLeadingLineBreaks As Boolean = True,
Optional removeTrailingLineBreaks As Boolean = True) As IEnumerable(Of String)
Dim lines = text.Split({separator}, StringSplitOptions.None).ToList()
If removeLeadingLineBreaks Then
While lines.Count > 0
If lines(0).Length = 0 Then
lines.RemoveAt(0)
Else
Exit While
End If
End While
End If
If removeTrailingLineBreaks Then
For i = lines.Count - 1 To 0 Step -1
If lines(i).Length = 0 Then
lines.RemoveAt(i)
Else
Exit For
End If
Next
End If
Return lines
End Function
Private Function SurroundAndJoinLines(
lines As IEnumerable(Of String),
Optional leading As String = Nothing,
Optional trailing As String = Nothing) As String
Dim builder As New StringBuilder
For Each line In lines
If Not String.IsNullOrWhiteSpace(line) Then
builder.Append(leading)
End If
builder.Append(line)
builder.Append(trailing)
Next
Return builder.ToString()
End Function
<Extension()>
Friend Function ConvertTestSourceTag(testSource As XElement) As String
' Line breaks in XML values are represented as line feeds.
Dim lines = SplitIntoLines(testSource.NormalizedValue, vbCrLf)
Dim importStatements = "Imports System" & vbCrLf &
"Imports System.Collections.Generic" & vbCrLf &
"Imports System.Linq"
Select Case testSource.Name
Case "ClassDeclaration"
Return importStatements & vbCrLf &
"Class C1" & vbCrLf &
SurroundAndJoinLines(lines, " ", vbCrLf) &
"End Class"
Case "StructureDeclaration"
Return importStatements & vbCrLf &
"Structure S" & vbCrLf &
SurroundAndJoinLines(lines, " ", vbCrLf) &
"End Structure"
Case "NamespaceDeclaration"
Return importStatements & vbCrLf &
"Namespace Roslyn" & vbCrLf &
SurroundAndJoinLines(lines, " ", vbCrLf) &
"End Namespace"
Case "InterfaceDeclaration"
Return importStatements & vbCrLf &
"Interface IInterface" & vbCrLf &
SurroundAndJoinLines(lines, " ", vbCrLf) &
"End Interface"
Case "EnumDeclaration"
Return importStatements & vbCrLf &
"Enum Foo" & vbCrLf &
SurroundAndJoinLines(lines, " ", vbCrLf) &
"End Enum"
Case "ModuleDeclaration"
Return importStatements & vbCrLf &
"Module M1" & vbCrLf &
SurroundAndJoinLines(lines, " ", vbCrLf) &
"End Module"
Case "MethodBody"
Return importStatements & vbCrLf &
"Class C1" & vbCrLf &
" Sub Method()" & vbCrLf &
SurroundAndJoinLines(lines, " ", vbCrLf) &
" End Sub" & vbCrLf &
"End Class"
Case "SharedMethodBody"
Return importStatements & vbCrLf &
"Class C1" & vbCrLf &
" Shared Sub Method()" & vbCrLf &
SurroundAndJoinLines(lines, " ", vbCrLf) &
" End Sub" & vbCrLf &
"End Class"
Case "PropertyGetter"
Return importStatements & vbCrLf &
"Class C1" & vbCrLf &
" ReadOnly Property P As Integer" & vbCrLf &
" Get" & vbCrLf &
SurroundAndJoinLines(lines, " ", vbCrLf) &
" End Get" & vbCrLf &
" End Property" & vbCrLf &
"End Class"
Case "PropertyDeclaration"
Return importStatements & vbCrLf &
"Class C1" & vbCrLf &
" Public Property P As String " & vbCrLf &
SurroundAndJoinLines(lines, " ", vbCrLf) &
" End Property" & vbCrLf &
"End Class"
Case "SharedPropertyGetter"
Return importStatements & vbCrLf &
"Class C1" & vbCrLf &
" Shared ReadOnly Property P As Integer" & vbCrLf &
" Get" & vbCrLf &
SurroundAndJoinLines(lines, " ", vbCrLf) &
" End Get" & vbCrLf &
" End Property" & vbCrLf &
"End Class"
Case "CustomEventDeclaration"
Return importStatements & vbCrLf &
"Class C1" & vbCrLf &
" Custom Event TestEvent As EventHandler" & vbCrLf &
SurroundAndJoinLines(lines, " ", vbCrLf) &
" End Event" & vbCrLf &
"End Class"
Case "EventAddHandler"
Return importStatements & vbCrLf &
"Class C1" & vbCrLf &
" Custom Event TestEvent As EventHandler" & vbCrLf &
" AddHandler(value As EventHandler)" & vbCrLf &
SurroundAndJoinLines(lines, " ", vbCrLf) &
" End AddHandler" & vbCrLf &
" RemoveHandler(value As EventHandler)" & vbCrLf &
" End RemoveHandler" & vbCrLf &
" RaiseEvent(sender As Object, e As EventArgs)" & vbCrLf &
" End RaiseEvent" & vbCrLf &
" End Event" & vbCrLf &
"End Class"
Case "SharedEventAddHandler"
Return importStatements & vbCrLf &
"Class C1" & vbCrLf &
" Shared Custom Event TestEvent As EventHandler" & vbCrLf &
" AddHandler(value As EventHandler)" & vbCrLf &
SurroundAndJoinLines(lines, " ", vbCrLf) &
" End AddHandler" & vbCrLf &
" RemoveHandler(value As EventHandler)" & vbCrLf &
" End RemoveHandler" & vbCrLf &
" RaiseEvent(sender As Object, e As EventArgs)" & vbCrLf &
" End RaiseEvent" & vbCrLf &
" End Event" & vbCrLf &
"End Class"
Case "ModuleMethodBody"
Return importStatements & vbCrLf &
"Module M1" & vbCrLf &
" Sub Method()" & vbCrLf &
SurroundAndJoinLines(lines, " ", vbCrLf) &
" End Sub" & vbCrLf &
"End Module"
Case "StructureMethodBody"
Return importStatements & vbCrLf &
"Structure S" & vbCrLf &
" Sub Method()" & vbCrLf &
SurroundAndJoinLines(lines, " ", vbCrLf) &
" End Sub" & vbCrLf &
"End Structure"
Case "File"
Return String.Join(vbCrLf, lines)
Case Else
Throw New ArgumentException("Unexpected testSource XML tag.", "testSource")
End Select
End Function
End Module
End Namespace