David Wengier 2024-02-27 07:53:50 +11:00 коммит произвёл GitHub
Родитель e3c48b6bc0 c8f53de4f9
Коммит 3d2c5b1353
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
3 изменённых файлов: 72 добавлений и 0 удалений

Просмотреть файл

@ -554,6 +554,7 @@ internal abstract class CSharpFormattingPassBase : FormattingPassBase
// Open brace is a child of the C# code block that is the directive itself
if (owner is RazorMetaCodeSyntax &&
owner.Parent is CSharpCodeBlockSyntax codeBlock &&
codeBlock.Children.Count > 3 &&
owner == codeBlock.Children[3] &&
// CSharpCodeBlock -> RazorDirectiveBody -> RazorDirective
codeBlock.Parent?.Parent is RazorDirectiveSyntax directive2 &&

Просмотреть файл

@ -130,10 +130,14 @@ internal class CSharpOnTypeFormattingPass : CSharpFormattingPassBase
// Find the lines that were affected by these edits.
var originalText = codeDocument.GetSourceText();
_logger.LogTestOnly("Original text:\r\n{originalText}", originalText);
var changes = filteredEdits.Select(e => e.ToTextChange(originalText));
// Apply the format on type edits sent over by the client.
var formattedText = ApplyChangesAndTrackChange(originalText, changes, out _, out var spanAfterFormatting);
_logger.LogTestOnly("After C# changes:\r\n{formattedText}", formattedText);
var changedContext = await context.WithTextAsync(formattedText).ConfigureAwait(false);
var rangeAfterFormatting = spanAfterFormatting.ToRange(formattedText);
@ -142,6 +146,8 @@ internal class CSharpOnTypeFormattingPass : CSharpFormattingPassBase
// We make an optimistic attempt at fixing corner cases.
var cleanupChanges = CleanupDocument(changedContext, rangeAfterFormatting);
var cleanedText = formattedText.WithChanges(cleanupChanges);
_logger.LogTestOnly("After CleanupDocument:\r\n{cleanedText}", cleanedText);
changedContext = await changedContext.WithTextAsync(cleanedText).ConfigureAwait(false);
cancellationToken.ThrowIfCancellationRequested();
@ -209,6 +215,8 @@ internal class CSharpOnTypeFormattingPass : CSharpFormattingPassBase
{
// Apply the edits that modify indentation.
cleanedText = cleanedText.WithChanges(indentationChanges);
_logger.LogTestOnly("After AdjustIndentationAsync:\r\n{cleanedText}", cleanedText);
}
// Now that we have made all the necessary changes to the document. Let's diff the original vs final version and return the diff.
@ -511,6 +519,16 @@ internal class CSharpOnTypeFormattingPass : CSharpFormattingPassBase
return;
}
if (owner is MarkupTagHelperAttributeSyntax { TagHelperAttributeInfo.Bound: true } or
MarkupTagHelperDirectiveAttributeSyntax { TagHelperAttributeInfo.Bound: true } or
MarkupMinimizedTagHelperAttributeSyntax { TagHelperAttributeInfo.Bound: true } or
MarkupMinimizedTagHelperDirectiveAttributeSyntax { TagHelperAttributeInfo.Bound: true })
{
// Special case, we don't want to add a line break at the end of a component attribute. They are technically
// C#, for features like GTD and FAR, but we consider them Html for formatting
return;
}
var contentStartOffset = text.Lines[mappingEndLineIndex].GetFirstNonWhitespaceOffset(sourceMappingRange.End.Character);
if (contentStartOffset is null)
{

Просмотреть файл

@ -12,6 +12,59 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.Formatting;
public class CodeDirectiveOnTypeFormattingTest(ITestOutputHelper testOutput) : FormattingTestBase(testOutput)
{
[Fact]
public async Task FormatsIfStatementInComponent()
{
await RunOnTypeFormattingTestAsync(
input: """
@using Microsoft.AspNetCore.Components.Forms
@if (true)
{
<InputSelect TValue="string" DisplayName="asdf">
<option>asdf</option>
</InputSelect>
}$$
""",
expected: """
@using Microsoft.AspNetCore.Components.Forms
@if (true)
{
<InputSelect TValue="string" DisplayName="asdf">
<option>asdf</option>
</InputSelect>
}
""",
triggerCharacter: '}');
}
[Fact]
public async Task FormatsIfStatementInComponent2()
{
await RunOnTypeFormattingTestAsync(
input: """
@using Microsoft.AspNetCore.Components.Forms
@if (true)
{<InputSelect TValue="string" DisplayName="asdf">
<option>asdf</option>
</InputSelect>
}$$
""",
expected: """
@using Microsoft.AspNetCore.Components.Forms
@if (true)
{
<InputSelect TValue="string" DisplayName="asdf">
<option>asdf</option>
</InputSelect>
}
""",
triggerCharacter: '}');
}
[Fact]
public async Task CloseCurly_Class_SingleLineAsync()
{