-
Notifications
You must be signed in to change notification settings - Fork 4k
/
SyntaxNodeRemover.vb
349 lines (287 loc) · 16.5 KB
/
SyntaxNodeRemover.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
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
340
341
342
343
344
345
346
347
348
349
' Licensed to the .NET Foundation under one or more agreements.
' The .NET Foundation licenses this file to you under the MIT license.
' See the LICENSE file in the project root for more information.
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports Microsoft.CodeAnalysis.Syntax
Imports Microsoft.CodeAnalysis.Text
Imports Microsoft.CodeAnalysis.VisualBasic.Symbols
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax
Public Class SyntaxNodeRemover
Friend Shared Function RemoveNodes(Of TRoot As SyntaxNode)(root As TRoot, nodes As IEnumerable(Of SyntaxNode), options As SyntaxRemoveOptions) As TRoot
Dim nodesToRemove As SyntaxNode() = nodes.ToArray()
If nodesToRemove.Length = 0 Then
Return root
End If
Dim remover = New SyntaxRemover(nodes.ToArray(), options)
Dim result = remover.Visit(root)
Dim residualTrivia = remover.ResidualTrivia
If residualTrivia.Count > 0 Then
result = result.WithTrailingTrivia(result.GetTrailingTrivia().Concat(residualTrivia))
End If
Return DirectCast(result, TRoot)
End Function
Private Class SyntaxRemover
Inherits VisualBasicSyntaxRewriter
Private ReadOnly _nodesToRemove As HashSet(Of SyntaxNode)
Private ReadOnly _options As SyntaxRemoveOptions
Private ReadOnly _searchSpan As TextSpan
Private ReadOnly _residualTrivia As SyntaxTriviaListBuilder
Private _directivesToKeep As HashSet(Of SyntaxNode)
Public Sub New(nodes As SyntaxNode(), options As SyntaxRemoveOptions)
MyBase.New(nodes.Any(Function(n) n.IsPartOfStructuredTrivia()))
Me._nodesToRemove = New HashSet(Of SyntaxNode)(nodes)
Me._options = options
Me._searchSpan = ComputeTotalSpan(nodes)
Me._residualTrivia = SyntaxTriviaListBuilder.Create()
End Sub
Private Shared Function ComputeTotalSpan(nodes As SyntaxNode()) As TextSpan
Dim span0 = nodes(0).FullSpan
Dim start As Integer = span0.Start
Dim [end] As Integer = span0.End
Dim i As Integer = 1
While i < nodes.Length
Dim span = nodes(i).FullSpan
start = Math.Min(start, span.Start)
[end] = Math.Max([end], span.End)
i = i + 1
End While
Return New TextSpan(start, [end] - start)
End Function
Friend ReadOnly Property ResidualTrivia As SyntaxTriviaList
Get
If Me._residualTrivia IsNot Nothing Then
Return Me._residualTrivia.ToList()
Else
Return Nothing
End If
End Get
End Property
Private Sub AddResidualTrivia(trivia As SyntaxTriviaList, Optional requiresNewLine As Boolean = False)
If requiresNewLine Then
AddEndOfLine()
End If
Me._residualTrivia.Add(trivia)
End Sub
Private Sub AddEndOfLine()
If Me._residualTrivia.Count = 0 OrElse Not IsEndOfLine(Me._residualTrivia(Me._residualTrivia.Count - 1)) Then
Me._residualTrivia.Add(SyntaxFactory.CarriageReturnLineFeed)
End If
End Sub
Private Shared Function IsEndOfLine(trivia As SyntaxTrivia) As Boolean
Return trivia.Kind = SyntaxKind.EndOfLineTrivia OrElse trivia.Kind = SyntaxKind.CommentTrivia OrElse trivia.IsDirective
End Function
Private Shared Function HasEndOfLine(trivia As SyntaxTriviaList) As Boolean
Return trivia.Any(Function(t) IsEndOfLine(t))
End Function
Private Function IsForRemoval(node As SyntaxNode) As Boolean
Return Me._nodesToRemove.Contains(node)
End Function
Private Function ShouldVisit(node As SyntaxNode) As Boolean
Return node.FullSpan.IntersectsWith(Me._searchSpan) OrElse (Me._residualTrivia IsNot Nothing AndAlso Me._residualTrivia.Count > 0)
End Function
Public Overrides Function Visit(node As SyntaxNode) As SyntaxNode
Dim result = node
If node IsNot Nothing Then
If Me.IsForRemoval(node) Then
Me.AddTrivia(node)
result = Nothing
ElseIf Me.ShouldVisit(node) Then
result = MyBase.Visit(node)
End If
End If
Return result
End Function
Public Overrides Function VisitToken(token As SyntaxToken) As SyntaxToken
Dim result = token
If Me.VisitIntoStructuredTrivia Then ' only bother visiting trivia if we are removing a node in structured trivia
result = MyBase.VisitToken(token)
End If
' the next token gets the accrued trivia.
If result.Kind <> SyntaxKind.None AndAlso Me._residualTrivia IsNot Nothing AndAlso Me._residualTrivia.Count > 0 Then
Me._residualTrivia.Add(result.LeadingTrivia)
result = result.WithLeadingTrivia(Me._residualTrivia.ToList())
Me._residualTrivia.Clear()
End If
Return result
End Function
' deal with separated lists and removal of associated separators
Public Overrides Function VisitList(Of TNode As SyntaxNode)(list As SeparatedSyntaxList(Of TNode)) As SeparatedSyntaxList(Of TNode)
Dim withSeps = list.GetWithSeparators()
Dim removeNextSeparator = False
Dim alternate As SyntaxNodeOrTokenListBuilder = Nothing
Dim n = withSeps.Count
For i As Integer = 0 To n - 1
Dim item = withSeps(i)
Dim visited As SyntaxNodeOrToken = Nothing
If item.IsToken Then ' separator
If removeNextSeparator Then
removeNextSeparator = False
visited = Nothing
Else
visited = Me.VisitListSeparator(item.AsToken())
End If
Else
Dim node = DirectCast(item.AsNode(), TNode)
If Me.IsForRemoval(node) Then
If alternate Is Nothing Then
alternate = New SyntaxNodeOrTokenListBuilder(n)
alternate.Add(withSeps, 0, i)
End If
Dim nextTokenIsSeparator, nextSeparatorBelongsToNode As Boolean
CommonSyntaxNodeRemover.GetSeparatorInfo(
withSeps, i, SyntaxKind.EndOfLineTrivia,
nextTokenIsSeparator, nextSeparatorBelongsToNode)
If Not nextSeparatorBelongsToNode AndAlso
alternate.Count > 0 AndAlso
alternate(alternate.Count - 1).IsToken Then
Dim separator = alternate(alternate.Count - 1).AsToken()
Me.AddTrivia(separator, node)
alternate.RemoveLast()
ElseIf nextTokenIsSeparator Then
Dim separator = withSeps(i + 1).AsToken()
Me.AddTrivia(node, separator)
removeNextSeparator = True
Else
Me.AddTrivia(node)
End If
visited = Nothing
Else
visited = Me.VisitListElement(node)
End If
End If
If item <> visited AndAlso alternate Is Nothing Then
alternate = New SyntaxNodeOrTokenListBuilder(n)
alternate.Add(withSeps, 0, i)
End If
If alternate IsNot Nothing AndAlso Not visited.IsKind(SyntaxKind.None) Then
alternate.Add(visited)
End If
Next
If alternate IsNot Nothing Then
Return alternate.ToList().AsSeparatedList(Of TNode)()
End If
Return list
End Function
Private Sub AddTrivia(node As SyntaxNode)
If (Me._options And SyntaxRemoveOptions.KeepLeadingTrivia) <> 0 Then
Me.AddResidualTrivia(node.GetLeadingTrivia())
ElseIf (Me._options And SyntaxRemoveOptions.KeepEndOfLine) <> 0 AndAlso HasEndOfLine(node.GetLeadingTrivia()) Then
Me.AddEndOfLine()
End If
If (Me._options And (SyntaxRemoveOptions.KeepDirectives Or SyntaxRemoveOptions.KeepUnbalancedDirectives)) <> 0 Then
Me.AddDirectives(node, GetRemovedSpan(node.Span, node.FullSpan))
End If
If (Me._options And SyntaxRemoveOptions.KeepTrailingTrivia) <> 0 Then
Me.AddResidualTrivia(node.GetTrailingTrivia())
ElseIf (Me._options And SyntaxRemoveOptions.KeepEndOfLine) <> 0 AndAlso HasEndOfLine(node.GetTrailingTrivia()) Then
Me.AddEndOfLine()
End If
If (Me._options And SyntaxRemoveOptions.AddElasticMarker) <> 0 Then
Me.AddResidualTrivia(SyntaxFactory.TriviaList(SyntaxFactory.ElasticMarker))
End If
End Sub
Private Sub AddTrivia(token As SyntaxToken, node As SyntaxNode)
If (Me._options And SyntaxRemoveOptions.KeepLeadingTrivia) <> 0 Then
Me.AddResidualTrivia(token.LeadingTrivia)
Me.AddResidualTrivia(token.TrailingTrivia)
Me.AddResidualTrivia(node.GetLeadingTrivia())
ElseIf (Me._options And SyntaxRemoveOptions.KeepEndOfLine) <> 0 AndAlso
(HasEndOfLine(token.LeadingTrivia) OrElse HasEndOfLine(token.TrailingTrivia) OrElse HasEndOfLine(node.GetLeadingTrivia())) Then
Me.AddEndOfLine()
End If
If (Me._options And (SyntaxRemoveOptions.KeepDirectives Or SyntaxRemoveOptions.KeepUnbalancedDirectives)) <> 0 Then
Dim fullSpan = TextSpan.FromBounds(token.FullSpan.Start, node.FullSpan.End)
Dim span = TextSpan.FromBounds(token.Span.Start, node.Span.End)
Me.AddDirectives(node.Parent, GetRemovedSpan(span, fullSpan))
End If
If (Me._options And SyntaxRemoveOptions.KeepTrailingTrivia) <> 0 Then
Me.AddResidualTrivia(node.GetTrailingTrivia())
ElseIf (Me._options And SyntaxRemoveOptions.KeepEndOfLine) <> 0 AndAlso HasEndOfLine(node.GetTrailingTrivia()) Then
Me.AddEndOfLine()
End If
If (Me._options And SyntaxRemoveOptions.AddElasticMarker) <> 0 Then
Me.AddResidualTrivia(SyntaxFactory.TriviaList(SyntaxFactory.ElasticMarker))
End If
End Sub
Private Sub AddTrivia(node As SyntaxNode, token As SyntaxToken)
If (Me._options And SyntaxRemoveOptions.KeepLeadingTrivia) <> 0 Then
Me.AddResidualTrivia(node.GetLeadingTrivia())
ElseIf (Me._options And SyntaxRemoveOptions.KeepEndOfLine) <> 0 AndAlso HasEndOfLine(node.GetLeadingTrivia()) Then
Me.AddEndOfLine()
End If
If (Me._options And (SyntaxRemoveOptions.KeepDirectives Or SyntaxRemoveOptions.KeepUnbalancedDirectives)) <> 0 Then
Dim fullSpan = TextSpan.FromBounds(node.FullSpan.Start, token.FullSpan.End)
Dim span = TextSpan.FromBounds(node.Span.Start, token.Span.End)
Me.AddDirectives(node.Parent, GetRemovedSpan(span, fullSpan))
End If
If (Me._options And SyntaxRemoveOptions.KeepTrailingTrivia) <> 0 Then
Me.AddResidualTrivia(node.GetTrailingTrivia())
Me.AddResidualTrivia(token.LeadingTrivia)
Me.AddResidualTrivia(token.TrailingTrivia)
ElseIf (Me._options And SyntaxRemoveOptions.KeepEndOfLine) <> 0 AndAlso
(HasEndOfLine(node.GetTrailingTrivia()) OrElse HasEndOfLine(token.LeadingTrivia) OrElse HasEndOfLine(token.TrailingTrivia)) Then
Me.AddEndOfLine()
End If
If (Me._options And SyntaxRemoveOptions.AddElasticMarker) <> 0 Then
Me.AddResidualTrivia(SyntaxFactory.TriviaList(SyntaxFactory.ElasticMarker))
End If
End Sub
Private Function GetRemovedSpan(span As TextSpan, fullSpan As TextSpan) As TextSpan
Dim removedSpan = fullSpan
If (Me._options And SyntaxRemoveOptions.KeepLeadingTrivia) <> 0 Then
removedSpan = TextSpan.FromBounds(span.Start, removedSpan.End)
End If
If (Me._options And SyntaxRemoveOptions.KeepTrailingTrivia) <> 0 Then
removedSpan = TextSpan.FromBounds(removedSpan.Start, span.End)
End If
Return removedSpan
End Function
Private Sub AddDirectives(node As SyntaxNode, span As TextSpan)
If node.ContainsDirectives Then
If Me._directivesToKeep Is Nothing Then
Me._directivesToKeep = New HashSet(Of SyntaxNode)()
Else
Me._directivesToKeep.Clear()
End If
Dim directivesInSpan = node.DescendantTrivia(span, Function(n) n.ContainsDirectives, descendIntoTrivia:=True) _
.Where(Function(tr) tr.IsDirective) _
.Select(Function(tr) DirectCast(tr.GetStructure(), DirectiveTriviaSyntax))
For Each directive In directivesInSpan
If (Me._options And SyntaxRemoveOptions.KeepDirectives) <> 0 Then
Me._directivesToKeep.Add(directive)
ElseIf HasRelatedDirectives(directive) Then
' a balanced directive with respect to a given node has all related directives rooted under that node
Dim relatedDirectives = directive.GetRelatedDirectives()
Dim balanced = relatedDirectives.All(Function(rd) rd.FullSpan.OverlapsWith(span))
If Not balanced Then
' if not fully balanced, all related directives under the node are considered unbalanced.
For Each unbalancedDirective In relatedDirectives.Where(Function(rd) rd.FullSpan.OverlapsWith(span))
Me._directivesToKeep.Add(unbalancedDirective)
Next
End If
End If
If Me._directivesToKeep.Contains(directive) Then
AddResidualTrivia(SyntaxFactory.TriviaList(directive.ParentTrivia), requiresNewLine:=True)
End If
Next
End If
End Sub
Private Shared Function HasRelatedDirectives(directive As DirectiveTriviaSyntax) As Boolean
Select Case directive.Kind
Case SyntaxKind.IfDirectiveTrivia,
SyntaxKind.ElseDirectiveTrivia,
SyntaxKind.ElseIfDirectiveTrivia,
SyntaxKind.EndIfDirectiveTrivia,
SyntaxKind.RegionDirectiveTrivia,
SyntaxKind.EndRegionDirectiveTrivia
Return True
Case Else
Return False
End Select
End Function
End Class
End Class
End Namespace