Clean up RazorSyntaxTree.Diagnostics computation

This commit is contained in:
Dustin Campbell 2024-08-26 13:25:45 -07:00
Родитель 2476b630ef
Коммит 8cba23587a
2 изменённых файлов: 27 добавлений и 22 удалений

Просмотреть файл

@ -4,9 +4,9 @@
#nullable disable
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Razor.Language.Legacy;
using Microsoft.AspNetCore.Razor.Language.Syntax;
using Microsoft.AspNetCore.Razor.PooledObjects;
namespace Microsoft.AspNetCore.Razor.Language;
@ -42,20 +42,32 @@ public sealed class RazorSyntaxTree
{
if (_allDiagnostics == null)
{
var allDiagnostics = new HashSet<RazorDiagnostic>();
using var allDiagnostics = new PooledArrayBuilder<RazorDiagnostic>();
using var pooledList = ListPool<RazorDiagnostic>.GetPooledObject(out var rootDiagnostics);
using var diagnosticSet = new PooledHashSet<RazorDiagnostic>();
foreach (var diagnostic in _diagnostics)
{
allDiagnostics.Add(diagnostic);
if (diagnosticSet.Add(diagnostic))
{
allDiagnostics.Add(diagnostic);
}
}
var rootDiagnostics = Root.GetAllDiagnostics();
for (var i = 0; i < rootDiagnostics.Count; i++)
Root.CollectAllDiagnostics(rootDiagnostics);
if (rootDiagnostics.Count > 0)
{
allDiagnostics.Add(rootDiagnostics[i]);
foreach (var diagnostic in rootDiagnostics)
{
if (diagnosticSet.Add(diagnostic))
{
allDiagnostics.Add(diagnostic);
}
}
}
var allOrderedDiagnostics = allDiagnostics.OrderBy(diagnostic => diagnostic.Span.AbsoluteIndex);
_allDiagnostics = allOrderedDiagnostics.ToArray();
_allDiagnostics = diagnosticSet.OrderByAsArray(static d => d.Span.AbsoluteIndex);
}
return _allDiagnostics;

Просмотреть файл

@ -57,13 +57,13 @@ internal static class SyntaxNodeExtensions
/// </summary>
/// <typeparam name="TNode">The type of syntax node.</typeparam>
/// <param name="node">The syntax node.</param>
/// <returns>The list of <see cref="RazorDiagnostic"/>s.</returns>
public static IReadOnlyList<RazorDiagnostic> GetAllDiagnostics<TNode>(this TNode node) where TNode : SyntaxNode
/// <param name="list"></param>
/// <returns>The list of <see cref="RazorDiagnostic">RazorDiagnostics</see>.</returns>
public static void CollectAllDiagnostics<TNode>(this TNode node, List<RazorDiagnostic> list)
where TNode : SyntaxNode
{
var walker = new DiagnosticSyntaxWalker();
var walker = new DiagnosticSyntaxWalker(list);
walker.Visit(node);
return walker.Diagnostics;
}
public static SourceLocation GetSourceLocation(this SyntaxNode node, RazorSourceDocument source)
@ -211,16 +211,9 @@ internal static class SyntaxNodeExtensions
return writer.ToString();
}
private class DiagnosticSyntaxWalker : SyntaxWalker
private sealed class DiagnosticSyntaxWalker(List<RazorDiagnostic> diagnostics) : SyntaxWalker
{
private readonly List<RazorDiagnostic> _diagnostics;
public DiagnosticSyntaxWalker()
{
_diagnostics = new List<RazorDiagnostic>();
}
public IReadOnlyList<RazorDiagnostic> Diagnostics => _diagnostics;
private readonly List<RazorDiagnostic> _diagnostics = diagnostics ?? [];
public override void Visit(SyntaxNode node)
{