Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary code from extract method #76689

Merged
merged 1 commit into from
Jan 9, 2025
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 @@ -173,15 +173,12 @@ await semanticDocument.WithSyntaxRootAsync(result.Root, cancellationToken).Confi
protected override AbstractFormattingRule GetCustomFormattingRule(Document document)
=> FormattingRule.Instance;

protected override SyntaxToken? GetInvocationNameToken(IEnumerable<SyntaxToken> methodNames)
=> methodNames.FirstOrNull(t => !t.Parent.IsKind(SyntaxKind.MethodDeclaration));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code exists from a time when we had multiple tokens annotated, both for the invocation site and the declaration side. this code then filtered down to the one wanted. however, this was changed and we now have a single annotation for this single location. so we can jsut fetch it without needing to do this.


protected override SyntaxNode ParseTypeName(string name)
=> SyntaxFactory.ParseTypeName(name);

protected override async Task<(Document document, SyntaxToken? invocationNameToken)> InsertNewLineBeforeLocalFunctionIfNecessaryAsync(
protected override async Task<(Document document, SyntaxToken invocationNameToken)> InsertNewLineBeforeLocalFunctionIfNecessaryAsync(
Document document,
SyntaxToken? invocationNameToken,
SyntaxToken invocationNameToken,
SyntaxNode methodDefinition,
CancellationToken cancellationToken)
{
Expand All @@ -206,8 +203,7 @@ protected override SyntaxNode ParseTypeName(string name)

var newRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

if (invocationNameToken != null)
invocationNameToken = newRoot.FindToken(invocationNameToken.Value.SpanStart);
invocationNameToken = newRoot.FindToken(invocationNameToken.SpanStart);
}

return (document, invocationNameToken);
Expand Down
8 changes: 4 additions & 4 deletions src/Features/Core/Portable/ExtractMethod/MethodExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ internal abstract partial class MethodExtractor(

protected abstract CodeGenerator CreateCodeGenerator(SelectionResult selectionResult, AnalyzerResult analyzerResult);

protected abstract SyntaxToken? GetInvocationNameToken(IEnumerable<SyntaxToken> tokens);
protected abstract AbstractFormattingRule GetCustomFormattingRule(Document document);

protected abstract Task<(Document document, SyntaxToken? invocationNameToken)> InsertNewLineBeforeLocalFunctionIfNecessaryAsync(
Document document, SyntaxToken? invocationNameToken, SyntaxNode methodDefinition, CancellationToken cancellationToken);
protected abstract Task<(Document document, SyntaxToken invocationNameToken)> InsertNewLineBeforeLocalFunctionIfNecessaryAsync(
Document document, SyntaxToken invocationNameToken, SyntaxNode methodDefinition, CancellationToken cancellationToken);

public ExtractMethodResult ExtractMethod(OperationStatus initialStatus, CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -92,7 +91,8 @@ public ExtractMethodResult ExtractMethod(OperationStatus initialStatus, Cancella
cancellationToken.ThrowIfCancellationRequested();

var newRoot = afterTriviaRestored.Root;
var invocationNameToken = GetInvocationNameToken(newRoot.GetAnnotatedTokens(MethodNameAnnotation));

var invocationNameToken = newRoot.GetAnnotatedTokens(MethodNameAnnotation).Single();

// Do some final patchups of whitespace when inserting a local function.
if (LocalFunction)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.ExtractMethod
Return FormattingRule.Instance
End Function

Protected Overrides Function GetInvocationNameToken(methodNames As IEnumerable(Of SyntaxToken)) As SyntaxToken?
Return methodNames.FirstOrNull(Function(t) t.Parent.Kind <> SyntaxKind.SubStatement AndAlso t.Parent.Kind <> SyntaxKind.FunctionStatement)
End Function

Protected Overrides Function ParseTypeName(name As String) As SyntaxNode
Return SyntaxFactory.ParseTypeName(name)
End Function
Expand Down Expand Up @@ -115,9 +111,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.ExtractMethod

Protected Overrides Function InsertNewLineBeforeLocalFunctionIfNecessaryAsync(
document As Document,
invocationNameToken? As SyntaxToken,
invocationNameToken As SyntaxToken,
methodDefinition As SyntaxNode,
cancellationToken As CancellationToken) As Task(Of (document As Document, invocationNameToken As SyntaxToken?))
cancellationToken As CancellationToken) As Task(Of (document As Document, invocationNameToken As SyntaxToken))
' VB doesn't need to do any correction, so we just return the values untouched
Return Task.FromResult((document, invocationNameToken))
End Function
Expand Down
Loading