Skip to content

Commit 42aa2c6

Browse files
committed
Only walk the changes to determine if there was a problematic formatting difference
1 parent 2d14fbb commit 42aa2c6

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Extensions/SourceTextExtensions.cs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,25 @@ public static string GetSubTextString(this SourceText text, TextSpan span)
6060
return new string(charBuffer, 0, span.Length);
6161
}
6262

63-
public static bool NonWhitespaceContentEquals(this SourceText text, SourceText other)
64-
=> NonWhitespaceContentEquals(text, other, 0, text.Length, 0, other.Length);
63+
public static bool NonWhitespaceContentEquals(this SourceText text, ImmutableArray<TextChange> changes)
64+
{
65+
foreach (var change in changes)
66+
{
67+
if (!text.NonWhitespaceContentEquals(change))
68+
{
69+
return false;
70+
}
71+
}
72+
73+
return true;
74+
}
75+
76+
public static bool NonWhitespaceContentEquals(this SourceText text, TextChange change)
77+
{
78+
var other = change.NewText ?? string.Empty;
79+
80+
return NonWhitespaceContentEquals(text, i => other[i], change.Span.Start, change.Span.End, 0, other.Length);
81+
}
6582

6683
public static bool NonWhitespaceContentEquals(
6784
this SourceText text,
@@ -70,22 +87,36 @@ public static bool NonWhitespaceContentEquals(
7087
int textEnd,
7188
int otherStart,
7289
int otherEnd)
90+
{
91+
return NonWhitespaceContentEquals(text, i => other[i], textStart, textEnd, otherStart, otherEnd);
92+
}
93+
94+
public static bool NonWhitespaceContentEquals(
95+
this SourceText text,
96+
Func<int, char> getOtherCharAt,
97+
int textStart,
98+
int textEnd,
99+
int otherStart,
100+
int otherEnd)
73101
{
74102
var i = textStart;
75103
var j = otherStart;
76104
while (i < textEnd && j < otherEnd)
77105
{
78-
if (char.IsWhiteSpace(text[i]))
106+
var textChar = text[i];
107+
if (char.IsWhiteSpace(textChar))
79108
{
80109
i++;
81110
continue;
82111
}
83-
else if (char.IsWhiteSpace(other[j]))
112+
113+
var otherChar = getOtherCharAt(j);
114+
if (char.IsWhiteSpace(otherChar))
84115
{
85116
j++;
86117
continue;
87118
}
88-
else if (text[i] != other[j])
119+
else if (textChar != otherChar)
89120
{
90121
return false;
91122
}
@@ -99,7 +130,7 @@ public static bool NonWhitespaceContentEquals(
99130
i++;
100131
}
101132

102-
while (j < otherEnd && char.IsWhiteSpace(other[j]))
133+
while (j < otherEnd && char.IsWhiteSpace(getOtherCharAt(j)))
103134
{
104135
j++;
105136
}

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/Passes/FormattingContentValidationPass.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ internal sealed class FormattingContentValidationPass(ILoggerFactory loggerFacto
2323
public Task<bool> IsValidAsync(FormattingContext context, ImmutableArray<TextChange> changes, CancellationToken cancellationToken)
2424
{
2525
var text = context.SourceText;
26-
var changedText = text.WithChanges(changes);
2726

28-
if (!text.NonWhitespaceContentEquals(changedText))
27+
if (!text.NonWhitespaceContentEquals(changes))
2928
{
3029
// Looks like we removed some non-whitespace content as part of formatting. Oops.
3130
// Discard this formatting result.

0 commit comments

Comments
 (0)