Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(DocumentFormattingParams

var options = RazorFormattingOptions.From(request.Options, _optionsMonitor.CurrentValue.CodeBlockBraceOnNextLine);

var htmlChanges = await _htmlFormatter.GetDocumentFormattingEditsAsync(documentContext.Snapshot, documentContext.Uri, request.Options, cancellationToken).ConfigureAwait(false);
if (await _htmlFormatter.GetDocumentFormattingEditsAsync(
documentContext.Snapshot,
documentContext.Uri,
request.Options,
cancellationToken).ConfigureAwait(false) is not { } htmlChanges)
{
return null;
}

var changes = await _razorFormattingService.GetDocumentFormattingChangesAsync(documentContext, htmlChanges, span: null, options, cancellationToken).ConfigureAwait(false);

return [.. changes.Select(codeDocument.Source.Text.GetTextEdit)];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,17 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(DocumentOnTypeFormatting
}
else if (triggerCharacterKind == RazorLanguageKind.Html)
{
var htmlChanges = await _htmlFormatter.GetOnTypeFormattingEditsAsync(documentContext.Snapshot, documentContext.Uri, request.Position, request.Character, request.Options, cancellationToken).ConfigureAwait(false);
if (await _htmlFormatter.GetOnTypeFormattingEditsAsync(
documentContext.Snapshot,
documentContext.Uri,
request.Position,
request.Character,
request.Options,
cancellationToken).ConfigureAwait(false) is not { } htmlChanges)
{
return null;
}

formattedChanges = await _razorFormattingService.GetHtmlOnTypeFormattingChangesAsync(documentContext, htmlChanges, options, hostDocumentIndex, request.Character[0], cancellationToken).ConfigureAwait(false);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,15 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(DocumentRangeFormattingP

var options = RazorFormattingOptions.From(request.Options, _optionsMonitor.CurrentValue.CodeBlockBraceOnNextLine);

var htmlChanges = await _htmlFormatter.GetDocumentFormattingEditsAsync(documentContext.Snapshot, documentContext.Uri, request.Options, cancellationToken).ConfigureAwait(false);
if (await _htmlFormatter.GetDocumentFormattingEditsAsync(
documentContext.Snapshot,
documentContext.Uri,
request.Options,
cancellationToken).ConfigureAwait(false) is not { } htmlChanges)
{
return null;
}

var changes = await _razorFormattingService.GetDocumentFormattingChangesAsync(documentContext, htmlChanges, request.Range.ToLinePositionSpan(), options, cancellationToken).ConfigureAwait(false);

return [.. changes.Select(codeDocument.Source.Text.GetTextEdit)];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal sealed class HtmlFormatter(
{
private readonly IClientConnection _clientConnection = clientConnection;

public async Task<ImmutableArray<TextChange>> GetDocumentFormattingEditsAsync(
public async Task<ImmutableArray<TextChange>?> GetDocumentFormattingEditsAsync(
IDocumentSnapshot documentSnapshot,
Uri uri,
FormattingOptions options,
Expand All @@ -44,14 +44,14 @@ public async Task<ImmutableArray<TextChange>> GetDocumentFormattingEditsAsync(

if (result?.Edits is null)
{
return [];
return null;
}

var sourceText = await documentSnapshot.GetTextAsync(cancellationToken).ConfigureAwait(false);
return result.Edits.SelectAsArray(sourceText.GetTextChange);
}

public async Task<ImmutableArray<TextChange>> GetOnTypeFormattingEditsAsync(
public async Task<ImmutableArray<TextChange>?> GetOnTypeFormattingEditsAsync(
IDocumentSnapshot documentSnapshot,
Uri uri,
Position position,
Expand All @@ -75,7 +75,7 @@ public async Task<ImmutableArray<TextChange>> GetOnTypeFormattingEditsAsync(

if (result?.Edits is null)
{
return [];
return null;
}

var sourceText = await documentSnapshot.GetTextAsync(cancellationToken).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.Formatting;

internal interface IHtmlFormatter
{
Task<ImmutableArray<TextChange>> GetDocumentFormattingEditsAsync(IDocumentSnapshot documentSnapshot, Uri uri, FormattingOptions options, CancellationToken cancellationToken);
Task<ImmutableArray<TextChange>> GetOnTypeFormattingEditsAsync(IDocumentSnapshot documentSnapshot, Uri uri, Position position, string triggerCharacter, FormattingOptions options, CancellationToken cancellationToken);
Task<ImmutableArray<TextChange>?> GetDocumentFormattingEditsAsync(IDocumentSnapshot documentSnapshot, Uri uri, FormattingOptions options, CancellationToken cancellationToken);
Task<ImmutableArray<TextChange>?> GetOnTypeFormattingEditsAsync(IDocumentSnapshot documentSnapshot, Uri uri, Position position, string triggerCharacter, FormattingOptions options, CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal partial class RazorCustomMessageTarget
{
// Called by the Razor Language Server to invoke a razor/htmlFormatting request on the virtual Html buffer.
[JsonRpcMethod(CustomMessageNames.RazorHtmlFormattingEndpoint, UseSingleObjectParameterDeserialization = true)]
public async Task<RazorDocumentFormattingResponse> HtmlFormattingAsync(RazorDocumentFormattingParams request, CancellationToken cancellationToken)
public async Task<RazorDocumentFormattingResponse?> HtmlFormattingAsync(RazorDocumentFormattingParams request, CancellationToken cancellationToken)
{
var response = new RazorDocumentFormattingResponse() { Edits = Array.Empty<TextEdit>() };

Expand All @@ -32,7 +32,7 @@ public async Task<RazorDocumentFormattingResponse> HtmlFormattingAsync(RazorDocu
if (!synchronized || htmlDocument is null)
{
Debug.Fail("RangeFormatting not synchronized.");
return response;
return null;
}

var projectedUri = htmlDocument.Uri;
Expand All @@ -51,14 +51,19 @@ public async Task<RazorDocumentFormattingResponse> HtmlFormattingAsync(RazorDocu
formattingParams,
cancellationToken).ConfigureAwait(false);

response.Edits = edits?.Response ?? Array.Empty<TextEdit>();
if (edits?.Response is null)
{
return null;
}

response.Edits = edits.Response;

return response;
}

// Called by the Razor Language Server to invoke a razor/htmlOnTypeFormatting request on the virtual Html buffer.
[JsonRpcMethod(CustomMessageNames.RazorHtmlOnTypeFormattingEndpoint, UseSingleObjectParameterDeserialization = true)]
public async Task<RazorDocumentFormattingResponse> HtmlOnTypeFormattingAsync(RazorDocumentOnTypeFormattingParams request, CancellationToken cancellationToken)
public async Task<RazorDocumentFormattingResponse?> HtmlOnTypeFormattingAsync(RazorDocumentOnTypeFormattingParams request, CancellationToken cancellationToken)
{
var response = new RazorDocumentFormattingResponse() { Edits = Array.Empty<TextEdit>() };

Expand All @@ -69,7 +74,7 @@ public async Task<RazorDocumentFormattingResponse> HtmlOnTypeFormattingAsync(Raz

if (!synchronized || htmlDocument is null)
{
return response;
return null;
}

var formattingParams = new DocumentOnTypeFormattingParams()
Expand All @@ -88,7 +93,12 @@ public async Task<RazorDocumentFormattingResponse> HtmlOnTypeFormattingAsync(Raz
formattingParams,
cancellationToken).ConfigureAwait(false);

response.Edits = edits?.Response ?? Array.Empty<TextEdit>();
if (edits?.Response is null)
{
return null;
}

response.Edits = edits.Response;

return response;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private async Task RunFormattingTestInternalAsync(string input, string expected,
var htmlChanges = await htmlFormatter.GetDocumentFormattingEditsAsync(documentSnapshot, uri, options, DisposalToken);

// Act
var changes = await formattingService.GetDocumentFormattingChangesAsync(documentContext, htmlChanges, range, razorOptions, DisposalToken);
var changes = await formattingService.GetDocumentFormattingChangesAsync(documentContext, htmlChanges.AssumeNotNull(), range, razorOptions, DisposalToken);

// Assert
var edited = source.WithChanges(changes);
Expand Down Expand Up @@ -182,7 +182,7 @@ private protected async Task RunOnTypeFormattingTestAsync(

var htmlFormatter = new HtmlFormatter(client);
var htmlChanges = await htmlFormatter.GetDocumentFormattingEditsAsync(documentSnapshot, uri, options, DisposalToken);
changes = await formattingService.GetHtmlOnTypeFormattingChangesAsync(documentContext, htmlChanges, razorOptions, hostDocumentIndex: positionAfterTrigger, triggerCharacter: triggerCharacter, DisposalToken);
changes = await formattingService.GetHtmlOnTypeFormattingChangesAsync(documentContext, htmlChanges.AssumeNotNull(), razorOptions, hostDocumentIndex: positionAfterTrigger, triggerCharacter: triggerCharacter, DisposalToken);
}

// Assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.ProjectSystem;
using Microsoft.AspNetCore.Razor.Threading;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServer.Protocol;

namespace Microsoft.AspNetCore.Razor.LanguageServer.Formatting;

internal class TestHtmlFormatter : IHtmlFormatter
{
public Task<ImmutableArray<TextChange>> GetDocumentFormattingEditsAsync(IDocumentSnapshot documentSnapshot, Uri uri, FormattingOptions options, CancellationToken cancellationToken)
public Task<ImmutableArray<TextChange>?> GetDocumentFormattingEditsAsync(IDocumentSnapshot documentSnapshot, Uri uri, FormattingOptions options, CancellationToken cancellationToken)
{
return SpecializedTasks.EmptyImmutableArray<TextChange>();
return Task.FromResult<ImmutableArray<TextChange>?>([]);
}

public Task<ImmutableArray<TextChange>> GetOnTypeFormattingEditsAsync(IDocumentSnapshot documentSnapshot, Uri uri, Position position, string triggerCharacter, FormattingOptions options, CancellationToken cancellationToken)
public Task<ImmutableArray<TextChange>?> GetOnTypeFormattingEditsAsync(IDocumentSnapshot documentSnapshot, Uri uri, Position position, string triggerCharacter, FormattingOptions options, CancellationToken cancellationToken)
{
return SpecializedTasks.EmptyImmutableArray<TextChange>();
return Task.FromResult<ImmutableArray<TextChange>?>([]);
}
}