7
7
using System . Linq ;
8
8
using System . Reflection ;
9
9
using System . Runtime . CompilerServices ;
10
+ using System . Runtime . InteropServices ;
10
11
using System . Text ;
12
+ using System . Text . RegularExpressions ;
11
13
using System . Threading ;
12
14
using System . Threading . Tasks ;
13
15
using Microsoft . AspNetCore . Razor . Language ;
22
24
using Microsoft . CodeAnalysis . Razor . ProjectSystem ;
23
25
using Microsoft . CodeAnalysis . Razor . Workspaces ;
24
26
using Microsoft . CodeAnalysis . Razor . Workspaces . Extensions ;
27
+ using Microsoft . CodeAnalysis . Testing ;
25
28
using Microsoft . CodeAnalysis . Text ;
26
29
using Microsoft . Extensions . Options ;
27
30
using Microsoft . VisualStudio . LanguageServer . Protocol ;
32
35
namespace Microsoft . AspNetCore . Razor . LanguageServer . Semantic ;
33
36
34
37
[ UseExportProvider ]
35
- public class SemanticTokensTest ( ITestOutputHelper testOutput ) : TagHelperServiceTestBase ( testOutput )
38
+ public partial class SemanticTokensTest ( ITestOutputHelper testOutput ) : TagHelperServiceTestBase ( testOutput )
36
39
{
37
40
private readonly Mock < ClientNotifierServiceBase > _languageServer = new ( MockBehavior . Strict ) ;
38
41
private static readonly string s_projectPath = TestProject . GetProjectDirectory ( typeof ( TagHelperServiceTestBase ) , layer : TestProject . Layer . Tooling ) ;
@@ -46,6 +49,15 @@ public class SemanticTokensTest(ITestOutputHelper testOutput) : TagHelperService
46
49
}
47
50
} ;
48
51
52
+ private static Regex s_matchNewLines = MyRegex ( ) ;
53
+
54
+ #if NET
55
+ [ GeneratedRegex ( "\r \n " ) ]
56
+ private static partial Regex MyRegex ( ) ;
57
+ #else
58
+ private static Regex MyRegex ( ) => new Regex ( "\r \n |\r |\n " ) ;
59
+ #endif
60
+
49
61
#if GENERATE_BASELINES
50
62
private bool GenerateBaselines { get ; set ; } = true ;
51
63
#else
@@ -851,31 +863,34 @@ public async Task GetSemanticTokens_CSharp_ExplicitStatement_WithBackground(bool
851
863
public void GetMappedCSharpRanges_MinimalRangeVsSmallDisjointRanges_DisjointRangesAreSmaller ( bool precise )
852
864
{
853
865
var documentText = """
854
- @using System
855
- @functions {
856
- Action<object> abc = @<span></span>;
857
- }
866
+ @[| using System|]
867
+ @functions {[|
868
+ Action<object> abc = |] @<span></span>[| ;
869
+ |] }
858
870
""" ;
859
871
872
+ TestFileMarkupParser . GetSpans ( documentText , out documentText ,
873
+ out ImmutableArray < TextSpan > spans ) ;
874
+
860
875
var codeDocument = CreateCodeDocument ( documentText , isRazorFile : true , DefaultTagHelpers ) ;
861
876
var csharpSourceText = codeDocument . GetCSharpSourceText ( ) ;
862
877
var razorRange = GetRange ( documentText ) ;
863
878
864
879
if ( precise )
865
880
{
866
- var expectedCsharpRangeLengths = new int [ ] { 12 , 27 , 3 } ;
867
881
Assert . True ( RazorSemanticTokensInfoService . TryGetSortedCSharpRanges ( codeDocument , razorRange , out var csharpRanges ) ) ;
868
- Assert . Equal ( 3 , csharpRanges . Length ) ;
882
+ Assert . Equal ( spans . Length , csharpRanges . Length ) ;
869
883
for ( var i = 0 ; i < csharpRanges . Length ; i ++ )
870
884
{
871
885
var csharpRange = csharpRanges [ i ] ;
872
886
var textSpan = csharpRange . ToTextSpan ( csharpSourceText ) ;
873
- Assert . Equal ( expectedCsharpRangeLengths [ i ] , textSpan . Length ) ;
887
+ Assert . Equal ( spans [ i ] . Length , textSpan . Length ) ;
874
888
}
875
889
}
876
890
else
877
891
{
878
- var expectedCsharpRangeLength = 970 ;
892
+ // Note that the expected lengths are different on Windows vs. Unix.
893
+ var expectedCsharpRangeLength = RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) ? 970 : 938 ;
879
894
Assert . True ( RazorSemanticTokensInfoService . TryGetMinimalCSharpRange ( codeDocument , razorRange , out var csharpRange ) ) ;
880
895
var textSpan = csharpRange . ToTextSpan ( csharpSourceText ) ;
881
896
Assert . Equal ( expectedCsharpRangeLength , textSpan . Length ) ;
@@ -1096,7 +1111,14 @@ private string GetBaselineFileContents(string baselineFileName)
1096
1111
return string . Empty ;
1097
1112
}
1098
1113
1099
- return semanticFile . ReadAllText ( ) ;
1114
+ var baselineContents = semanticFile . ReadAllText ( ) ;
1115
+
1116
+ if ( ! RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
1117
+ {
1118
+ baselineContents = s_matchNewLines . Replace ( baselineContents , "\n " ) ;
1119
+ }
1120
+
1121
+ return baselineContents ;
1100
1122
}
1101
1123
1102
1124
private Range [ ] ? GetMappedCSharpRanges ( RazorCodeDocument codeDocument , Range razorRange , bool precise )
0 commit comments