From ee2f29042040b907a1f7be7ab5b1ac927d6783d9 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Sat, 19 Sep 2020 13:39:59 -0700 Subject: [PATCH] Improve comments for top-level statement insertion See #44453 --- .../Portable/CodeGeneration/CSharpCodeGenerationService.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Workspaces/CSharp/Portable/CodeGeneration/CSharpCodeGenerationService.cs b/src/Workspaces/CSharp/Portable/CodeGeneration/CSharpCodeGenerationService.cs index 18f4b3cd3d7..c5744915cdb 100644 --- a/src/Workspaces/CSharp/Portable/CodeGeneration/CSharpCodeGenerationService.cs +++ b/src/Workspaces/CSharp/Portable/CodeGeneration/CSharpCodeGenerationService.cs @@ -484,7 +484,12 @@ protected override TDeclarationNode AddMembers(TDeclarationNod } else if (destinationMember is CompilationUnitSyntax compilationUnit && options is null) { - // Insert the new global statement(s) at the end of any current global statements + // This path supports top-level statement insertion. It only applies when 'options' + // is null so the fallback code below can handle cases where the insertion location + // is provided through options.BestLocation. + // + // Insert the new global statement(s) at the end of any current global statements. + // This code relies on 'LastIndexOf' returning -1 when no matching element is found. var insertionIndex = compilationUnit.Members.LastIndexOf(memberDeclaration => memberDeclaration.IsKind(SyntaxKind.GlobalStatement)) + 1; var wrappedStatements = StatementGenerator.GenerateStatements(statements).Select(generated => SyntaxFactory.GlobalStatement(generated)).ToArray(); return Cast(compilationUnit.WithMembers(compilationUnit.Members.InsertRange(insertionIndex, wrappedStatements))); -- GitLab