Rename some methods to Try... pattern

This commit is contained in:
David Wengier 2024-09-08 21:53:46 +10:00
Родитель b275b91b26
Коммит caa7ca0d99
14 изменённых файлов: 35 добавлений и 36 удалений

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

@ -193,8 +193,8 @@ internal class OnAutoInsertEndpoint(
var textChange = csharpSourceText.GetTextChange(delegatedResponse.TextEdit);
var mappedChange = delegatedResponse.TextEditFormat == InsertTextFormat.Snippet
? await _razorFormattingService.GetCSharpSnippetFormattingEditAsync(documentContext, [textChange], options, cancellationToken).ConfigureAwait(false)
: await _razorFormattingService.GetSingleCSharpEditAsync(documentContext, textChange, options, cancellationToken).ConfigureAwait(false);
? await _razorFormattingService.TryGetCSharpSnippetFormattingEditAsync(documentContext, [textChange], options, cancellationToken).ConfigureAwait(false)
: await _razorFormattingService.TryGetSingleCSharpEditAsync(documentContext, textChange, options, cancellationToken).ConfigureAwait(false);
if (mappedChange is not { } change)
{
return null;

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

@ -66,7 +66,7 @@ internal sealed class DefaultCSharpCodeActionResolver(
// Remaps the text edits from the generated C# to the razor file,
// as well as applying appropriate formatting.
var formattedChange = await _razorFormattingService.GetCSharpCodeActionEditAsync(
var formattedChange = await _razorFormattingService.TryGetCSharpCodeActionEditAsync(
documentContext,
csharpTextChanges,
new RazorFormattingOptions(),

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

@ -213,7 +213,7 @@ internal sealed class GenerateMethodCodeActionResolver(
CodeBlockBraceOnNextLine = _razorLSPOptionsMonitor.CurrentValue.CodeBlockBraceOnNextLine
};
var formattedChange = await _razorFormattingService.GetCSharpCodeActionEditAsync(
var formattedChange = await _razorFormattingService.TryGetCSharpCodeActionEditAsync(
documentContext,
result.SelectAsArray(code.Source.Text.GetTextChange),
formattingOptions,

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

@ -123,7 +123,7 @@ internal class DelegatedCompletionItemResolver(
if (resolvedCompletionItem.TextEdit.Value.TryGetFirst(out var textEdit))
{
var textChange = csharpSourceText.GetTextChange(textEdit);
var formattedTextChange = await _formattingService.GetCSharpSnippetFormattingEditAsync(
var formattedTextChange = await _formattingService.TryGetCSharpSnippetFormattingEditAsync(
documentContext,
[textChange],
options,
@ -145,7 +145,7 @@ internal class DelegatedCompletionItemResolver(
if (resolvedCompletionItem.AdditionalTextEdits is not null)
{
var additionalChanges = resolvedCompletionItem.AdditionalTextEdits.SelectAsArray(csharpSourceText.GetTextChange);
var formattedTextChange = await _formattingService.GetCSharpSnippetFormattingEditAsync(
var formattedTextChange = await _formattingService.TryGetCSharpSnippetFormattingEditAsync(
documentContext,
additionalChanges,
options,

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

@ -322,14 +322,13 @@ internal static class RazorSyntaxNodeExtensions
MarkupMiscAttributeContentSyntax;
}
public static LinePositionSpan? GetLinePositionSpanWithoutWhitespace(this SyntaxNode node, RazorSourceDocument source)
public static bool TryGetLinePositionSpanWithoutWhitespace(this SyntaxNode node, RazorSourceDocument source, out LinePositionSpan linePositionSpan)
{
var tokens = node.GetTokens();
SyntaxToken? firstToken = null;
for (var i = 0; i < tokens.Count; i++)
foreach (var token in tokens)
{
var token = tokens[i];
if (!token.IsWhitespace())
{
firstToken = token;
@ -348,23 +347,23 @@ internal static class RazorSyntaxNodeExtensions
}
}
if (firstToken is null && lastToken is null)
// These two are either both null or neither null, but the || means the compiler doesn't give us nullability warnings
if (firstToken is null || lastToken is null)
{
return null;
linePositionSpan = default;
return false;
}
var startPositionSpan = GetLinePositionSpan(firstToken, source, node.SpanStart);
var endPositionSpan = GetLinePositionSpan(lastToken, source, node.SpanStart);
return new LinePositionSpan(startPositionSpan.Start, endPositionSpan.End);
linePositionSpan = new LinePositionSpan(startPositionSpan.Start, endPositionSpan.End);
return true;
// This is needed because SyntaxToken positions taken from GetTokens
// are relative to their parent node and not to the document.
static LinePositionSpan GetLinePositionSpan(SyntaxNode? node, RazorSourceDocument source, int parentStart)
static LinePositionSpan GetLinePositionSpan(SyntaxNode node, RazorSourceDocument source, int parentStart)
{
ArgHelper.ThrowIfNull(node);
ArgHelper.ThrowIfNull(source);
var sourceText = source.Text;
var start = node.Position + parentStart;

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

@ -35,19 +35,19 @@ internal interface IRazorFormattingService
char triggerCharacter,
CancellationToken cancellationToken);
Task<TextChange?> GetSingleCSharpEditAsync(
Task<TextChange?> TryGetSingleCSharpEditAsync(
DocumentContext documentContext,
TextChange csharpEdit,
RazorFormattingOptions options,
CancellationToken cancellationToken);
Task<TextChange?> GetCSharpCodeActionEditAsync(
Task<TextChange?> TryGetCSharpCodeActionEditAsync(
DocumentContext documentContext,
ImmutableArray<TextChange> csharpEdits,
RazorFormattingOptions options,
CancellationToken cancellationToken);
Task<TextChange?> GetCSharpSnippetFormattingEditAsync(
Task<TextChange?> TryGetCSharpSnippetFormattingEditAsync(
DocumentContext documentContext,
ImmutableArray<TextChange> csharpEdits,
RazorFormattingOptions options,

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

@ -353,12 +353,12 @@ internal sealed class RazorFormattingPass : IFormattingPass
{
var didFormat = false;
if (codeNode.GetLinePositionSpanWithoutWhitespace(source) is not { } codeRange)
if (!codeNode.TryGetLinePositionSpanWithoutWhitespace(source, out var codeRange))
{
return didFormat;
}
if (openBraceNode.GetLinePositionSpanWithoutWhitespace(source) is { } openBraceRange &&
if (openBraceNode.TryGetLinePositionSpanWithoutWhitespace(source, out var openBraceRange) &&
openBraceRange.End.Line == codeRange.Start.Line &&
!RangeHasBeenModified(ref changes, source.Text, codeRange))
{
@ -373,7 +373,7 @@ internal sealed class RazorFormattingPass : IFormattingPass
didFormat = true;
}
if (closeBraceNode.GetLinePositionSpanWithoutWhitespace(source) is { } closeBraceRange &&
if (closeBraceNode.TryGetLinePositionSpanWithoutWhitespace(source, out var closeBraceRange) &&
!RangeHasBeenModified(ref changes, source.Text, codeRange))
{
if (directiveNode is not null &&

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

@ -147,7 +147,7 @@ internal class RazorFormattingService : IRazorFormattingService
automaticallyAddUsings: false,
cancellationToken: cancellationToken);
public async Task<TextChange?> GetSingleCSharpEditAsync(DocumentContext documentContext, TextChange csharpEdit, RazorFormattingOptions options, CancellationToken cancellationToken)
public async Task<TextChange?> TryGetSingleCSharpEditAsync(DocumentContext documentContext, TextChange csharpEdit, RazorFormattingOptions options, CancellationToken cancellationToken)
{
var razorChanges = await ApplyFormattedChangesAsync(
documentContext,
@ -162,7 +162,7 @@ internal class RazorFormattingService : IRazorFormattingService
return razorChanges.SingleOrDefault();
}
public async Task<TextChange?> GetCSharpCodeActionEditAsync(DocumentContext documentContext, ImmutableArray<TextChange> csharpChanges, RazorFormattingOptions options, CancellationToken cancellationToken)
public async Task<TextChange?> TryGetCSharpCodeActionEditAsync(DocumentContext documentContext, ImmutableArray<TextChange> csharpChanges, RazorFormattingOptions options, CancellationToken cancellationToken)
{
var razorChanges = await ApplyFormattedChangesAsync(
documentContext,
@ -177,7 +177,7 @@ internal class RazorFormattingService : IRazorFormattingService
return razorChanges.SingleOrDefault();
}
public async Task<TextChange?> GetCSharpSnippetFormattingEditAsync(DocumentContext documentContext, ImmutableArray<TextChange> csharpChanges, RazorFormattingOptions options, CancellationToken cancellationToken)
public async Task<TextChange?> TryGetCSharpSnippetFormattingEditAsync(DocumentContext documentContext, ImmutableArray<TextChange> csharpChanges, RazorFormattingOptions options, CancellationToken cancellationToken)
{
csharpChanges = WrapCSharpSnippets(csharpChanges);

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

@ -157,13 +157,13 @@ internal sealed class RemoteAutoInsertService(in ServiceArgs args)
var sourceText = await remoteDocumentContext.GetSourceTextAsync(cancellationToken).ConfigureAwait(false);
var csharpTextChange = new TextChange(sourceText.GetTextSpan(autoInsertResponseItem.TextEdit.Range), autoInsertResponseItem.TextEdit.NewText);
var mappedChange = autoInsertResponseItem.TextEditFormat == RoslynInsertTextFormat.Snippet
? await _razorFormattingService.GetCSharpSnippetFormattingEditAsync(
? await _razorFormattingService.TryGetCSharpSnippetFormattingEditAsync(
remoteDocumentContext,
[csharpTextChange],
razorFormattingOptions,
cancellationToken)
.ConfigureAwait(false)
: await _razorFormattingService.GetSingleCSharpEditAsync(
: await _razorFormattingService.TryGetSingleCSharpEditAsync(
remoteDocumentContext,
csharpTextChange,
razorFormattingOptions,

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

@ -106,7 +106,7 @@ internal sealed class CohostOnTypeFormattingEndpoint(
if (triggerKind == IRemoteFormattingService.TriggerKind.ValidHtml)
{
_logger.LogDebug($"Getting Html formatting changes for {razorDocument.FilePath}");
var htmlResult = await GetHtmlFormattingEditsAsync(request, razorDocument, cancellationToken).ConfigureAwait(false);
var htmlResult = await TryGetHtmlFormattingEditsAsync(request, razorDocument, cancellationToken).ConfigureAwait(false);
if (htmlResult is not { } htmlEdits)
{
@ -136,7 +136,7 @@ internal sealed class CohostOnTypeFormattingEndpoint(
return null;
}
private async Task<TextEdit[]?> GetHtmlFormattingEditsAsync(DocumentOnTypeFormattingParams request, TextDocument razorDocument, CancellationToken cancellationToken)
private async Task<TextEdit[]?> TryGetHtmlFormattingEditsAsync(DocumentOnTypeFormattingParams request, TextDocument razorDocument, CancellationToken cancellationToken)
{
var htmlDocument = await _htmlDocumentSynchronizer.TryGetSynchronizedHtmlDocumentAsync(razorDocument, cancellationToken).ConfigureAwait(false);
if (htmlDocument is null)

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

@ -71,7 +71,7 @@ internal sealed class CohostRangeFormattingEndpoint(
private async Task<TextEdit[]?> HandleRequestAsync(DocumentRangeFormattingParams request, TextDocument razorDocument, CancellationToken cancellationToken)
{
_logger.LogDebug($"Getting Html formatting changes for {razorDocument.FilePath}");
var htmlResult = await GetHtmlFormattingEditsAsync(request, razorDocument, cancellationToken).ConfigureAwait(false);
var htmlResult = await TryGetHtmlFormattingEditsAsync(request, razorDocument, cancellationToken).ConfigureAwait(false);
if (htmlResult is not { } htmlEdits)
{
@ -101,7 +101,7 @@ internal sealed class CohostRangeFormattingEndpoint(
return null;
}
private async Task<TextEdit[]?> GetHtmlFormattingEditsAsync(DocumentRangeFormattingParams request, TextDocument razorDocument, CancellationToken cancellationToken)
private async Task<TextEdit[]?> TryGetHtmlFormattingEditsAsync(DocumentRangeFormattingParams request, TextDocument razorDocument, CancellationToken cancellationToken)
{
var htmlDocument = await _htmlDocumentSynchronizer.TryGetSynchronizedHtmlDocumentAsync(razorDocument, cancellationToken).ConfigureAwait(false);
if (htmlDocument is null)

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

@ -192,7 +192,7 @@ public class DefaultCSharpCodeActionResolverTest(ITestOutputHelper testOutput) :
private static IRazorFormattingService CreateRazorFormattingService(Uri documentUri)
{
var razorFormattingService = Mock.Of<IRazorFormattingService>(
rfs => rfs.GetCSharpCodeActionEditAsync(
rfs => rfs.TryGetCSharpCodeActionEditAsync(
It.Is<DocumentContext>(c => c.Uri == documentUri),
It.IsAny<ImmutableArray<TextChange>>(),
It.IsAny<RazorFormattingOptions>(),

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

@ -41,7 +41,7 @@ public abstract class FormattingLanguageServerTestBase(ITestOutputHelper testOut
return SpecializedTasks.EmptyImmutableArray<TextChange>();
}
public Task<TextChange?> GetCSharpCodeActionEditAsync(DocumentContext documentContext, ImmutableArray<TextChange> formattedChanges, RazorFormattingOptions options, CancellationToken cancellationToken)
public Task<TextChange?> TryGetCSharpCodeActionEditAsync(DocumentContext documentContext, ImmutableArray<TextChange> formattedChanges, RazorFormattingOptions options, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
@ -51,7 +51,7 @@ public abstract class FormattingLanguageServerTestBase(ITestOutputHelper testOut
throw new NotImplementedException();
}
public Task<TextChange?> GetCSharpSnippetFormattingEditAsync(DocumentContext documentContext, ImmutableArray<TextChange> edits, RazorFormattingOptions options, CancellationToken cancellationToken)
public Task<TextChange?> TryGetCSharpSnippetFormattingEditAsync(DocumentContext documentContext, ImmutableArray<TextChange> edits, RazorFormattingOptions options, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
@ -61,7 +61,7 @@ public abstract class FormattingLanguageServerTestBase(ITestOutputHelper testOut
return Task.FromResult(htmlChanges);
}
public Task<TextChange?> GetSingleCSharpEditAsync(DocumentContext documentContext, TextChange initialEdit, RazorFormattingOptions options, CancellationToken cancellationToken)
public Task<TextChange?> TryGetSingleCSharpEditAsync(DocumentContext documentContext, TextChange initialEdit, RazorFormattingOptions options, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}

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

@ -232,7 +232,7 @@ public class FormattingTestBase : RazorToolingIntegrationTestBase
// Act
var csharpSourceText = codeDocument.GetCSharpSourceText();
var changes = codeActionEdits.SelectAsArray(csharpSourceText.GetTextChange);
var edit = await formattingService.GetCSharpCodeActionEditAsync(documentContext, changes, options, DisposalToken);
var edit = await formattingService.TryGetCSharpCodeActionEditAsync(documentContext, changes, options, DisposalToken);
// Assert
var edited = razorSourceText.WithChanges(edit.Value);