-
Notifications
You must be signed in to change notification settings - Fork 509
/
DiagnosticExtensions.cs
70 lines (61 loc) · 2.79 KB
/
DiagnosticExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using Microsoft.CodeAnalysis;
using Microsoft.DotNet.Try.Protocol;
using RoslynDiagnosticSeverity = Microsoft.CodeAnalysis.DiagnosticSeverity;
using TdnDiagnosticSeverity = Microsoft.DotNet.Try.Protocol.DiagnosticSeverity;
namespace Microsoft.DotNet.Try.Project
{
public static class DiagnosticExtensions
{
public static TdnDiagnosticSeverity ConvertSeverity(this Diagnostic diagnostic)
{
return (TdnDiagnosticSeverity) diagnostic.Severity;
}
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}";
}
}
}
}