Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename the interval tree types. #73860

Merged
merged 8 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ private static ImmutableArray<TextSpan> AnalyzeCodeBlock(CodeBlockAnalysisContex
return simplifier.Spans;
}

private ImmutableArray<Diagnostic> AnalyzeSemanticModel(SemanticModelAnalysisContext context, int positionOfFirstReducingNullableDirective, TextSpanIntervalTree? codeBlockIntervalTree, TextSpanIntervalTree? possibleNullableImpactIntervalTree)
private ImmutableArray<Diagnostic> AnalyzeSemanticModel(SemanticModelAnalysisContext context, int positionOfFirstReducingNullableDirective, TextSpanMutableIntervalTree? codeBlockIntervalTree, TextSpanMutableIntervalTree? possibleNullableImpactIntervalTree)
{
var root = context.SemanticModel.SyntaxTree.GetCompilationUnitRoot(context.CancellationToken);

using (var simplifier = new NullableImpactingSpanWalker(context.SemanticModel, positionOfFirstReducingNullableDirective, ignoredSpans: codeBlockIntervalTree, context.CancellationToken))
{
simplifier.Visit(root);
possibleNullableImpactIntervalTree ??= new TextSpanIntervalTree();
possibleNullableImpactIntervalTree ??= new TextSpanMutableIntervalTree();
foreach (var interval in simplifier.Spans)
{
possibleNullableImpactIntervalTree.AddIntervalInPlace(interval);
Expand Down Expand Up @@ -144,7 +144,7 @@ SyntaxKind.IfDirectiveTrivia or
SyntaxKind.ElifDirectiveTrivia or
SyntaxKind.ElseDirectiveTrivia)
{
possibleNullableImpactIntervalTree ??= new TextSpanIntervalTree();
possibleNullableImpactIntervalTree ??= new TextSpanMutableIntervalTree();
possibleNullableImpactIntervalTree.AddIntervalInPlace(directive.Span);
}
}
Expand Down Expand Up @@ -173,16 +173,16 @@ private SyntaxTreeState(bool completed, int? positionOfFirstReducingNullableDire
PositionOfFirstReducingNullableDirective = positionOfFirstReducingNullableDirective;
if (!completed)
{
IntervalTree = new TextSpanIntervalTree();
PossibleNullableImpactIntervalTree = new TextSpanIntervalTree();
IntervalTree = new TextSpanMutableIntervalTree();
PossibleNullableImpactIntervalTree = new TextSpanMutableIntervalTree();
}
}

[MemberNotNullWhen(false, nameof(PositionOfFirstReducingNullableDirective), nameof(IntervalTree), nameof(PossibleNullableImpactIntervalTree))]
public bool Completed { get; private set; }
public int? PositionOfFirstReducingNullableDirective { get; }
public TextSpanIntervalTree? IntervalTree { get; }
public TextSpanIntervalTree? PossibleNullableImpactIntervalTree { get; }
public TextSpanMutableIntervalTree? IntervalTree { get; }
public TextSpanMutableIntervalTree? PossibleNullableImpactIntervalTree { get; }

public static SyntaxTreeState Create(bool defaultCompleted, NullableContextOptions compilationOptions, SyntaxTree tree, CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ namespace Microsoft.CodeAnalysis.CSharp.Analyzers.RemoveUnnecessaryNullableDirec
internal sealed class NullableImpactingSpanWalker(
SemanticModel semanticModel,
int positionOfFirstReducingNullableDirective,
TextSpanIntervalTree? ignoredSpans,
TextSpanMutableIntervalTree? ignoredSpans,
CancellationToken cancellationToken) : CSharpSyntaxWalker(SyntaxWalkerDepth.StructuredTrivia), IDisposable
{
private readonly SemanticModel _semanticModel = semanticModel;
private readonly int _positionOfFirstReducingNullableDirective = positionOfFirstReducingNullableDirective;
private readonly TextSpanIntervalTree? _ignoredSpans = ignoredSpans;
private readonly TextSpanMutableIntervalTree? _ignoredSpans = ignoredSpans;
private readonly CancellationToken _cancellationToken = cancellationToken;

private ImmutableArray<TextSpan>.Builder? _spans;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private void AnalyzeCompilation(CompilationStartAnalysisContext context)
/// <see cref="AnalyzeSemanticModel"/>.</returns>
protected abstract bool IsIgnoredCodeBlock(SyntaxNode codeBlock);
protected abstract ImmutableArray<Diagnostic> AnalyzeCodeBlock(CodeBlockAnalysisContext context, SyntaxNode root);
protected abstract ImmutableArray<Diagnostic> AnalyzeSemanticModel(SemanticModelAnalysisContext context, SyntaxNode root, TextSpanIntervalTree? codeBlockIntervalTree);
protected abstract ImmutableArray<Diagnostic> AnalyzeSemanticModel(SemanticModelAnalysisContext context, SyntaxNode root, TextSpanMutableIntervalTree? codeBlockIntervalTree);

public bool TrySimplify(SemanticModel model, SyntaxNode node, [NotNullWhen(true)] out Diagnostic? diagnostic, TSimplifierOptions options, AnalyzerOptions analyzerOptions, CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -229,14 +229,14 @@ private class AnalyzerImpl(SimplifyTypeNamesDiagnosticAnalyzerBase<TLanguageKind
/// </description></item>
/// </list>
/// </summary>
private readonly ConcurrentDictionary<SyntaxTree, (StrongBox<bool> completed, TextSpanIntervalTree? intervalTree)> _codeBlockIntervals = [];
private readonly ConcurrentDictionary<SyntaxTree, (StrongBox<bool> completed, TextSpanMutableIntervalTree? intervalTree)> _codeBlockIntervals = [];

public void AnalyzeCodeBlock(CodeBlockAnalysisContext context)
{
if (_analyzer.IsIgnoredCodeBlock(context.CodeBlock))
return;

var (completed, intervalTree) = _codeBlockIntervals.GetOrAdd(context.CodeBlock.SyntaxTree, _ => (new StrongBox<bool>(false), new TextSpanIntervalTree()));
var (completed, intervalTree) = _codeBlockIntervals.GetOrAdd(context.CodeBlock.SyntaxTree, _ => (new StrongBox<bool>(false), new TextSpanMutableIntervalTree()));
if (completed.Value)
return;

Expand All @@ -256,7 +256,7 @@ public void AnalyzeCodeBlock(CodeBlockAnalysisContext context)
context.ReportDiagnostic(diagnostic);
}

static bool TryProceedWithInterval(bool addIfAvailable, TextSpan span, StrongBox<bool> completed, TextSpanIntervalTree intervalTree)
static bool TryProceedWithInterval(bool addIfAvailable, TextSpan span, StrongBox<bool> completed, TextSpanMutableIntervalTree intervalTree)
{
lock (completed)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal sealed partial class TagSpanIntervalTree<TTag>(SpanTrackingMode spanTra
public static readonly TagSpanIntervalTree<TTag> Empty = new(SpanTrackingMode.EdgeInclusive);

private readonly SpanTrackingMode _spanTrackingMode = spanTrackingMode;
private readonly FlatArrayIntervalTree<TagSpan<TTag>> _tree = FlatArrayIntervalTree<TagSpan<TTag>>.Empty;
private readonly ImmutableIntervalTree<TagSpan<TTag>> _tree = ImmutableIntervalTree<TagSpan<TTag>>.Empty;

public TagSpanIntervalTree(
ITextSnapshot textSnapshot,
Expand All @@ -39,7 +39,7 @@ public TagSpanIntervalTree(
// routines), and allows us to build the balanced tree directly without having to do any additional work.
values.Sort(static (t1, t2) => t1.Span.Start.Position - t2.Span.Start.Position);

_tree = FlatArrayIntervalTree<TagSpan<TTag>>.CreateFromSorted(
_tree = ImmutableIntervalTree<TagSpan<TTag>>.CreateFromSorted(
new IntervalIntrospector(textSnapshot, trackingMode), values);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ private static string GetIndentationStringForPosition(SourceText text, SyntaxFor
private static void AppendFullLine(StringBuilder builder, TextLine line)
=> builder.Append(line.Text!.ToString(line.SpanIncludingLineBreak));

private static (FlatArrayIntervalTree<TextSpan> interpolationInteriorSpans, FlatArrayIntervalTree<TextSpan> restrictedSpans) GetInterpolationSpans(
private static (ImmutableIntervalTree<TextSpan> interpolationInteriorSpans, ImmutableIntervalTree<TextSpan> restrictedSpans) GetInterpolationSpans(
InterpolatedStringExpressionSyntax stringExpression, CancellationToken cancellationToken)
{
var interpolationInteriorSpans = new SegmentedList<TextSpan>();
Expand Down Expand Up @@ -515,8 +515,8 @@ private static (FlatArrayIntervalTree<TextSpan> interpolationInteriorSpans, Flat
}

return (
FlatArrayIntervalTree<TextSpan>.CreateFromUnsorted(new TextSpanIntervalIntrospector(), interpolationInteriorSpans),
FlatArrayIntervalTree<TextSpan>.CreateFromUnsorted(new TextSpanIntervalIntrospector(), restrictedSpans));
ImmutableIntervalTree<TextSpan>.CreateFromUnsorted(new TextSpanIntervalIntrospector(), interpolationInteriorSpans),
ImmutableIntervalTree<TextSpan>.CreateFromUnsorted(new TextSpanIntervalIntrospector(), restrictedSpans));
}

private static InterpolatedStringExpressionSyntax CleanInterpolatedString(
Expand Down Expand Up @@ -630,7 +630,7 @@ private static InterpolatedStringExpressionSyntax CleanInterpolatedString(

private static string ComputeCommonWhitespacePrefix(
ArrayBuilder<TextLine> lines,
FlatArrayIntervalTree<TextSpan> interpolationInteriorSpans)
ImmutableIntervalTree<TextSpan> interpolationInteriorSpans)
{
string? commonLeadingWhitespace = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected override ImmutableArray<Diagnostic> AnalyzeCodeBlock(CodeBlockAnalysis
return simplifier.Diagnostics;
}

protected override ImmutableArray<Diagnostic> AnalyzeSemanticModel(SemanticModelAnalysisContext context, SyntaxNode root, TextSpanIntervalTree? codeBlockIntervalTree)
protected override ImmutableArray<Diagnostic> AnalyzeSemanticModel(SemanticModelAnalysisContext context, SyntaxNode root, TextSpanMutableIntervalTree? codeBlockIntervalTree)
{
var options = context.GetCSharpAnalyzerOptions().GetSimplifierOptions();
if (ShouldSkipAnalysis(context.FilterTree, context.Options, context.SemanticModel.Compilation.Options, GetAllNotifications(options), context.CancellationToken))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal class TypeSyntaxSimplifierWalker : CSharpSyntaxWalker, IDisposable
private readonly SemanticModel _semanticModel;
private readonly CSharpSimplifierOptions _options;
private readonly AnalyzerOptions _analyzerOptions;
private readonly TextSpanIntervalTree? _ignoredSpans;
private readonly TextSpanMutableIntervalTree? _ignoredSpans;
private readonly CancellationToken _cancellationToken;

private ImmutableArray<Diagnostic>.Builder? _diagnostics;
Expand Down Expand Up @@ -70,7 +70,7 @@ public ImmutableArray<Diagnostic>.Builder DiagnosticsBuilder
}
}

public TypeSyntaxSimplifierWalker(CSharpSimplifyTypeNamesDiagnosticAnalyzer analyzer, SemanticModel semanticModel, CSharpSimplifierOptions options, AnalyzerOptions analyzerOptions, TextSpanIntervalTree? ignoredSpans, CancellationToken cancellationToken)
public TypeSyntaxSimplifierWalker(CSharpSimplifyTypeNamesDiagnosticAnalyzer analyzer, SemanticModel semanticModel, CSharpSimplifierOptions options, AnalyzerOptions analyzerOptions, TextSpanMutableIntervalTree? ignoredSpans, CancellationToken cancellationToken)
: base(SyntaxWalkerDepth.StructuredTrivia)
{
_analyzer = analyzer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeFixes.SimplifyTypeNames
Private ReadOnly _semanticModel As SemanticModel
Private ReadOnly _options As VisualBasicSimplifierOptions
Private ReadOnly _analyzerOptions As AnalyzerOptions
Private ReadOnly _ignoredSpans As TextSpanIntervalTree
Private ReadOnly _ignoredSpans As TextSpanMutableIntervalTree
Private ReadOnly _cancellationToken As CancellationToken

Private _diagnostics As ImmutableArray(Of Diagnostic).Builder
Expand Down Expand Up @@ -69,7 +69,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeFixes.SimplifyTypeNames
End Get
End Property

Public Sub New(analyzer As VisualBasicSimplifyTypeNamesDiagnosticAnalyzer, semanticModel As SemanticModel, options As VisualBasicSimplifierOptions, analyzerOptions As AnalyzerOptions, ignoredSpans As TextSpanIntervalTree, cancellationToken As CancellationToken)
Public Sub New(analyzer As VisualBasicSimplifyTypeNamesDiagnosticAnalyzer, semanticModel As SemanticModel, options As VisualBasicSimplifierOptions, analyzerOptions As AnalyzerOptions, ignoredSpans As TextSpanMutableIntervalTree, cancellationToken As CancellationToken)
MyBase.New(SyntaxWalkerDepth.StructuredTrivia)

_analyzer = analyzer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeFixes.SimplifyTypeNames
Return simplifier.Diagnostics
End Function

Protected Overrides Function AnalyzeSemanticModel(context As SemanticModelAnalysisContext, root As SyntaxNode, codeBlockIntervalTree As TextSpanIntervalTree) As ImmutableArray(Of Diagnostic)
Protected Overrides Function AnalyzeSemanticModel(context As SemanticModelAnalysisContext, root As SyntaxNode, codeBlockIntervalTree As TextSpanMutableIntervalTree) As ImmutableArray(Of Diagnostic)
Dim simplifierOptions = context.GetVisualBasicAnalyzerOptions().GetSimplifierOptions()
If (ShouldSkipAnalysis(context.FilterTree, context.Options, context.SemanticModel.Compilation.Options, GetAllNotifications(simplifierOptions), context.CancellationToken)) Then
Return ImmutableArray(Of Diagnostic).Empty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ private sealed class DocumentOutlineViewState
/// Interval-tree view over <see cref="ViewModelItems"/> so that we can quickly determine which of them
/// intersect with a particular position in the document.
/// </summary>
public readonly FlatArrayIntervalTree<DocumentSymbolDataViewModel> ViewModelItemsTree;
public readonly ImmutableIntervalTree<DocumentSymbolDataViewModel> ViewModelItemsTree;

public DocumentOutlineViewState(
ITextSnapshot textSnapshot,
string searchText,
ImmutableArray<DocumentSymbolDataViewModel> viewModelItems,
FlatArrayIntervalTree<DocumentSymbolDataViewModel> viewModelItemsTree)
ImmutableIntervalTree<DocumentSymbolDataViewModel> viewModelItemsTree)
{
TextSnapshot = textSnapshot;
SearchText = searchText;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ private async ValueTask ComputeViewStateAsync(CancellationToken cancellationToke
// models given any position in the file with any particular text snapshot.
using var _ = SegmentedListPool.GetPooledList<DocumentSymbolDataViewModel>(out var models);
AddAllModels(newViewModelItems, models);
var intervalTree = FlatArrayIntervalTree<DocumentSymbolDataViewModel>.CreateFromUnsorted(
var intervalTree = ImmutableIntervalTree<DocumentSymbolDataViewModel>.CreateFromUnsorted(
new IntervalIntrospector(), models);

var newViewState = new DocumentOutlineViewState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ internal sealed partial class InheritanceGlyphManager : IDisposable
/// <summary>
/// Mutable. Must only be accessed from the UI thread.
/// </summary>
private SimpleBinaryIntervalTree<GlyphData, GlyphDataIntrospector> _glyphDataTree;
private SimpleMutableIntervalTree<GlyphData, GlyphDataIntrospector> _glyphDataTree;

public InheritanceGlyphManager(
Workspace workspace,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public static void MergeParts<TClassifiedSpan, TClassifiedSpanIntervalIntrospect
}
}

var semanticPartsTree = FlatArrayIntervalTree<TClassifiedSpan>.CreateFromUnsorted(
var semanticPartsTree = ImmutableIntervalTree<TClassifiedSpan>.CreateFromUnsorted(
default(TClassifiedSpanIntervalIntrospector), semanticSpans);

using var tempBuffer = TemporaryArray<TClassifiedSpan>.Empty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public TextSpan GetSpan(TextChange value)
private readonly Document _oldDocument;
private readonly IDocumentTextDifferencingService _differenceService;

private readonly SimpleBinaryIntervalTree<TextChange, IntervalIntrospector> _totalChangesIntervalTree =
private readonly SimpleMutableIntervalTree<TextChange, IntervalIntrospector> _totalChangesIntervalTree =
BinaryIntervalTree.Create(new IntervalIntrospector(), Array.Empty<TextChange>());

public TextChangeMerger(Document document)
Expand Down Expand Up @@ -78,7 +78,7 @@ public async Task<SourceText> GetFinalMergedTextAsync(CancellationToken cancella
}

private static bool AllChangesCanBeApplied(
SimpleBinaryIntervalTree<TextChange, IntervalIntrospector> cumulativeChanges,
SimpleMutableIntervalTree<TextChange, IntervalIntrospector> cumulativeChanges,
ImmutableArray<TextChange> currentChanges)
{
using var overlappingSpans = TemporaryArray<TextChange>.Empty;
Expand All @@ -91,7 +91,7 @@ private static bool AllChangesCanBeApplied(
}

private static bool AllChangesCanBeApplied(
SimpleBinaryIntervalTree<TextChange, IntervalIntrospector> cumulativeChanges,
SimpleMutableIntervalTree<TextChange, IntervalIntrospector> cumulativeChanges,
ImmutableArray<TextChange> currentChanges,
ref TemporaryArray<TextChange> overlappingSpans,
ref TemporaryArray<TextChange> intersectingSpans)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public async Task<Document> AddImportsAsync(
var generator = document.GetRequiredLanguageService<SyntaxGenerator>();

// Create a simple interval tree for simplification spans.
var spansTree = new TextSpanIntervalTree(spans);
var spansTree = new TextSpanMutableIntervalTree(spans);

// Only dive deeper into nodes that actually overlap with the span we care about. And also only include
// those child nodes that themselves overlap with the span. i.e. if we have:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private async Task<Document> ReduceCoreAsync(
CancellationToken cancellationToken)
{
// Create a simple interval tree for simplification spans.
var spansTree = new TextSpanIntervalTree(spans);
var spansTree = new TextSpanMutableIntervalTree(spans);

var root = (TCompilationUnitSyntax)await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

Expand Down
Loading
Loading