Skip to content

Commit

Permalink
Try to use append/append/delete instead of replace/append
Browse files Browse the repository at this point in the history
  • Loading branch information
bradwilson committed Nov 19, 2023
1 parent e678b6c commit 1d124ac
Showing 1 changed file with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,11 @@ static async Task<Document> UseSingleMethod(

var oneItemVariableStatement =
OneItemVariableStatement(parameterName, replacementNode)
.WithLeadingTrivia(invocation.GetLeadingTrivia());
.WithLeadingTrivia(invocation.Parent.GetLeadingTrivia());

ReplaceCollectionWithSingle(editor, oneItemVariableStatement, invocation.Parent);
AppendSingleAfterCollection(editor, oneItemVariableStatement, invocation.Parent);
AppendLambdaStatements(editor, oneItemVariableStatement, lambdaExpression);
editor.RemoveNode(invocation.Parent);
}
else if (invocation.ArgumentList.Arguments[1].Expression is IdentifierNameSyntax identifierExpression)
{
Expand All @@ -127,10 +128,11 @@ static async Task<Document> UseSingleMethod(

var oneItemVariableStatement =
OneItemVariableStatement(parameterName, replacementNode)
.WithLeadingTrivia(invocation.GetLeadingTrivia());
.WithLeadingTrivia(invocation.Parent.GetLeadingTrivia());

ReplaceCollectionWithSingle(editor, oneItemVariableStatement, invocation.Parent);
AppendSingleAfterCollection(editor, oneItemVariableStatement, invocation.Parent);
AppendMethodInvocation(editor, oneItemVariableStatement, identifierExpression, parameterName);
editor.RemoveNode(invocation.Parent);
}
}
}
Expand All @@ -139,6 +141,14 @@ static async Task<Document> UseSingleMethod(
return editor.GetChangedDocument();
}

static void AppendSingleAfterCollection(
DocumentEditor editor,
LocalDeclarationStatementSyntax oneItemVariableStatement,
SyntaxNode parent)
{
editor.InsertAfter(parent, oneItemVariableStatement);
}

static LocalDeclarationStatementSyntax OneItemVariableStatement(
string parameterName,
InvocationExpressionSyntax replacementNode)
Expand Down

0 comments on commit 1d124ac

Please sign in to comment.