зеркало из https://github.com/dotnet/razor.git
Separate formatting methods into specific use cases
This commit is contained in:
Родитель
86d654a1cd
Коммит
49ba3817e4
|
@ -124,14 +124,7 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.CodeActions
|
|||
|
||||
// Remaps the text edits from the generated C# to the razor file,
|
||||
// as well as applying appropriate formatting.
|
||||
var formattedEdits = await _razorFormattingService.ApplyFormattedEditsAsync(
|
||||
csharpParams.RazorFileUri,
|
||||
documentSnapshot,
|
||||
RazorLanguageKind.CSharp,
|
||||
csharpTextEdits,
|
||||
s_defaultFormattingOptions,
|
||||
cancellationToken,
|
||||
bypassValidationPasses: true);
|
||||
var formattedEdits = await _razorFormattingService.FormatCodeActionAsync(csharpParams.RazorFileUri, documentSnapshot, RazorLanguageKind.CSharp, csharpTextEdits, s_defaultFormattingOptions, cancellationToken);
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
|
|
|
@ -90,15 +90,25 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.Formatting
|
|||
return filteredEdits;
|
||||
}
|
||||
|
||||
public override async Task<TextEdit[]> ApplyFormattedEditsAsync(
|
||||
public override Task<TextEdit[]> FormatOnTypeAsync(DocumentUri uri, DocumentSnapshot documentSnapshot, RazorLanguageKind kind, TextEdit[] formattedEdits, FormattingOptions options, CancellationToken cancellationToken)
|
||||
=> ApplyFormattedEditsAsync(uri, documentSnapshot, kind, formattedEdits, options, bypassValidationPasses: false, collapseEdits: false, automaticallyAddUsings: false, cancellationToken: cancellationToken);
|
||||
|
||||
public override Task<TextEdit[]> FormatCodeActionAsync(DocumentUri uri, DocumentSnapshot documentSnapshot, RazorLanguageKind kind, TextEdit[] formattedEdits, FormattingOptions options, CancellationToken cancellationToken)
|
||||
=> ApplyFormattedEditsAsync(uri, documentSnapshot, kind, formattedEdits, options, bypassValidationPasses: true, collapseEdits: false, automaticallyAddUsings: true, cancellationToken: cancellationToken);
|
||||
|
||||
public override Task<TextEdit[]> FormatSnippetAsync(DocumentUri uri, DocumentSnapshot documentSnapshot, RazorLanguageKind kind, TextEdit[] formattedEdits, FormattingOptions options, CancellationToken cancellationToken)
|
||||
=> ApplyFormattedEditsAsync(uri, documentSnapshot, kind, formattedEdits, options, bypassValidationPasses: true, collapseEdits: true, automaticallyAddUsings: false, cancellationToken: cancellationToken);
|
||||
|
||||
private async Task<TextEdit[]> ApplyFormattedEditsAsync(
|
||||
DocumentUri uri,
|
||||
DocumentSnapshot documentSnapshot,
|
||||
RazorLanguageKind kind,
|
||||
TextEdit[] formattedEdits,
|
||||
FormattingOptions options,
|
||||
CancellationToken cancellationToken,
|
||||
bool bypassValidationPasses = false,
|
||||
bool collapseEdits = false)
|
||||
bool bypassValidationPasses,
|
||||
bool collapseEdits,
|
||||
bool automaticallyAddUsings,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (kind == RazorLanguageKind.Html)
|
||||
{
|
||||
|
@ -111,7 +121,7 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.Formatting
|
|||
collapseEdits |= formattedEdits.Length == 1;
|
||||
|
||||
var codeDocument = await documentSnapshot.GetGeneratedOutputAsync();
|
||||
using var context = FormattingContext.Create(uri, documentSnapshot, codeDocument, options, _workspaceFactory, isFormatOnType: true);
|
||||
using var context = FormattingContext.Create(uri, documentSnapshot, codeDocument, options, _workspaceFactory, isFormatOnType: true, automaticallyAddUsings: automaticallyAddUsings);
|
||||
var result = new FormattingResult(formattedEdits, kind);
|
||||
|
||||
foreach (var pass in _formattingPasses)
|
||||
|
|
|
@ -83,6 +83,8 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.Formatting
|
|||
|
||||
public bool IsFormatOnType { get; private set; }
|
||||
|
||||
public bool AutomaticallyAddUsings { get; private set; }
|
||||
|
||||
public Range Range { get; private set; } = null!;
|
||||
|
||||
/// <summary>A Dictionary of int (line number) to IndentationContext.</summary>
|
||||
|
@ -248,33 +250,9 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.Formatting
|
|||
RazorCodeDocument codeDocument,
|
||||
FormattingOptions options,
|
||||
AdhocWorkspaceFactory workspaceFactory,
|
||||
bool isFormatOnType = false)
|
||||
bool isFormatOnType = false,
|
||||
bool automaticallyAddUsings = false)
|
||||
{
|
||||
if (uri is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(uri));
|
||||
}
|
||||
|
||||
if (originalSnapshot is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(originalSnapshot));
|
||||
}
|
||||
|
||||
if (codeDocument is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(codeDocument));
|
||||
}
|
||||
|
||||
if (options is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(options));
|
||||
}
|
||||
|
||||
if (workspaceFactory is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(workspaceFactory));
|
||||
}
|
||||
|
||||
var syntaxTree = codeDocument.GetSyntaxTree();
|
||||
var formattingSpans = syntaxTree.GetFormattingSpans();
|
||||
|
||||
|
@ -285,6 +263,7 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.Formatting
|
|||
CodeDocument = codeDocument,
|
||||
Options = options,
|
||||
IsFormatOnType = isFormatOnType,
|
||||
AutomaticallyAddUsings = automaticallyAddUsings,
|
||||
FormattingSpans = formattingSpans
|
||||
};
|
||||
|
||||
|
|
|
@ -265,8 +265,7 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.Formatting
|
|||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
var formattedEdits = await _razorFormattingService.ApplyFormattedEditsAsync(
|
||||
request.TextDocument.Uri, documentSnapshot, triggerCharacterKind, textEdits, request.Options, cancellationToken).ConfigureAwait(false);
|
||||
var formattedEdits = await _razorFormattingService.FormatOnTypeAsync(request.TextDocument.Uri, documentSnapshot, triggerCharacterKind, textEdits, request.Options, cancellationToken).ConfigureAwait(false);
|
||||
if (formattedEdits.Length == 0)
|
||||
{
|
||||
_logger.LogInformation("No formatting changes were necessary");
|
||||
|
|
|
@ -21,14 +21,28 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.Formatting
|
|||
FormattingOptions options,
|
||||
CancellationToken cancellationToken);
|
||||
|
||||
public abstract Task<TextEdit[]> ApplyFormattedEditsAsync(
|
||||
public abstract Task<TextEdit[]> FormatOnTypeAsync(
|
||||
DocumentUri uri,
|
||||
DocumentSnapshot documentSnapshot,
|
||||
RazorLanguageKind kind,
|
||||
TextEdit[] formattedEdits,
|
||||
FormattingOptions options,
|
||||
CancellationToken cancellationToken);
|
||||
|
||||
public abstract Task<TextEdit[]> FormatCodeActionAsync(
|
||||
DocumentUri uri,
|
||||
DocumentSnapshot documentSnapshot,
|
||||
RazorLanguageKind kind,
|
||||
TextEdit[] formattedEdits,
|
||||
FormattingOptions options,
|
||||
CancellationToken cancellationToken,
|
||||
bool bypassValidationPasses = false,
|
||||
bool collapseEdits = false);
|
||||
CancellationToken cancellationToken);
|
||||
|
||||
public abstract Task<TextEdit[]> FormatSnippetAsync(
|
||||
DocumentUri uri,
|
||||
DocumentSnapshot documentSnapshot,
|
||||
RazorLanguageKind kind,
|
||||
TextEdit[] formattedEdits,
|
||||
FormattingOptions options,
|
||||
CancellationToken cancellationToken);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -238,8 +238,7 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer
|
|||
|
||||
if (request.TextEditKind == TextEditKind.FormatOnType)
|
||||
{
|
||||
var mappedEdits = await _razorFormattingService.ApplyFormattedEditsAsync(
|
||||
request.RazorDocumentUri, documentSnapshot, request.Kind, request.ProjectedTextEdits, request.FormattingOptions, cancellationToken);
|
||||
var mappedEdits = await _razorFormattingService.FormatOnTypeAsync(request.RazorDocumentUri, documentSnapshot, request.Kind, request.ProjectedTextEdits, request.FormattingOptions, cancellationToken);
|
||||
|
||||
return new RazorMapToDocumentEditsResponse()
|
||||
{
|
||||
|
@ -254,15 +253,7 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer
|
|||
WrapCSharpSnippets(request.ProjectedTextEdits);
|
||||
}
|
||||
|
||||
var mappedEdits = await _razorFormattingService.ApplyFormattedEditsAsync(
|
||||
request.RazorDocumentUri,
|
||||
documentSnapshot,
|
||||
request.Kind,
|
||||
request.ProjectedTextEdits,
|
||||
request.FormattingOptions,
|
||||
cancellationToken,
|
||||
bypassValidationPasses: true,
|
||||
collapseEdits: true);
|
||||
var mappedEdits = await _razorFormattingService.FormatSnippetAsync(request.RazorDocumentUri, documentSnapshot, request.Kind, request.ProjectedTextEdits, request.FormattingOptions, cancellationToken);
|
||||
|
||||
if (request.Kind == RazorLanguageKind.CSharp)
|
||||
{
|
||||
|
|
|
@ -212,15 +212,13 @@ namespace Microsoft.AspNetCore.Razor.LanguageServer.CodeActions
|
|||
private static RazorFormattingService CreateRazorFormattingService(Uri documentUri)
|
||||
{
|
||||
var razorFormattingService = Mock.Of<RazorFormattingService>(
|
||||
rfs => rfs.ApplyFormattedEditsAsync(
|
||||
rfs => rfs.FormatCodeActionAsync(
|
||||
documentUri,
|
||||
It.IsAny<DocumentSnapshot>(),
|
||||
RazorLanguageKind.CSharp,
|
||||
It.IsAny<TextEdit[]>(),
|
||||
It.IsAny<FormattingOptions>(),
|
||||
It.IsAny<CancellationToken>(),
|
||||
/*bypassValidationPasses:*/ true,
|
||||
It.IsAny<bool>()) == Task.FromResult(s_defaultFormattedEdits), MockBehavior.Strict);
|
||||
It.IsAny<CancellationToken>()) == Task.FromResult(s_defaultFormattedEdits), MockBehavior.Strict);
|
||||
return razorFormattingService;
|
||||
}
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче