Skip to content

Commit

Permalink
REvert
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi committed Jun 6, 2023
1 parent f51628d commit 9513cf5
Showing 1 changed file with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,18 @@ namespace Roslyn.Utilities
///
/// also, note that this table is not thread safe.
/// </summary>
internal class AnnotationTable<TAnnotation>(string annotationKind) where TAnnotation : class
internal class AnnotationTable<TAnnotation> where TAnnotation : class
{
private int _globalId;

private readonly Dictionary<TAnnotation, SyntaxAnnotation> _realAnnotationMap = new();
private readonly Dictionary<string, TAnnotation> _annotationMap = new();

private readonly string _annotationKind;

public AnnotationTable(string annotationKind)
=> _annotationKind = annotationKind;

private IEnumerable<SyntaxAnnotation> GetOrCreateRealAnnotations(TAnnotation[] annotations)
{
foreach (var annotation in annotations)
Expand All @@ -46,7 +51,7 @@ private SyntaxAnnotation GetOrCreateRealAnnotation(TAnnotation annotation)
var id = Interlocked.Increment(ref _globalId);
var idString = id.ToString();

realAnnotation = new SyntaxAnnotation(annotationKind, idString);
realAnnotation = new SyntaxAnnotation(_annotationKind, idString);
_annotationMap.Add(idString, annotation);
_realAnnotationMap.Add(annotation, realAnnotation);
}
Expand Down Expand Up @@ -109,16 +114,16 @@ private IEnumerable<TAnnotation> GetAnnotations(IEnumerable<SyntaxAnnotation> re
}

public IEnumerable<TAnnotation> GetAnnotations(SyntaxNode node)
=> GetAnnotations(node.GetAnnotations(annotationKind));
=> GetAnnotations(node.GetAnnotations(_annotationKind));

public IEnumerable<TAnnotation> GetAnnotations(SyntaxToken token)
=> GetAnnotations(token.GetAnnotations(annotationKind));
=> GetAnnotations(token.GetAnnotations(_annotationKind));

public IEnumerable<TAnnotation> GetAnnotations(SyntaxTrivia trivia)
=> GetAnnotations(trivia.GetAnnotations(annotationKind));
=> GetAnnotations(trivia.GetAnnotations(_annotationKind));

public IEnumerable<TAnnotation> GetAnnotations(SyntaxNodeOrToken nodeOrToken)
=> GetAnnotations(nodeOrToken.GetAnnotations(annotationKind));
=> GetAnnotations(nodeOrToken.GetAnnotations(_annotationKind));

public IEnumerable<TSpecificAnnotation> GetAnnotations<TSpecificAnnotation>(SyntaxNode node) where TSpecificAnnotation : TAnnotation
=> this.GetAnnotations(node).OfType<TSpecificAnnotation>();
Expand All @@ -133,16 +138,16 @@ public IEnumerable<TSpecificAnnotation> GetAnnotations<TSpecificAnnotation>(Synt
=> this.GetAnnotations(nodeOrToken).OfType<TSpecificAnnotation>();

public bool HasAnnotations(SyntaxNode node)
=> node.HasAnnotations(annotationKind);
=> node.HasAnnotations(_annotationKind);

public bool HasAnnotations(SyntaxToken token)
=> token.HasAnnotations(annotationKind);
=> token.HasAnnotations(_annotationKind);

public bool HasAnnotations(SyntaxTrivia trivia)
=> trivia.HasAnnotations(annotationKind);
=> trivia.HasAnnotations(_annotationKind);

public bool HasAnnotations(SyntaxNodeOrToken nodeOrToken)
=> nodeOrToken.HasAnnotations(annotationKind);
=> nodeOrToken.HasAnnotations(_annotationKind);

public bool HasAnnotations<TSpecificAnnotation>(SyntaxNode node) where TSpecificAnnotation : TAnnotation
=> this.GetAnnotations(node).OfType<TSpecificAnnotation>().Any();
Expand All @@ -169,27 +174,27 @@ public bool HasAnnotation(SyntaxNodeOrToken nodeOrToken, TAnnotation annotation)
=> nodeOrToken.HasAnnotation(this.GetRealAnnotation(annotation));

public IEnumerable<SyntaxNodeOrToken> GetAnnotatedNodesAndTokens(SyntaxNode node)
=> node.GetAnnotatedNodesAndTokens(annotationKind);
=> node.GetAnnotatedNodesAndTokens(_annotationKind);

public IEnumerable<SyntaxNode> GetAnnotatedNodes(SyntaxNode node)
=> node.GetAnnotatedNodesAndTokens(annotationKind).Where(nt => nt.IsNode).Select(nt => nt.AsNode()!);
=> node.GetAnnotatedNodesAndTokens(_annotationKind).Where(nt => nt.IsNode).Select(nt => nt.AsNode()!);

public IEnumerable<SyntaxToken> GetAnnotatedTokens(SyntaxNode node)
=> node.GetAnnotatedNodesAndTokens(annotationKind).Where(nt => nt.IsToken).Select(nt => nt.AsToken());
=> node.GetAnnotatedNodesAndTokens(_annotationKind).Where(nt => nt.IsToken).Select(nt => nt.AsToken());

public IEnumerable<SyntaxTrivia> GetAnnotatedTrivia(SyntaxNode node)
=> node.GetAnnotatedTrivia(annotationKind);
=> node.GetAnnotatedTrivia(_annotationKind);

public IEnumerable<SyntaxNodeOrToken> GetAnnotatedNodesAndTokens<TSpecificAnnotation>(SyntaxNode node) where TSpecificAnnotation : TAnnotation
=> node.GetAnnotatedNodesAndTokens(annotationKind).Where(this.HasAnnotations<TSpecificAnnotation>);
=> node.GetAnnotatedNodesAndTokens(_annotationKind).Where(this.HasAnnotations<TSpecificAnnotation>);

public IEnumerable<SyntaxNode> GetAnnotatedNodes<TSpecificAnnotation>(SyntaxNode node) where TSpecificAnnotation : TAnnotation
=> node.GetAnnotatedNodesAndTokens(annotationKind).Where(nt => nt.IsNode && this.HasAnnotations<TSpecificAnnotation>(nt)).Select(nt => nt.AsNode()!);
=> node.GetAnnotatedNodesAndTokens(_annotationKind).Where(nt => nt.IsNode && this.HasAnnotations<TSpecificAnnotation>(nt)).Select(nt => nt.AsNode()!);

public IEnumerable<SyntaxToken> GetAnnotatedTokens<TSpecificAnnotation>(SyntaxNode node) where TSpecificAnnotation : TAnnotation
=> node.GetAnnotatedNodesAndTokens(annotationKind).Where(nt => nt.IsToken && this.HasAnnotations<TSpecificAnnotation>(nt)).Select(nt => nt.AsToken());
=> node.GetAnnotatedNodesAndTokens(_annotationKind).Where(nt => nt.IsToken && this.HasAnnotations<TSpecificAnnotation>(nt)).Select(nt => nt.AsToken());

public IEnumerable<SyntaxTrivia> GetAnnotatedTrivia<TSpecificAnnotation>(SyntaxNode node) where TSpecificAnnotation : TAnnotation
=> node.GetAnnotatedTrivia(annotationKind).Where(this.HasAnnotations<TSpecificAnnotation>);
=> node.GetAnnotatedTrivia(_annotationKind).Where(this.HasAnnotations<TSpecificAnnotation>);
}
}

0 comments on commit 9513cf5

Please sign in to comment.