private static Tuple<Compilation, SyntaxTree> AddToCompilation(Compilation compilation, SyntaxTree tree)
{
if (!compilation.ContainsSyntaxTree(tree))
{
var newTree = tree;
if (!tree.HasCompilationUnitRoot)
{
var childNodes = tree.GetRoot()
.ChildNodes()
.AsArray();
newTree = CSharpSyntaxTree.Create(SyntaxFactory.CompilationUnit()
.WithMembers(
SyntaxFactory.List(childNodes.OfType<MemberDeclarationSyntax>()))
.WithUsings(
SyntaxFactory.List(childNodes.OfType<UsingDirectiveSyntax>()))
.WithExterns(
SyntaxFactory.List(childNodes.OfType<ExternAliasDirectiveSyntax>())));
}
var comp = compilation.AddSyntaxTrees(newTree);
return new Tuple<Compilation, SyntaxTree>(comp, newTree);
}
return new Tuple<Compilation, SyntaxTree>(compilation, tree);
}