private static ImmutableArray<SyntaxToken> GetTokensFromText( ISyntaxFactsService syntaxFacts, SyntaxNode root, SourceText content, string text, Func<SyntaxToken, bool> candidate, CancellationToken cancellationToken) { var result = ImmutableArray.CreateBuilder<SyntaxToken>(); var index = 0; while ((index = content.IndexOf(text, index, syntaxFacts.IsCaseSensitive)) >= 0) { cancellationToken.ThrowIfCancellationRequested(); var nextIndex = index + text.Length; var token = root.FindToken(index, findInsideTrivia: true); var span = token.Span; if (!token.IsMissing && span.Start == index && span.Length == text.Length && candidate(token)) { result.Add(token); } nextIndex = Math.Max(nextIndex, token.SpanStart); index = nextIndex; } return result.ToImmutable(); }