File tree Expand file tree Collapse file tree 2 files changed +49
-1
lines changed
src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/Passes
test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx Expand file tree Collapse file tree 2 files changed +49
-1
lines changed Original file line number Diff line number Diff line change @@ -30,7 +30,9 @@ public virtual async Task<ImmutableArray<TextChange>> ExecuteAsync(FormattingCon
30
30
31
31
if ( changes . Length > 0 )
32
32
{
33
- changedText = originalText . WithChanges ( changes ) ;
33
+ var filteredChanges = FilterIncomingChanges ( changedContext , changes ) ;
34
+
35
+ changedText = originalText . WithChanges ( filteredChanges ) ;
34
36
// Create a new formatting context for the changed razor document.
35
37
changedContext = await context . WithTextAsync ( changedText ) . ConfigureAwait ( false ) ;
36
38
@@ -48,6 +50,27 @@ public virtual async Task<ImmutableArray<TextChange>> ExecuteAsync(FormattingCon
48
50
return changedText . GetTextChangesArray ( originalText ) ;
49
51
}
50
52
53
+ private static ImmutableArray < TextChange > FilterIncomingChanges ( FormattingContext context , ImmutableArray < TextChange > changes )
54
+ {
55
+ var syntaxTree = context . CodeDocument . GetSyntaxTree ( ) ;
56
+
57
+ using var changesToKeep = new PooledArrayBuilder < TextChange > ( capacity : changes . Length ) ;
58
+
59
+ foreach ( var change in changes )
60
+ {
61
+ // Don't keep changes that start inside of a razor comment block.
62
+ var comment = syntaxTree . Root . FindInnermostNode ( change . Span . Start ) ? . FirstAncestorOrSelf < RazorCommentBlockSyntax > ( ) ;
63
+ if ( comment is not null )
64
+ {
65
+ continue ;
66
+ }
67
+
68
+ changesToKeep . Add ( change ) ;
69
+ }
70
+
71
+ return changesToKeep . DrainToImmutable ( ) ;
72
+ }
73
+
51
74
private static ImmutableArray < TextChange > AdjustRazorIndentation ( FormattingContext context )
52
75
{
53
76
// Assume HTML formatter has already run at this point and HTML is relatively indented correctly.
Original file line number Diff line number Diff line change @@ -705,6 +705,31 @@ await RunFormattingTestAsync(
705
705
fileKind : FileKinds . Legacy ) ;
706
706
}
707
707
708
+ [ Fact ]
709
+ public async Task MultiLineComment_WithinHtml ( )
710
+ {
711
+ await RunFormattingTestAsync (
712
+ input : """
713
+ <div>
714
+ @* <div>
715
+ This comment's opening at-star will be aligned, and the
716
+ indentation of the rest of its lines will be preserved.
717
+ </div>
718
+ *@
719
+ </div>
720
+ """ ,
721
+ expected : """
722
+ <div>
723
+ @* <div>
724
+ This comment's opening at-star will be aligned, and the
725
+ indentation of the rest of its lines will be preserved.
726
+ </div>
727
+ *@
728
+ </div>
729
+ """ ,
730
+ fileKind : FileKinds . Legacy ) ;
731
+ }
732
+
708
733
// Regression prevention tests:
709
734
[ Fact ]
710
735
public async Task Using ( )
You can’t perform that action at this time.
0 commit comments