Пример #1
0
        public static async Task <Compilation> Compile(
            this Package package,
            Workspace workspace,
            Budget budget,
            BufferId activeBufferId)
        {
            var sourceFiles = workspace.GetSourceFiles().ToArray();

            var(compilation, documents) = await package.GetCompilationForRun(sourceFiles, SourceCodeKind.Regular, workspace.Usings, budget);

            var viewports = workspace.ExtractViewPorts();

            var diagnostics = compilation.GetDiagnostics();

            if (workspace.IncludeInstrumentation && !diagnostics.ContainsError())
            {
                var activeDocument = GetActiveDocument(documents, activeBufferId);
                compilation = await AugmentCompilationAsync(viewports, compilation, activeDocument, activeBufferId, package);
            }

            return(compilation);
        }
Пример #2
0
        internal static (IReadOnlyCollection <SerializableDiagnostic> DiagnosticsInActiveBuffer, IReadOnlyCollection <SerializableDiagnostic> AllDiagnostics) MapDiagnostics(
            this Workspace workspace,
            BufferId activeBufferId,
            IReadOnlyCollection <Diagnostic> diagnostics,
            Budget budget = null)
        {
            if (workspace == null)
            {
                throw new ArgumentNullException(nameof(workspace));
            }

            if (diagnostics == null || diagnostics.Count == 0)
            {
                return(Array.Empty <SerializableDiagnostic>(), Array.Empty <SerializableDiagnostic>());
            }
            else
            {
                diagnostics = diagnostics.RemoveSuppressed();
            }

            budget = budget ?? new Budget();

            var viewPorts = workspace.ExtractViewPorts().ToList();

            budget.RecordEntry();

            var paddingSize = BufferInliningTransformer.PaddingSize;

            var diagnosticsInBuffer = FilterDiagnosticsForViewport().ToArray();
            var projectDiagnostics  = diagnostics.Select(d => d.ToSerializableDiagnostic()).ToArray();

            return(
                diagnosticsInBuffer,
                projectDiagnostics
                );

            IEnumerable <SerializableDiagnostic> FilterDiagnosticsForViewport()
            {
                foreach (var diagnostic in diagnostics)
                {
                    if (diagnostic.Location == Location.None)
                    {
                        continue;
                    }

                    var filePath = diagnostic.Location.SourceTree?.FilePath;

                    // hide warnings that are not within the visible code
                    if (!diagnostic.IsError() &&
                        !string.IsNullOrWhiteSpace(filePath))
                    {
                        if (Path.GetFileName(filePath) != Path.GetFileName(activeBufferId?.FileName))
                        {
                            continue;
                        }
                    }

                    var lineSpan     = diagnostic.Location.GetMappedLineSpan();
                    var lineSpanPath = lineSpan.Path;

                    if (viewPorts.Count == 0 || string.IsNullOrWhiteSpace(activeBufferId?.RegionName))
                    {
                        var errorMessage = RelativizeDiagnosticMessage();

                        yield return(diagnostic.ToSerializableDiagnostic(errorMessage, activeBufferId));
                    }
                    else
                    {
                        var target = viewPorts
                                     .Where(e => e.BufferId.RegionName != null &&
                                            e.BufferId.RegionName == activeBufferId.RegionName &&
                                            (string.IsNullOrWhiteSpace(lineSpanPath) || lineSpanPath.EndsWith(e.Destination.Name)))
                                     .FirstOrDefault(e => e.Region.Contains(diagnostic.Location.SourceSpan.Start));

                        if (target != null && !target.Region.IsEmpty)
                        {
                            var processedDiagnostic = AlignDiagnosticLocation(target, diagnostic, paddingSize);
                            if (processedDiagnostic != null)
                            {
                                yield return(processedDiagnostic);
                            }
                        }
                    }

                    string RelativizeDiagnosticMessage()
                    {
                        var message = diagnostic.ToString();

                        if (!string.IsNullOrWhiteSpace(lineSpanPath))
                        {
                            var directoryPath = new FileInfo(lineSpanPath).Directory?.FullName ?? "";

                            if (!directoryPath.EndsWith(Path.DirectorySeparatorChar.ToString()))
                            {
                                directoryPath += Path.DirectorySeparatorChar;
                            }

                            if (message.StartsWith(directoryPath))
                            {
                                return(message.Substring(directoryPath.Length));
                            }
                        }

                        return(message);
                    }
                }
            }
        }