Skip to content

Commit

Permalink
Remove blocking async calls from sig-change (#76507)
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi authored Dec 19, 2024
2 parents d4b570f + 85b743f commit 0c39338
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,13 @@ private static bool InSymbolHeader(SyntaxNode matchingNode, int position)
}
}

public override async Task<SyntaxNode> ChangeSignatureAsync(
Document document,
public override SyntaxNode ChangeSignature(
SemanticDocument document,
ISymbol declarationSymbol,
SyntaxNode potentiallyUpdatedNode,
SyntaxNode originalNode,
SignatureChange signaturePermutation,
LineFormattingOptions lineFormattingOptions,
CancellationToken cancellationToken)
{
var updatedNode = potentiallyUpdatedNode as CSharpSyntaxNode;
Expand All @@ -307,7 +308,8 @@ or SyntaxKind.RecordDeclaration
or SyntaxKind.StructDeclaration
or SyntaxKind.ClassDeclaration)
{
var updatedLeadingTrivia = await UpdateParamTagsInLeadingTriviaAsync(document, updatedNode, declarationSymbol, signaturePermutation, cancellationToken).ConfigureAwait(false);
var updatedLeadingTrivia = UpdateParamTagsInLeadingTrivia(
document.Document, updatedNode, declarationSymbol, signaturePermutation, lineFormattingOptions);
if (updatedLeadingTrivia != default && !updatedLeadingTrivia.IsEmpty)
{
updatedNode = updatedNode.WithLeadingTrivia(updatedLeadingTrivia);
Expand All @@ -318,37 +320,37 @@ or SyntaxKind.StructDeclaration
if (updatedNode is MethodDeclarationSyntax method)
{
var updatedParameters = UpdateDeclaration(method.ParameterList.Parameters, signaturePermutation, CreateNewParameterSyntax);
return method.WithParameterList(method.ParameterList.WithParameters(updatedParameters).WithAdditionalAnnotations(changeSignatureFormattingAnnotation));
return method.WithParameterList(method.ParameterList.WithParameters(updatedParameters).WithAdditionalAnnotations(ChangeSignatureFormattingAnnotation));
}

if (updatedNode is TypeDeclarationSyntax { ParameterList: not null } typeWithParameters)
{
var updatedParameters = UpdateDeclaration(typeWithParameters.ParameterList.Parameters, signaturePermutation, CreateNewParameterSyntax);
return typeWithParameters.WithParameterList(typeWithParameters.ParameterList.WithParameters(updatedParameters).WithAdditionalAnnotations(changeSignatureFormattingAnnotation));
return typeWithParameters.WithParameterList(typeWithParameters.ParameterList.WithParameters(updatedParameters).WithAdditionalAnnotations(ChangeSignatureFormattingAnnotation));
}

if (updatedNode is LocalFunctionStatementSyntax localFunction)
{
var updatedParameters = UpdateDeclaration(localFunction.ParameterList.Parameters, signaturePermutation, CreateNewParameterSyntax);
return localFunction.WithParameterList(localFunction.ParameterList.WithParameters(updatedParameters).WithAdditionalAnnotations(changeSignatureFormattingAnnotation));
return localFunction.WithParameterList(localFunction.ParameterList.WithParameters(updatedParameters).WithAdditionalAnnotations(ChangeSignatureFormattingAnnotation));
}

if (updatedNode is ConstructorDeclarationSyntax constructor)
{
var updatedParameters = UpdateDeclaration(constructor.ParameterList.Parameters, signaturePermutation, CreateNewParameterSyntax);
return constructor.WithParameterList(constructor.ParameterList.WithParameters(updatedParameters).WithAdditionalAnnotations(changeSignatureFormattingAnnotation));
return constructor.WithParameterList(constructor.ParameterList.WithParameters(updatedParameters).WithAdditionalAnnotations(ChangeSignatureFormattingAnnotation));
}

if (updatedNode is IndexerDeclarationSyntax indexer)
{
var updatedParameters = UpdateDeclaration(indexer.ParameterList.Parameters, signaturePermutation, CreateNewParameterSyntax);
return indexer.WithParameterList(indexer.ParameterList.WithParameters(updatedParameters).WithAdditionalAnnotations(changeSignatureFormattingAnnotation));
return indexer.WithParameterList(indexer.ParameterList.WithParameters(updatedParameters).WithAdditionalAnnotations(ChangeSignatureFormattingAnnotation));
}

if (updatedNode is DelegateDeclarationSyntax delegateDeclaration)
{
var updatedParameters = UpdateDeclaration(delegateDeclaration.ParameterList.Parameters, signaturePermutation, CreateNewParameterSyntax);
return delegateDeclaration.WithParameterList(delegateDeclaration.ParameterList.WithParameters(updatedParameters).WithAdditionalAnnotations(changeSignatureFormattingAnnotation));
return delegateDeclaration.WithParameterList(delegateDeclaration.ParameterList.WithParameters(updatedParameters).WithAdditionalAnnotations(ChangeSignatureFormattingAnnotation));
}

if (updatedNode is AnonymousMethodExpressionSyntax anonymousMethod)
Expand All @@ -360,7 +362,7 @@ or SyntaxKind.StructDeclaration
}

var updatedParameters = UpdateDeclaration(anonymousMethod.ParameterList.Parameters, signaturePermutation, CreateNewParameterSyntax);
return anonymousMethod.WithParameterList(anonymousMethod.ParameterList.WithParameters(updatedParameters).WithAdditionalAnnotations(changeSignatureFormattingAnnotation));
return anonymousMethod.WithParameterList(anonymousMethod.ParameterList.WithParameters(updatedParameters).WithAdditionalAnnotations(ChangeSignatureFormattingAnnotation));
}

if (updatedNode is SimpleLambdaExpressionSyntax lambda)
Expand Down Expand Up @@ -411,23 +413,23 @@ or SyntaxKind.StructDeclaration
return nameMemberCref.WithParameters(newCrefParameterList);
}

var semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var semanticModel = document.SemanticModel;

// Update reference site argument lists
if (updatedNode is InvocationExpressionSyntax invocation)
{
var symbolInfo = semanticModel.GetSymbolInfo((InvocationExpressionSyntax)originalNode, cancellationToken);

return invocation.WithArgumentList(
await UpdateArgumentListAsync(
UpdateArgumentList(
document,
declarationSymbol,
signaturePermutation,
invocation.ArgumentList,
symbolInfo.Symbol is IMethodSymbol { MethodKind: MethodKind.ReducedExtension },
IsParamsArrayExpanded(semanticModel, invocation, symbolInfo, cancellationToken),
document,
originalNode.SpanStart,
cancellationToken).ConfigureAwait(false));
cancellationToken));
}

// Handles both ObjectCreationExpressionSyntax and ImplicitObjectCreationExpressionSyntax
Expand All @@ -441,47 +443,47 @@ await UpdateArgumentListAsync(
var symbolInfo = semanticModel.GetSymbolInfo((BaseObjectCreationExpressionSyntax)originalNode, cancellationToken);

return objCreation.WithArgumentList(
await UpdateArgumentListAsync(
UpdateArgumentList(
document,
declarationSymbol,
signaturePermutation,
objCreation.ArgumentList,
isReducedExtensionMethod: false,
IsParamsArrayExpanded(semanticModel, objCreation, symbolInfo, cancellationToken),
document,
originalNode.SpanStart,
cancellationToken).ConfigureAwait(false));
cancellationToken));
}

