本文整理汇总了C#中SyntaxNode.FindToken方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxNode.FindToken方法的具体用法?C# SyntaxNode.FindToken怎么用?C# SyntaxNode.FindToken使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SyntaxNode
的用法示例。
在下文中一共展示了SyntaxNode.FindToken方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Create
public static StringSplitter Create(
Document document, int position,
SyntaxTree syntaxTree, SyntaxNode root, SourceText sourceText,
bool useTabs, int tabSize, CancellationToken cancellationToken)
{
var token = root.FindToken(position);
if (token.IsKind(SyntaxKind.StringLiteralToken))
{
return new SimpleStringSplitter(
document, position, syntaxTree, root,
sourceText, token, useTabs, tabSize,
cancellationToken);
}
var interpolatedStringExpression = TryGetInterpolatedStringExpression(token, position);
if (interpolatedStringExpression != null)
{
return new InterpolatedStringSplitter(
document, position, syntaxTree, root,
sourceText, interpolatedStringExpression,
useTabs, tabSize, cancellationToken);
}
return null;
}
示例2: GetCastNode
private static CastExpressionSyntax GetCastNode(SyntaxNode root, SemanticModel model, TextSpan span, CancellationToken cancellationToken)
{
var token = root.FindToken(span.Start);
if (!token.Span.IntersectsWith(span))
{
return null;
}
return token.GetAncestors<CastExpressionSyntax>()
.FirstOrDefault(c => c.Span.IntersectsWith(span) && c.IsUnnecessaryCast(model, cancellationToken));
}
示例3: FindNode
public override SyntaxNode FindNode(SyntaxNode root, TextSpan span)
{
var result = root.FindToken(span.Start).Parent;
while (result.Span != span)
{
result = result.Parent;
Assert.NotNull(result);
}
return result;
}
示例4: GetFormattingSpan
public static TextSpan GetFormattingSpan(SyntaxNode root, TextSpan span)
{
Contract.ThrowIfNull(root);
var startToken = root.FindToken(span.Start).GetPreviousToken();
var endToken = root.FindTokenFromEnd(span.End).GetNextToken();
var startPosition = startToken.SpanStart;
var endPosition = endToken.RawKind == 0 ? root.Span.End : endToken.Span.End;
return TextSpan.FromBounds(startPosition, endPosition);
}
示例5: TryGetNode
private bool TryGetNode(SyntaxNode root, TextSpan span, out SyntaxNode node)
{
node = null;
var ancestors = root.FindToken(span.Start).GetAncestors<SyntaxNode>();
if (!ancestors.Any())
{
return false;
}
node = ancestors.FirstOrDefault(n => n.Span.Contains(span) && n != root);
return node != null;
}
示例6: GetNodeToSimplify
internal static SyntaxNode GetNodeToSimplify(SyntaxNode root, SemanticModel model, TextSpan span, OptionSet optionSet, out string diagnosticId, CancellationToken cancellationToken)
{
diagnosticId = null;
var token = root.FindToken(span.Start, findInsideTrivia: true);
if (!token.Span.IntersectsWith(span))
{
return null;
}
foreach (var n in token.GetAncestors<SyntaxNode>())
{
if (n.Span.IntersectsWith(span) && CanSimplifyTypeNameExpression(model, n, optionSet, span, out diagnosticId, cancellationToken))
{
return n;
}
}
return null;
}
示例7: GetTargetNodes
protected virtual IEnumerable<SyntaxNode> GetTargetNodes(SyntaxNode root, TextSpan span)
{
var token = root.FindToken(span.Start);
if (!token.Span.IntersectsWith(span))
{
yield break;
}
var nodes = token.GetAncestors<SyntaxNode>().Where(IsCandidate);
foreach (var node in nodes)
{
var name = GetTargetNode(node);
if (name != null)
{
yield return name;
}
}
}
示例8: GetContainingTypeDeclaration
public SyntaxNode GetContainingTypeDeclaration(SyntaxNode root, int position)
{
if (root == null)
{
throw new ArgumentNullException(nameof(root));
}
if (position < 0 || position > root.Span.End)
{
throw new ArgumentOutOfRangeException(nameof(position));
}
return root
.FindToken(position)
.GetAncestors<SyntaxNode>()
.FirstOrDefault(n => n is BaseTypeDeclarationSyntax || n is DelegateDeclarationSyntax);
}
示例9: GetTokensFromText
private static ImmutableArray<SyntaxToken> GetTokensFromText(
SyntaxNode root, List<int> positions, string text, Func<SyntaxToken, bool> candidate, CancellationToken cancellationToken)
{
var result = ImmutableArray.CreateBuilder<SyntaxToken>();
foreach (var index in positions)
{
cancellationToken.ThrowIfCancellationRequested();
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);
}
}
return result.ToImmutable();
}
示例10: GetTokensFromText
private static IEnumerable<SyntaxToken> GetTokensFromText(
SyntaxNode root, List<int> positions, string text, Func<SyntaxToken, bool> candidate, CancellationToken cancellationToken)
{
foreach (var index in positions)
{
cancellationToken.ThrowIfCancellationRequested();
var token = root.FindToken(index, findInsideTrivia: true);
var span = token.Span;
if (!token.IsMissing && span.Start == index && span.Length == text.Length && candidate(token))
{
yield return token;
}
}
}
示例11: ComputeDestinationSpan
private TextSpan ComputeDestinationSpan(SyntaxNode insertionRoot, string insertionText)
{
var targetToken = insertionRoot.GetAnnotatedTokens(_otherAnnotation).FirstOrNullable();
var text = insertionRoot.GetText();
var line = text.Lines.GetLineFromPosition(targetToken.Value.Span.End);
// DevDiv 958235:
//
// void foo()
// {
// }
// override $$
//
// If our text edit includes the trailing trivia of the close brace of foo(),
// that token will be reconstructed. The ensuing tree diff will then count
// the { } as replaced even though we didn't want it to. If the user
// has collapsed the outline for foo, that means we'll edit the outlined
// region and weird stuff will happen. Therefore, we'll start with the first
// token on the line in order to leave the token and its trivia alone.
var firstToken = insertionRoot.FindToken(line.GetFirstNonWhitespacePosition().Value);
return TextSpan.FromBounds(firstToken.SpanStart, line.End);
}
示例12: GetTokens
private static void GetTokens(SyntaxNode root, TextSpan span, out SyntaxToken token1, out SyntaxToken token2)
{
// get tokens within given span
token1 = root.FindToken(span.Start);
token2 = root.FindTokenFromEnd(span.End);
// It is possible the given span doesn't have any tokens in them. In that case,
// make tokens to be the adjacent ones to the given span.
if (span.End < token1.Span.Start)
{
token1 = token1.GetPreviousToken();
}
if (token2.Span.End < span.Start)
{
token2 = token2.GetNextToken();
}
}
示例13: GetContainingMemberDeclaration
public SyntaxNode GetContainingMemberDeclaration(SyntaxNode root, int position, bool useFullSpan = true)
{
Contract.ThrowIfNull(root, "root");
Contract.ThrowIfTrue(position < 0 || position > root.FullSpan.End, "position");
var end = root.FullSpan.End;
if (end == 0)
{
// empty file
return null;
}
// make sure position doesn't touch end of root
position = Math.Min(position, end - 1);
var node = root.FindToken(position).Parent;
while (node != null)
{
if (useFullSpan || node.Span.Contains(position))
{
var kind = node.Kind();
if ((kind != SyntaxKind.GlobalStatement) && (kind != SyntaxKind.IncompleteMember) && (node is MemberDeclarationSyntax))
{
return node;
}
}
node = node.Parent;
}
return null;
}
示例14: GetTokensAroundSpan
/// <summary>
/// retrieve 4 tokens around span like below.
///
/// [previousToken][startToken][SPAN][endToken][nextToken]
/// </summary>
private static void GetTokensAroundSpan(
SyntaxNode root, TextSpan span,
out SyntaxToken previousToken,
out SyntaxToken startToken,
out SyntaxToken endToken,
out SyntaxToken nextToken)
{
// get tokens at the edges of the span
startToken = root.FindToken(span.Start, findInsideTrivia: true);
endToken = root.FindTokenFromEnd(span.End, findInsideTrivia: true);
// there must be tokens at each edge
Contract.ThrowIfTrue(startToken.RawKind == 0 || endToken.RawKind == 0);
// get previous and next tokens around span
previousToken = startToken.GetPreviousToken(includeZeroWidth: true, includeSkipped: true, includeDirectives: true, includeDocumentationComments: true);
nextToken = endToken.GetNextToken(includeZeroWidth: true, includeSkipped: true, includeDirectives: true, includeDocumentationComments: true);
}
示例15: CheckFindToken
private void CheckFindToken(SyntaxNode node)
{
for (int i = 0; i < node.FullSpan.End; i++)
{
var token = node.FindToken(i);
Assert.Equal(true, token.FullSpan.Contains(i));
}
}