private static async Task <Compilation> AugmentCompilationAsync( IEnumerable <Viewport> viewports, Compilation compilation, Document document, BufferId activeBufferId, Package build) { var regions = InstrumentationLineMapper.FilterActiveViewport(viewports, activeBufferId) .Where(v => v.Destination?.Name != null) .GroupBy(v => v.Destination.Name, v => v.Region, (name, region) => new InstrumentationMap(name, region)) .ToArray(); var solution = document.Project.Solution; var newCompilation = compilation; foreach (var tree in newCompilation.SyntaxTrees) { var replacementRegions = regions.FirstOrDefault(r => tree.FilePath.EndsWith(r.FileToInstrument))?.InstrumentationRegions; var subdocument = solution.GetDocument(tree); var visitor = new InstrumentationSyntaxVisitor(subdocument, await subdocument.GetSemanticModelAsync(), replacementRegions); var linesWithInstrumentation = visitor.Augmentations.Data.Keys; var activeViewport = viewports.DefaultIfEmpty(null).First(); var(augmentationMap, variableLocationMap) = await InstrumentationLineMapper.MapLineLocationsRelativeToViewportAsync( visitor.Augmentations, visitor.VariableLocations, document, activeViewport); var rewrite = new InstrumentationSyntaxRewriter( linesWithInstrumentation, variableLocationMap, augmentationMap); var newRoot = rewrite.Visit(tree.GetRoot()); var newTree = tree.WithRootAndOptions(newRoot, tree.Options); newCompilation = newCompilation.ReplaceSyntaxTree(tree, newTree); } var instrumentationSyntaxTree = build.GetInstrumentationEmitterSyntaxTree(); newCompilation = newCompilation.AddSyntaxTrees(instrumentationSyntaxTree); var augmentedDiagnostics = newCompilation.GetDiagnostics(); if (augmentedDiagnostics.ContainsError()) { throw new InvalidOperationException( $@"Augmented source failed to compile Diagnostics ----------- {string.Join(NewLine, augmentedDiagnostics)} Source ------ {newCompilation.SyntaxTrees.Select(s => $"// {s.FilePath ?? "(anonymous)"}{NewLine}//---------------------------------{NewLine}{NewLine}{s}").Join(NewLine + NewLine)}"); } return(newCompilation); }