if (updatedNode is ConstructorInitializerSyntax constructorInit)
{
var symbolInfo = semanticModel.GetSymbolInfo((ConstructorInitializerSyntax)originalNode, cancellationToken);

return constructorInit.WithArgumentList(
await UpdateArgumentListAsync(
UpdateArgumentList(
document,
declarationSymbol,
signaturePermutation,
constructorInit.ArgumentList,
isReducedExtensionMethod: false,
IsParamsArrayExpanded(semanticModel, constructorInit, symbolInfo, cancellationToken),
document,
originalNode.SpanStart,
cancellationToken).ConfigureAwait(false));
cancellationToken));
}

if (updatedNode is ElementAccessExpressionSyntax elementAccess)
{
var symbolInfo = semanticModel.GetSymbolInfo((ElementAccessExpressionSyntax)originalNode, cancellationToken);

return elementAccess.WithArgumentList(
await UpdateArgumentListAsync(
UpdateArgumentList(
document,
declarationSymbol,
signaturePermutation,
elementAccess.ArgumentList,
isReducedExtensionMethod: false,
IsParamsArrayExpanded(semanticModel, elementAccess, symbolInfo, cancellationToken),
document,
originalNode.SpanStart,
cancellationToken).ConfigureAwait(false));
cancellationToken));
}

if (updatedNode is AttributeSyntax attribute)
Expand All @@ -494,28 +496,28 @@ await UpdateArgumentListAsync(
}

return attribute.WithArgumentList(
await UpdateAttributeArgumentListAsync(
UpdateAttributeArgumentList(
document,
declarationSymbol,
signaturePermutation,
attribute.ArgumentList,
isReducedExtensionMethod: false,
IsParamsArrayExpanded(semanticModel, attribute, symbolInfo, cancellationToken),
document,
originalNode.SpanStart,
cancellationToken).ConfigureAwait(false));
cancellationToken));
}

Debug.Assert(false, "Unknown reference location");
return null;
}

