Skip to content

Commit

Permalink
Make strongly typed
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi committed Dec 19, 2024
1 parent e5777b3 commit a1438f1
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ internal partial class InlineHintsTaggerProvider
{
/// <summary>
/// The computed adornment tag for an inline hint, along with information needed to determine if it can be reused.
/// This is created and cached on <see cref="InlineHintDataTag.AdditionalData"/> on demand so that we only create
/// adornment tags once and reuse as long as possible.
/// This is created and cached on <see cref="InlineHintDataTag{TAdditionalInformation}.AdditionalData"/> on demand
/// so that we only create adornment tags once and reuse as long as possible.
/// </summary>
/// <param name="classified">Whether or not the adornment tag was classified. If the option for this changes, this
/// cached tag should not be reused.</param>
Expand Down
13 changes: 6 additions & 7 deletions src/EditorFeatures/Core.Wpf/InlineHints/InlineHintsTagger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Runtime.CompilerServices;
using Microsoft.CodeAnalysis.Collections;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
using Microsoft.CodeAnalysis.Editor.Tagging;
Expand All @@ -25,13 +24,13 @@ namespace Microsoft.CodeAnalysis.Editor.InlineHints;
internal partial class InlineHintsTaggerProvider
{
/// <summary>
/// The purpose of this tagger is to convert the <see cref="InlineHintDataTag"/> to the <see
/// The purpose of this tagger is to convert the <see cref="InlineHintDataTag{TAdditionalInformation}"/> to the <see
/// cref="InlineHintsTag"/>, which actually creates the UIElement. It reacts to tags changing and updates the
/// adornments accordingly.
/// </summary>
private sealed class InlineHintsTagger : EfficientTagger<IntraTextAdornmentTag>
{
private readonly EfficientTagger<InlineHintDataTag> _underlyingTagger;
private readonly EfficientTagger<InlineHintDataTag<CachedAdornmentTagSpan>> _underlyingTagger;

private readonly IClassificationFormatMap _formatMap;

Expand All @@ -50,7 +49,7 @@ public InlineHintsTagger(
InlineHintsTaggerProvider taggerProvider,
IWpfTextView textView,
ITextBuffer subjectBuffer,
EfficientTagger<InlineHintDataTag> tagger)
EfficientTagger<InlineHintDataTag<CachedAdornmentTagSpan>> tagger)
{
_taggerProvider = taggerProvider;

Expand Down Expand Up @@ -125,7 +124,7 @@ public override void AddTags(

var colorHints = _taggerProvider.EditorOptionsService.GlobalOptions.GetOption(InlineHintsViewOptionsStorage.ColorHints, document.Project.Language);

using var _1 = SegmentedListPool.GetPooledList<TagSpan<InlineHintDataTag>>(out var dataTagSpans);
using var _1 = SegmentedListPool.GetPooledList<TagSpan<InlineHintDataTag<CachedAdornmentTagSpan>>>(out var dataTagSpans);
_underlyingTagger.AddTags(spans, dataTagSpans);

// Presize so we can add the elements below without continually resizing.
Expand All @@ -147,10 +146,10 @@ public override void AddTags(
}

private TagSpan<IntraTextAdornmentTag> GetOrCreateAdornmentTagsSpan(
TagSpan<InlineHintDataTag> dataTagSpan, bool classify, TextFormattingRunProperties format)
TagSpan<InlineHintDataTag<CachedAdornmentTagSpan>> dataTagSpan, bool classify, TextFormattingRunProperties format)
{
// If we've never computed the adornment info, or options have changed, then compute and cache the new information.
var cachedTagInformation = (CachedAdornmentTagSpan?)dataTagSpan.Tag.AdditionalData;
var cachedTagInformation = dataTagSpan.Tag.AdditionalData;
if (cachedTagInformation is null || cachedTagInformation.Classified != classify || cachedTagInformation.Format != format)
{
var adornmentSpan = dataTagSpan.Span;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ internal sealed partial class InlineHintsTaggerProvider(
/// its data tags. Then, on demand, we convert and cache those data tags into adornment tags and pass on the
/// results.
/// </summary>
private readonly InlineHintsDataTaggerProvider _dataTaggerProvider = new(taggerHost, inlineHintKeyProcessor);
private readonly InlineHintsDataTaggerProvider<CachedAdornmentTagSpan> _dataTaggerProvider = new(taggerHost, inlineHintKeyProcessor);

public ITagger<T>? CreateTagger<T>(ITextView textView, ITextBuffer subjectBuffer) where T : ITag
{
Expand Down
13 changes: 8 additions & 5 deletions src/EditorFeatures/Core/InlineHints/InlineHintDataTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ namespace Microsoft.CodeAnalysis.Editor.InlineHints;
/// The simple tag that only holds information regarding the associated parameter name
/// for the argument
/// </summary>
internal sealed class InlineHintDataTag(InlineHintsDataTaggerProvider provider, ITextSnapshot snapshot, InlineHint hint) : ITag, IEquatable<InlineHintDataTag>
internal sealed class InlineHintDataTag<TAdditionalInformation>(
InlineHintsDataTaggerProvider<TAdditionalInformation> provider, ITextSnapshot snapshot, InlineHint hint)
: ITag, IEquatable<InlineHintDataTag<TAdditionalInformation>>
where TAdditionalInformation : class
{
private readonly InlineHintsDataTaggerProvider _provider = provider;
private readonly InlineHintsDataTaggerProvider<TAdditionalInformation> _provider = provider;

/// <summary>
/// The snapshot this tag was created against.
Expand All @@ -31,17 +34,17 @@ internal sealed class InlineHintDataTag(InlineHintsDataTaggerProvider provider,
/// Additional data that can be attached to the tag. For example, the view tagger uses this to attach the adornment
/// tag information so it can be created and cached on demand.
/// </summary>
public object? AdditionalData;
public TAdditionalInformation? AdditionalData;

// Intentionally throwing, we have never supported this facility, and there is no contract around placing
// these tags in sets or maps.
public override int GetHashCode()
=> throw new NotImplementedException();

public override bool Equals(object? obj)
=> obj is InlineHintDataTag tag && Equals(tag);
=> obj is InlineHintDataTag<TAdditionalInformation> tag && Equals(tag);

public bool Equals(InlineHintDataTag? other)
public bool Equals(InlineHintDataTag<TAdditionalInformation>? other)
{
if (other is null)
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Microsoft.CodeAnalysis.Editor.InlineHints;

internal partial class InlineHintsDataTaggerProvider
internal partial class InlineHintsDataTaggerProvider<TAdditionalInformation>
{
private sealed class InlineHintKeyProcessorEventSource(IInlineHintKeyProcessor? inlineHintKeyProcessor) : AbstractTaggerEventSource
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ namespace Microsoft.CodeAnalysis.Editor.InlineHints;
/// <summary>
/// The TaggerProvider that calls upon the service in order to locate the spans and names
/// </summary>
internal sealed partial class InlineHintsDataTaggerProvider(
internal sealed partial class InlineHintsDataTaggerProvider<TAdditionalInformation>(
TaggerHost taggerHost,
IInlineHintKeyProcessor inlineHintKeyProcessor)
: AsynchronousViewportTaggerProvider<InlineHintDataTag>(taggerHost, FeatureAttribute.InlineHints)
: AsynchronousViewportTaggerProvider<InlineHintDataTag<TAdditionalInformation>>(taggerHost, FeatureAttribute.InlineHints)
where TAdditionalInformation : class
{
private readonly IInlineHintKeyProcessor _inlineHintKeyProcessor = inlineHintKeyProcessor;

Expand Down Expand Up @@ -63,7 +64,7 @@ protected override ITaggerEventSource CreateEventSource(ITextView textView, ITex
}

protected override async Task ProduceTagsAsync(
TaggerContext<InlineHintDataTag> context,
TaggerContext<InlineHintDataTag<TAdditionalInformation>> context,
DocumentSnapshotSpan spanToTag,
CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -95,12 +96,12 @@ protected override async Task ProduceTagsAsync(
if (hint.DisplayParts.Sum(p => p.ToString().Length) == 0)
continue;

context.AddTag(new TagSpan<InlineHintDataTag>(
context.AddTag(new TagSpan<InlineHintDataTag<TAdditionalInformation>>(
hint.Span.ToSnapshotSpan(snapshotSpan.Snapshot),
new InlineHintDataTag(this, snapshotSpan.Snapshot, hint)));
new(this, snapshotSpan.Snapshot, hint)));
}
}

protected override bool TagEquals(InlineHintDataTag tag1, InlineHintDataTag tag2)
protected override bool TagEquals(InlineHintDataTag<TAdditionalInformation> tag1, InlineHintDataTag<TAdditionalInformation> tag2)
=> tag1.Equals(tag2);
}

0 comments on commit a1438f1

Please sign in to comment.