private static SyntaxNode InsertStatements( SyntaxNode node, IEnumerable <StatementSyntax> newStatements) { var body = (BlockSyntax)GetBodyOrExpressionBody(node); SyntaxList <StatementSyntax> statements = body.Statements; StatementSyntax lastStatement = statements.LastOrDefault(f => !f.IsKind(SyntaxKind.LocalFunctionStatement, SyntaxKind.ReturnStatement)); int index = (lastStatement != null) ? statements.IndexOf(lastStatement) + 1 : 0; BlockSyntax newBody = body.WithStatements(statements.InsertRange(index, newStatements)); if (node is MethodDeclarationSyntax methodDeclaration) { return(methodDeclaration.WithBody(newBody)); } return(((LocalFunctionStatementSyntax)node).WithBody(newBody)); }