Convert HtmlFormatter to ImmutableArray<TextChange>

All callers did the conversion anyway
This commit is contained in:
David Wengier 2024-09-09 11:27:43 +10:00
Родитель b268c61db8
Коммит 4aeb51937b
7 изменённых файлов: 32 добавлений и 20 удалений

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

@ -56,8 +56,7 @@ internal class DocumentFormattingEndpoint(
var options = RazorFormattingOptions.From(request.Options, _optionsMonitor.CurrentValue.CodeBlockBraceOnNextLine);
var htmlEdits = await _htmlFormatter.GetDocumentFormattingEditsAsync(documentContext.Snapshot, documentContext.Uri, request.Options, cancellationToken).ConfigureAwait(false);
var htmlChanges = htmlEdits.SelectAsArray(codeDocument.Source.Text.GetTextChange);
var htmlChanges = await _htmlFormatter.GetDocumentFormattingEditsAsync(documentContext.Snapshot, documentContext.Uri, request.Options, cancellationToken).ConfigureAwait(false);
var changes = await _razorFormattingService.GetDocumentFormattingChangesAsync(documentContext, htmlChanges, span: null, options, cancellationToken).ConfigureAwait(false);
return [.. changes.Select(codeDocument.Source.Text.GetTextEdit)];

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

@ -107,8 +107,7 @@ internal class DocumentOnTypeFormattingEndpoint(
}
else if (triggerCharacterKind == RazorLanguageKind.Html)
{
var htmlEdits = await _htmlFormatter.GetOnTypeFormattingEditsAsync(documentContext.Snapshot, documentContext.Uri, request.Position, request.Character, request.Options, cancellationToken).ConfigureAwait(false);
var htmlChanges = htmlEdits.SelectAsArray(sourceText.GetTextChange);
var htmlChanges = await _htmlFormatter.GetOnTypeFormattingEditsAsync(documentContext.Snapshot, documentContext.Uri, request.Position, request.Character, request.Options, cancellationToken).ConfigureAwait(false);
formattedChanges = await _razorFormattingService.GetHtmlOnTypeFormattingChangesAsync(documentContext, htmlChanges, options, hostDocumentIndex, request.Character[0], cancellationToken).ConfigureAwait(false);
}
else

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

@ -56,8 +56,7 @@ internal class DocumentRangeFormattingEndpoint(
var options = RazorFormattingOptions.From(request.Options, _optionsMonitor.CurrentValue.CodeBlockBraceOnNextLine);
var htmlEdits = await _htmlFormatter.GetDocumentFormattingEditsAsync(documentContext.Snapshot, documentContext.Uri, request.Options, cancellationToken).ConfigureAwait(false);
var htmlChanges = htmlEdits.SelectAsArray(codeDocument.Source.Text.GetTextChange);
var htmlChanges = await _htmlFormatter.GetDocumentFormattingEditsAsync(documentContext.Snapshot, documentContext.Uri, request.Options, cancellationToken).ConfigureAwait(false);
var changes = await _razorFormattingService.GetDocumentFormattingChangesAsync(documentContext, htmlChanges, request.Range.ToLinePositionSpan(), options, cancellationToken).ConfigureAwait(false);
return [.. changes.Select(codeDocument.Source.Text.GetTextEdit)];

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

@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
@ -20,7 +21,7 @@ internal sealed class HtmlFormatter(
{
private readonly IClientConnection _clientConnection = clientConnection;
public async Task<TextEdit[]> GetDocumentFormattingEditsAsync(
public async Task<ImmutableArray<TextChange>> GetDocumentFormattingEditsAsync(
IDocumentSnapshot documentSnapshot,
Uri uri,
FormattingOptions options,
@ -41,10 +42,16 @@ internal sealed class HtmlFormatter(
@params,
cancellationToken).ConfigureAwait(false);
return result?.Edits ?? [];
if (result?.Edits is null)
{
return [];
}
var sourceText = await documentSnapshot.GetTextAsync().ConfigureAwait(false);
return result.Edits.SelectAsArray(sourceText.GetTextChange);
}
public async Task<TextEdit[]> GetOnTypeFormattingEditsAsync(
public async Task<ImmutableArray<TextChange>> GetOnTypeFormattingEditsAsync(
IDocumentSnapshot documentSnapshot,
Uri uri,
Position position,
@ -66,7 +73,13 @@ internal sealed class HtmlFormatter(
@params,
cancellationToken).ConfigureAwait(false);
return result?.Edits ?? [];
if (result?.Edits is null)
{
return [];
}
var sourceText = await documentSnapshot.GetTextAsync().ConfigureAwait(false);
return result.Edits.SelectAsArray(sourceText.GetTextChange);
}
/// <summary>

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

@ -2,15 +2,17 @@
// Licensed under the MIT license. See License.txt in the project root for license information.
using System;
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServer.Protocol;
namespace Microsoft.AspNetCore.Razor.LanguageServer.Formatting;
internal interface IHtmlFormatter
{
Task<TextEdit[]> GetDocumentFormattingEditsAsync(IDocumentSnapshot documentSnapshot, Uri uri, FormattingOptions options, CancellationToken cancellationToken);
Task<TextEdit[]> 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);
}

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

@ -98,8 +98,7 @@ public class FormattingTestBase : RazorToolingIntegrationTestBase
client.AddCodeDocument(codeDocument);
var htmlFormatter = new HtmlFormatter(client);
var htmlEdits = await htmlFormatter.GetDocumentFormattingEditsAsync(documentSnapshot, uri, options, DisposalToken);
var htmlChanges = htmlEdits.SelectAsArray(source.GetTextChange);
var htmlChanges = await htmlFormatter.GetDocumentFormattingEditsAsync(documentSnapshot, uri, options, DisposalToken);
// Act
var changes = await formattingService.GetDocumentFormattingChangesAsync(documentContext, htmlChanges, range, razorOptions, DisposalToken);
@ -164,8 +163,7 @@ public class FormattingTestBase : RazorToolingIntegrationTestBase
client.AddCodeDocument(codeDocument);
var htmlFormatter = new HtmlFormatter(client);
var htmlEdits = await htmlFormatter.GetDocumentFormattingEditsAsync(documentSnapshot, uri, options, DisposalToken);
var htmlChanges = htmlEdits.SelectAsArray(razorSourceText.GetTextChange);
var htmlChanges = await htmlFormatter.GetDocumentFormattingEditsAsync(documentSnapshot, uri, options, DisposalToken);
changes = await formattingService.GetHtmlOnTypeFormattingChangesAsync(documentContext, htmlChanges, razorOptions, hostDocumentIndex: positionAfterTrigger, triggerCharacter: triggerCharacter, DisposalToken);
}

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

@ -2,23 +2,25 @@
// Licensed under the MIT license. See License.txt in the project root for license information.
using System;
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.Threading;
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServer.Protocol;
namespace Microsoft.AspNetCore.Razor.LanguageServer.Formatting;
internal class TestHtmlFormatter : IHtmlFormatter
{
public Task<TextEdit[]> GetDocumentFormattingEditsAsync(IDocumentSnapshot documentSnapshot, Uri uri, FormattingOptions options, CancellationToken cancellationToken)
public Task<ImmutableArray<TextChange>> GetDocumentFormattingEditsAsync(IDocumentSnapshot documentSnapshot, Uri uri, FormattingOptions options, CancellationToken cancellationToken)
{
return SpecializedTasks.EmptyArray<TextEdit>();
return SpecializedTasks.EmptyImmutableArray<TextChange>();
}
public Task<TextEdit[]> 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.EmptyArray<TextEdit>();
return SpecializedTasks.EmptyImmutableArray<TextChange>();
}
}