-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathError.cs
109 lines (96 loc) · 4.06 KB
/
Error.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/////////////////////////////////////////////////////////////////////////////////
// CodeLab for Paint.NET
// Copyright ©2006 Rick Brewster, Tom Jackson. All Rights Reserved.
// Portions Copyright ©Microsoft Corporation. All Rights Reserved.
//
// THE CODELAB DEVELOPERS MAKE NO WARRANTY OF ANY KIND REGARDING THE CODE. THEY
// SPECIFICALLY DISCLAIM ANY WARRANTY OF FITNESS FOR ANY PARTICULAR PURPOSE OR
// ANY OTHER WARRANTY. THE CODELAB DEVELOPERS DISCLAIM ALL LIABILITY RELATING
// TO THE USE OF THIS CODE. NO LICENSE, EXPRESS OR IMPLIED, BY ESTOPPEL OR
// OTHERWISE, TO ANY INTELLECTUAL PROPERTY RIGHTS IS GRANTED HEREIN.
//
// Latest distribution: https://www.BoltBait.com/pdn/codelab
/////////////////////////////////////////////////////////////////////////////////
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using System;
namespace PdnCodeLab
{
internal sealed class Error : IComparable<Error>
{
private readonly ErrorType errorType;
internal int StartLine { get; }
internal int StartColumn { get; }
internal int EndLine { get; }
internal int EndColumn { get; }
internal string ErrorNumber { get; }
internal string ErrorText { get; }
internal string ErrorUrl { get; }
internal bool IsWarning { get; }
internal string FullError => ToString();
internal static Error NewCodeError(Diagnostic diagnostic)
{
LinePositionSpan span = diagnostic.Location.GetLineSpan().Span;
return new Error(
ErrorType.CSharp,
span.Start.Line - ScriptBuilder.LineOffset,
span.Start.Character - ScriptBuilder.ColumnOffset,
span.End.Line - ScriptBuilder.LineOffset,
span.End.Character - ScriptBuilder.ColumnOffset,
diagnostic.Id,
diagnostic.GetMessage(),
diagnostic.Descriptor.HelpLinkUri,
diagnostic.Severity == DiagnosticSeverity.Warning);
}
internal static Error NewShapeError(int line, int column, string errorText)
{
return new Error(ErrorType.Xaml, line - 1, column - 1, -1, -1, string.Empty, errorText, null, false);
}
internal static Error NewInternalError(string internalError)
{
return new Error(ErrorType.Internal, 0, 0, -1, -1, string.Empty, internalError, null, false);
}
internal static Error NewExceptionError(Exception exception)
{
return new Error(ErrorType.Exception, 0, 0, -1, -1, string.Empty, exception.ToString(), null, false);
}
private Error(ErrorType errorType, int startLine, int startColumn, int endLine, int endColumn, string errorNumber, string errorText, string errorUrl, bool isWarning)
{
this.errorType = errorType;
this.StartLine = startLine;
this.StartColumn = startColumn;
this.EndLine = endLine;
this.EndColumn = endColumn;
this.ErrorNumber = errorNumber;
this.ErrorText = errorText;
this.ErrorUrl = errorUrl;
this.IsWarning = isWarning;
}
public override string ToString()
{
return this.errorType switch
{
ErrorType.CSharp => $"{(this.IsWarning ? "Warning" : "Error")} at line {this.StartLine + 1}: {this.ErrorText} ({this.ErrorNumber})",
ErrorType.Xaml => this.ErrorText,
ErrorType.Internal => $"Internal Error: {this.ErrorText}",
ErrorType.Exception => this.ErrorText,
_ => string.Empty,
};
}
public int CompareTo(Error other)
{
return this.StartLine.CompareTo(other.StartLine);
}
private enum ErrorType
{
CSharp,
Xaml,
Internal,
Exception
}
internal static string GetErrorUrl(string errorNumber)
{
return $"https://msdn.microsoft.com/query/roslyn.query?appId=roslyn&k=k({errorNumber})";
}
}
}