Skip to content

Commit 5073fb7

Browse files
committed
Unify logic
1 parent fa496ce commit 5073fb7

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+

2+
// Licensed to the .NET Foundation under one or more agreements.
23
// The .NET Foundation licenses this file to you under the MIT license.
34

45
using System;
@@ -151,11 +152,7 @@ public async Task<ImmutableArray<TextChange>> ExecuteAsync(FormattingContext con
151152
// or skip the opening brace of a lambda definition we insert, but Roslyn might place it on the
152153
// next line. In that case, we can't place it on the next line ourselves because Roslyn doesn't
153154
// adjust the indentation of opening braces of lambdas in that scenario.
154-
if (iFormatted + 1 < formattedCSharpText.Lines.Count &&
155-
formattedCSharpText.Lines[iFormatted + 1] is { Span.Length: > 0 } nextLine &&
156-
nextLine.GetFirstNonWhitespaceOffset() is { } firstNonWhitespace &&
157-
nextLine.Start + firstNonWhitespace == nextLine.End - 1 &&
158-
nextLine.CharAt(firstNonWhitespace) == '{')
155+
if (NextLineIsOnlyAnOpenBrace(formattedCSharpText, iFormatted))
159156
{
160157
iFormatted++;
161158
}
@@ -164,10 +161,7 @@ public async Task<ImmutableArray<TextChange>> ExecuteAsync(FormattingContext con
164161
// it up to the previous line, so we would want to skip the next line in the original document
165162
// in that case. Fortunately its illegal to have `@code {\r\n {` in a Razor file, so there can't
166163
// be false positives here.
167-
if (iOriginal + 1 < changedText.Lines.Count &&
168-
changedText.Lines[iOriginal + 1] is { } nextOriginalLine &&
169-
nextOriginalLine.GetFirstNonWhitespaceOffset() is { } firstChar &&
170-
nextOriginalLine.CharAt(firstChar) == '{')
164+
if (NextLineIsOnlyAnOpenBrace(changedText, iOriginal))
171165
{
172166
iOriginal++;
173167
}
@@ -226,6 +220,13 @@ public async Task<ImmutableArray<TextChange>> ExecuteAsync(FormattingContext con
226220
return SourceTextDiffer.GetMinimalTextChanges(context.SourceText, changedText, DiffKind.Char);
227221
}
228222

223+
private static bool NextLineIsOnlyAnOpenBrace(SourceText text, int lineNumber)
224+
=> lineNumber + 1 < text.Lines.Count &&
225+
text.Lines[lineNumber + 1] is { Span.Length: > 0 } nextLine &&
226+
nextLine.GetFirstNonWhitespaceOffset() is { } firstNonWhitespace &&
227+
nextLine.Start + firstNonWhitespace == nextLine.End - 1 &&
228+
nextLine.CharAt(firstNonWhitespace) == '{';
229+
229230
private async Task<SourceText> FormatCSharpAsync(SourceText generatedCSharpText, RazorFormattingOptions options, CancellationToken cancellationToken)
230231
{
231232
using var helper = new RoslynWorkspaceHelper(_hostServicesProvider);

0 commit comments

Comments
 (0)