Skip to content
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 @@ -17,6 +17,7 @@
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.RemoveUnnecessaryImports;
using Microsoft.CodeAnalysis.Shared.Collections;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Roslyn.Utilities;

Expand All @@ -33,7 +34,7 @@ public static async Task<Document> ConvertToTopLevelStatementsAsync(
Contract.ThrowIfNull(typeDeclaration); // checked by analyzer

var generator = document.GetRequiredLanguageService<SyntaxGenerator>();
var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var root = (CompilationUnitSyntax)await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

var semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var rootWithGlobalStatements = GetRootWithGlobalStatements(
Expand Down Expand Up @@ -97,7 +98,7 @@ private static void AddUsingDirectives(NameSyntax name, SyntaxAnnotation annotat
private static SyntaxNode GetRootWithGlobalStatements(
SemanticModel semanticModel,
SyntaxGenerator generator,
SyntaxNode root,
CompilationUnitSyntax root,
TypeDeclarationSyntax typeDeclaration,
MethodDeclarationSyntax methodDeclaration,
CancellationToken cancellationToken)
Expand All @@ -107,19 +108,17 @@ private static SyntaxNode GetRootWithGlobalStatements(
semanticModel, typeDeclaration, methodDeclaration, cancellationToken);

var namespaceDeclaration = typeDeclaration.Parent as BaseNamespaceDeclarationSyntax;

if (namespaceDeclaration != null &&
namespaceDeclaration.Members.Count >= 2)
{
// Our parent namespace has another symbol in it. Keep the namespace declaration around, removing only
// the existing Program type from it.
editor.RemoveNode(typeDeclaration);
editor.ReplaceNode(
root,
(current, _) =>
{
var currentRoot = (CompilationUnitSyntax)current;
return currentRoot.WithMembers(currentRoot.Members.InsertRange(0, globalStatements));
});
editor.InsertBefore(namespaceDeclaration, globalStatements);

// We want to place the trailing directive on the namespace declaration we're preceding.
AddDirectivesToNextMemberOrEndOfFile(root.Members.IndexOf(namespaceDeclaration));
}
else if (namespaceDeclaration != null)
{
Expand All @@ -136,18 +135,60 @@ private static SyntaxNode GetRootWithGlobalStatements(
globalStatements[0].WithPrependedLeadingTrivia(fileBanner));
}

editor.ReplaceNode(
root,
root.ReplaceNode(namespaceDeclaration, globalStatements));
editor.ReplaceNode(namespaceDeclaration, (_, _) => globalStatements);

// We're removing the namespace itself. So we want to place the trailing directive on the element that follows that.
AddDirectivesToNextMemberOrEndOfFile(root.Members.IndexOf(namespaceDeclaration) + 1);
}
else
{
// type wasn't in a namespace. just remove the type and replace it with the new global statements.
editor.ReplaceNode(
root, root.ReplaceNode(typeDeclaration, globalStatements));
editor.ReplaceNode(typeDeclaration, (_, _) => globalStatements);

// We're removing the namespace itself. So we want to place the trailing directive on the element that follows that.
AddDirectivesToNextMemberOrEndOfFile(root.Members.IndexOf(typeDeclaration) + 1);
}

return editor.GetChangedRoot();

void AddDirectivesToNextMemberOrEndOfFile(int memberIndexToPlaceTrailingDirectivesOn)
{
// If the method has trailing directive on the close brace, move them to whatever will come after the
// final global statement in the new file. That could be the next namespace/type member declaration. Or
// it could be the end of file token if there are no more members in the file.
if (methodDeclaration.Body is not BlockSyntax block)
return;

var leadingCloseBraceTrivia = block.CloseBraceToken.LeadingTrivia;
if (!leadingCloseBraceTrivia.Any(t => t.IsDirective))
return;

if (memberIndexToPlaceTrailingDirectivesOn < root.Members.Count)
{
editor.ReplaceNode(
root.Members[memberIndexToPlaceTrailingDirectivesOn],
(current, _) =>
{
var updated = current.WithPrependedLeadingTrivia(leadingCloseBraceTrivia);
updated = updated.ReplaceToken(
updated.GetFirstToken(),
updated.GetFirstToken().WithAdditionalAnnotations(Formatter.Annotation));
return updated;
});
}
else
{
editor.ReplaceNode(
root,
(current, _) =>
{
var currentRoot = (CompilationUnitSyntax)current;
return currentRoot.WithEndOfFileToken(currentRoot.EndOfFileToken
.WithPrependedLeadingTrivia(leadingCloseBraceTrivia)
.WithAdditionalAnnotations(Formatter.Annotation));
});
}
}
}

private static ImmutableArray<GlobalStatementSyntax> GetGlobalStatements(
Expand Down
Loading
Loading