public void FilterActiveViewport_Should_Return_Viewport_In_ActiveBufferId() { var text = @" using System; namespace RoslynRecorder { class Program { static void Main(string[] args) { {|regionStart:#region test|} int a = 0; Console.WriteLine(""Entry Point""); {|regionEnd:#endregion|} } #region notthis } #endregion }".EnforceLF(); MarkupTestFile.GetNamedSpans(text, out var code, out var spans); var workspace = new Workspace(files: new[] { new File("testFile.cs", code) }); var viewports = workspace.ExtractViewPorts(); var activeViewport = InstrumentationLineMapper.FilterActiveViewport(viewports, BufferId.Parse("testFile.cs@test")).First(); activeViewport.Region.Start.Should().Be(spans["regionStart"].First().End); activeViewport.Region.End.Should().Be(spans["regionEnd"].First().Start); }
public void FilterActiveViewport_Should_Return_Empty_Array_If_No_Regions() { var text = Sources.simple.EnforceLF(); var workspace = new Workspace(files: new[] { new File("testFile.cs", text) }); var viewports = workspace.ExtractViewPorts(); var activeViewport = InstrumentationLineMapper.FilterActiveViewport(viewports, BufferId.Parse("testFile.cs@test")); activeViewport.Should().BeEmpty(); }
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); }