Beispiel #1
0
        private static SerializableDiagnostic AlignDiagnosticLocation(
            Viewport viewport,
            Diagnostic diagnostic,
            int paddingSize)
        {
            // this diagnostics does not apply to viewport
            if (diagnostic.Location != Location.None &&
                !string.IsNullOrWhiteSpace(diagnostic.Location.SourceTree.FilePath) &&
                !diagnostic.Location.SourceTree.FilePath.Contains(viewport.Destination.Name))
            {
                return(null);
            }

            // offset of the buffer into the original source file
            var offset = viewport.Region.Start;
            // span of content injected in the buffer viewport
            var selectionSpan = new TextSpan(offset + paddingSize, viewport.Region.Length - (2 * paddingSize));

            // aligned offset of the diagnostic entry
            var start = diagnostic.Location.SourceSpan.Start - selectionSpan.Start;
            var end   = diagnostic.Location.SourceSpan.End - selectionSpan.Start;
            // line containing the diagnostic in the original source file
            var line = viewport.Destination.Text.Lines[diagnostic.Location.GetMappedLineSpan().StartLinePosition.Line];

            // first line of the region from the source file
            var lineOffset = 0;
            var sourceText = viewport.Destination.Text.GetSubText(selectionSpan);

            foreach (var regionLine in sourceText.Lines)
            {
                if (regionLine.ToString() == line.ToString())
                {
                    var lineText       = line.ToString();
                    var startCharacter = diagnostic.Location.GetMappedLineSpan().Span.Start.Character;
                    if (startCharacter <= lineText.Length)
                    {
                        var partToFind = lineText.Substring(startCharacter);
                        var charOffset = sourceText.Lines[lineOffset].ToString().IndexOf(partToFind, StringComparison.Ordinal);

                        var errorMessage = $"({lineOffset + 1},{charOffset + 1}): error {diagnostic.Id}: {diagnostic.GetMessage()}";

                        return(new SerializableDiagnostic(
                                   start,
                                   end,
                                   errorMessage,
                                   diagnostic.ConvertSeverity(),
                                   diagnostic.Id,
                                   viewport.BufferId));
                    }
                }

                lineOffset++;
            }

            return(null);
        }
Beispiel #2
0
        public static SerializableDiagnostic ToSerializableDiagnostic(
            this Diagnostic diagnostic,
            string message    = null,
            BufferId bufferId = null)
        {
            var diagnosticMessage = diagnostic.GetMessage();

            var startPosition = diagnostic.Location.GetLineSpan().Span.Start;

            var diagnosticFilePath = diagnostic?.Location.SourceTree?.FilePath
                                     ?? bufferId?.FileName // F# doesn't have a source tree
                                     ?? diagnostic?.Location.GetLineSpan().Path;

            var location =
                diagnostic.Location != null
                    ? $"{diagnosticFilePath}({startPosition.Line + 1},{startPosition.Character + 1}): {GetMessagePrefix()}"
                    : null;

            return(new SerializableDiagnostic(diagnostic.Location?.SourceSpan.Start ?? throw new ArgumentException(nameof(diagnostic.Location)),
                                              diagnostic.Location.SourceSpan.End,
                                              message ?? diagnosticMessage,
                                              diagnostic.ConvertSeverity(),
                                              diagnostic.Descriptor.Id,
                                              bufferId,
                                              location));

            string GetMessagePrefix()
            {
                string prefix;

                switch (diagnostic.Severity)
                {
                case RoslynDiagnosticSeverity.Hidden:
                    prefix = "hidden";
                    break;

                case RoslynDiagnosticSeverity.Info:
                    prefix = "info";
                    break;

                case RoslynDiagnosticSeverity.Warning:
                    prefix = "warning";
                    break;

                case RoslynDiagnosticSeverity.Error:
                    prefix = "error";
                    break;

                default:
                    return(null);
                }

                return($"{prefix} {diagnostic.Id}");
            }
        }