private async Task<T> UpdateArgumentListAsync<T>(
private T UpdateArgumentList<T>(
SemanticDocument document,
ISymbol declarationSymbol,
SignatureChange signaturePermutation,
T argumentList,
bool isReducedExtensionMethod,
bool isParamsArrayExpanded,
Document document,
int position,
CancellationToken cancellationToken) where T : BaseArgumentListSyntax
{
Expand All @@ -529,30 +531,30 @@ private async Task<T> UpdateArgumentListAsync<T>(

// Adds new arguments into the updated list
// e.g. P(c, a) ==> P(x, c, a, y)
newArguments = await AddNewArgumentsToListAsync(
newArguments = AddNewArgumentsToList(
document,
declarationSymbol,
newArguments,
argumentList.Arguments,
signaturePermutation,
isReducedExtensionMethod,
isParamsArrayExpanded,
generateAttributeArguments: false,
document,
position,
cancellationToken).ConfigureAwait(false);
cancellationToken);

return (T)argumentList
.WithArguments(newArguments)
.WithAdditionalAnnotations(changeSignatureFormattingAnnotation);
.WithAdditionalAnnotations(ChangeSignatureFormattingAnnotation);
}

private async Task<AttributeArgumentListSyntax> UpdateAttributeArgumentListAsync(
private AttributeArgumentListSyntax UpdateAttributeArgumentList(
SemanticDocument document,
ISymbol declarationSymbol,
SignatureChange signaturePermutation,
AttributeArgumentListSyntax argumentList,
bool isReducedExtensionMethod,
bool isParamsArrayExpanded,
Document document,
int position,
CancellationToken cancellationToken)
{
Expand All @@ -561,21 +563,21 @@ private async Task<AttributeArgumentListSyntax> UpdateAttributeArgumentListAsync
argumentList.Arguments,
signaturePermutation.WithoutAddedParameters());

newArguments = await AddNewArgumentsToListAsync(
newArguments = AddNewArgumentsToList(
document,
declarationSymbol,
newArguments,
argumentList.Arguments,
signaturePermutation,
isReducedExtensionMethod,
isParamsArrayExpanded,
generateAttributeArguments: true,
document,
position,
cancellationToken).ConfigureAwait(false);
cancellationToken);

return argumentList
.WithArguments(newArguments)
.WithAdditionalAnnotations(changeSignatureFormattingAnnotation);
.WithAdditionalAnnotations(ChangeSignatureFormattingAnnotation);
}

private static bool IsParamsArrayExpanded(SemanticModel semanticModel, SyntaxNode node, SymbolInfo symbolInfo, CancellationToken cancellationToken)
Expand Down Expand Up @@ -687,24 +689,22 @@ protected override T TransferLeadingWhitespaceTrivia<T>(T newArgument, SyntaxNod
return newArgument;
}

private async Task<SeparatedSyntaxList<TArgumentSyntax>> AddNewArgumentsToListAsync<TArgumentSyntax>(
private SeparatedSyntaxList<TArgumentSyntax> AddNewArgumentsToList<TArgumentSyntax>(
SemanticDocument document,
ISymbol declarationSymbol,
SeparatedSyntaxList<TArgumentSyntax> newArguments,
SeparatedSyntaxList<TArgumentSyntax> originalArguments,
SignatureChange signaturePermutation,
bool isReducedExtensionMethod,
bool isParamsArrayExpanded,
bool generateAttributeArguments,
Document document,
int position,
CancellationToken cancellationToken)
where TArgumentSyntax : SyntaxNode
{
var newArgumentList = await AddNewArgumentsToListAsync(
declarationSymbol, newArguments,
signaturePermutation, isReducedExtensionMethod,
isParamsArrayExpanded, generateAttributeArguments,
document, position, cancellationToken).ConfigureAwait(false);
var newArgumentList = AddNewArgumentsToList(
document, declarationSymbol, newArguments, signaturePermutation,
isReducedExtensionMethod, isParamsArrayExpanded, generateAttributeArguments, position, cancellationToken);

return SeparatedList(
TransferLeadingWhitespaceTrivia(newArgumentList, originalArguments),
Expand Down Expand Up @@ -765,13 +765,15 @@ private ImmutableArray<T> TransferLeadingWhitespaceTrivia<T, U>(IEnumerable<T> n
return result.ToImmutableAndClear();
}

private async ValueTask<ImmutableArray<SyntaxTrivia>> UpdateParamTagsInLeadingTriviaAsync(
Document document, CSharpSyntaxNode node, ISymbol declarationSymbol, SignatureChange updatedSignature, CancellationToken cancellationToken)
private ImmutableArray<SyntaxTrivia> UpdateParamTagsInLeadingTrivia(
Document document,
CSharpSyntaxNode node,
ISymbol declarationSymbol,
SignatureChange updatedSignature,
LineFormattingOptions lineFormattingOptions)
{
if (!node.HasLeadingTrivia)
{
return [];
}

var paramNodes = node
.DescendantNodes(descendIntoTrivia: true)
Expand All @@ -780,12 +782,9 @@ private async ValueTask<ImmutableArray<SyntaxTrivia>> UpdateParamTagsInLeadingTr

var permutedParamNodes = VerifyAndPermuteParamNodes(paramNodes, declarationSymbol, updatedSignature);
if (permutedParamNodes.IsEmpty)
{
return [];
}

var options = await document.GetLineFormattingOptionsAsync(cancellationToken).ConfigureAwait(false);
return GetPermutedDocCommentTrivia(node, permutedParamNodes, document.Project.Services, options);
return GetPermutedDocCommentTrivia(node, permutedParamNodes, document.Project.Services, lineFormattingOptions);
}

private ImmutableArray<SyntaxNode> VerifyAndPermuteParamNodes(IEnumerable<XmlElementSyntax> paramNodes, ISymbol declarationSymbol, SignatureChange updatedSignature)
Expand Down
Loading

0 comments on commit 0c39338

Please sign in to comment.