-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRangeSupport.vb
More file actions
66 lines (54 loc) · 3.08 KB
/
RangeSupport.vb
File metadata and controls
66 lines (54 loc) · 3.08 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
' 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 Microsoft.CodeAnalysis
Imports Microsoft.CodeAnalysis.Classification
Imports Microsoft.CodeAnalysis.CSharp.Formatting
Imports Microsoft.CodeAnalysis.Formatting
Imports Microsoft.CodeAnalysis.Options
Imports Microsoft.CodeAnalysis.Text
Public Module RangeSupport
Private Iterator Function FillGaps(text As SourceText, ranges As IEnumerable(Of Range)) As IEnumerable(Of Range)
Const whitespaceClassification As String = Nothing
Dim current As Integer = 0
Dim previous As Range = Nothing
For Each range As Range In ranges
Dim start As Integer = range.TextSpan.Start
If start > current Then
Yield New Range(whitespaceClassification, TextSpan.FromBounds(current, start), text)
End If
If previous Is Nothing OrElse range.TextSpan <> previous.TextSpan Then
Yield range
End If
previous = range
current = range.TextSpan.End
Next
If current < text.Length Then
Yield New Range(whitespaceClassification, TextSpan.FromBounds(current, text.Length), text)
End If
End Function
Public Function GetClassifiedRanges(SourceCode As String, Language As String) As IEnumerable(Of Range)
Using workspace As New AdhocWorkspace()
Dim solution As Solution = workspace.CurrentSolution
Dim document As Document
If Language = LanguageNames.CSharp Then
Dim project As Project = solution.AddProject("projectName", "assemblyName", LanguageNames.CSharp)
document = project.AddDocument("name.cs", SourceCode)
Dim CSharpOptions As OptionSet = workspace.Options
CSharpOptions = CSharpOptions.WithChangedOption(CSharpFormattingOptions.NewLinesForBracesInMethods, value:=True)
CSharpOptions = CSharpOptions.WithChangedOption(CSharpFormattingOptions.NewLinesForBracesInProperties, value:=True)
Else
Dim VisualBasicOptions As OptionSet = workspace.Options
Dim project As Project = solution.AddProject("projectName", "assemblyName", LanguageNames.VisualBasic)
document = project.AddDocument("name.vb", SourceCode)
End If
document = Formatter.FormatAsync(document).Result
Dim text As SourceText = document.GetTextAsync().Result
Dim classifiedSpans As IEnumerable(Of ClassifiedSpan) = Classifier.GetClassifiedSpansAsync(document, TextSpan.FromBounds(0, text.Length)).Result
Dim ranges As IEnumerable(Of Range) = From span As ClassifiedSpan In classifiedSpans
Select New Range(span, text.GetSubText(span.TextSpan).ToString())
' Whitespace isn't classified so fill in ranges for whitespace.
Return FillGaps(text, ranges)
End Using
End Function
End Module