-
-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: diagnostics refactoring (#919)
- Loading branch information
Showing
4 changed files
with
54 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System.Collections; | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace Riok.Mapperly.Diagnostics; | ||
|
||
public class DiagnosticCollection : IReadOnlyCollection<Diagnostic> | ||
{ | ||
private readonly List<Diagnostic> _diagnostics = new(); | ||
private readonly Location _defaultLocation; | ||
|
||
internal DiagnosticCollection(Location defaultLocation) | ||
{ | ||
_defaultLocation = defaultLocation; | ||
} | ||
|
||
public IEnumerator<Diagnostic> GetEnumerator() => _diagnostics.GetEnumerator(); | ||
|
||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
|
||
public int Count => _diagnostics.Count; | ||
|
||
internal void ReportDiagnostic(DiagnosticDescriptor descriptor, ISymbol? location = null, params object[] messageArgs) | ||
{ | ||
// cannot use the symbol since it would break the incremental generator | ||
// due to being different for each compilation. | ||
for (var i = 0; i < messageArgs.Length; i++) | ||
{ | ||
if (messageArgs[i] is ISymbol symbol) | ||
{ | ||
messageArgs[i] = symbol.ToDisplayString(); | ||
} | ||
} | ||
|
||
var syntaxNode = location?.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax(); | ||
var nodeLocation = syntaxNode?.GetLocation(); | ||
_diagnostics.Add(Diagnostic.Create(descriptor, nodeLocation ?? _defaultLocation, messageArgs)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters