本文整理汇总了C#中Microsoft.CodeAnalysis.SyntaxNode.DescendantTokens方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxNode.DescendantTokens方法的具体用法?C# SyntaxNode.DescendantTokens怎么用?C# SyntaxNode.DescendantTokens使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.SyntaxNode
的用法示例。
在下文中一共展示了SyntaxNode.DescendantTokens方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetTokens
public static IList<SyntaxToken> GetTokens(SyntaxNode node)
{
if (node.Language == LanguageNames.CSharp)
{
return node.DescendantTokens().Where(t => !SkipCSharpToken(t)).ToList();
}
else
{
return node.DescendantTokens().Where(t => !SkipVisualBasicToken(t)).ToList();
}
}
示例2: GetAsyncOrAwaitTokens
private static IEnumerable<SyntaxToken> GetAsyncOrAwaitTokens(SyntaxNode node)
{
return node.DescendantTokens()
.Where(token =>
token.IsKind(SyntaxKind.IdentifierToken) &&
AsyncOrAwait.Contains(token.ToString()));
}
示例3: GetAsyncOrAwaitTokens
private static IEnumerable<SyntaxToken> GetAsyncOrAwaitTokens(SyntaxNode node)
{
return from token in node.DescendantTokens()
where token.IsKind(SyntaxKind.IdentifierToken) &&
AsyncOrAwait.Contains(token.ToString())
select token;
}
示例4: TryGetVariable
private bool TryGetVariable(SyntaxNode node, out PatternVariable variable)
{
variable = null;
var tokens = node.DescendantTokens().Take(2).ToImmutableArray();
if (tokens.Length != 1)
return false;
var token = tokens[0];
return TryGetVariable(token, out variable);
}
示例5: FixOpenBraces
/// <summary>
/// Fix the new lines around open brace tokens. An open brace should be followed by content, not a blank
/// line. Remove those here.
/// </summary>
private static SyntaxNode FixOpenBraces(SyntaxNode syntaxNode)
{
// Look for the open brace tokens that are followed by empty blank lines. The new lines will
// be attached to the next nodes, not the open brace token.
var tokensToReplace = syntaxNode.DescendantTokens().Where((token) =>
{
var nextToken = token.GetNextToken();
if (token.Kind() != SyntaxKind.OpenBraceToken || !nextToken.HasLeadingTrivia)
{
return false;
}
return IsSimpleNewLine(nextToken.LeadingTrivia, 0);
}).Select(x => x.GetNextToken());
return syntaxNode.ReplaceTokens(tokensToReplace, (_, y) => FixOpenBraceNextToken(y));
}
示例6: ProcessTokenAfterOpenBrace
SyntaxNode ISyntaxRule.Process(SyntaxNode root)
{
var braceTokenNeighbors = root.DescendantTokens()
.Where(t =>
t.IsKind(SyntaxKind.OpenBraceToken)
|| (t.IsKind(SyntaxKind.CloseBraceToken) && t.GetPreviousToken().IsKind(SyntaxKind.OpenBraceToken))
)
.ToDictionary(
t => t.IsKind(SyntaxKind.OpenBraceToken) ? t.GetNextToken() : t.GetPreviousToken(),
t => t.Kind()
);
var processed = root.ReplaceTokens(
braceTokenNeighbors.Keys,
(orig, t) => braceTokenNeighbors[orig] == SyntaxKind.OpenBraceToken
? ProcessTokenAfterOpenBrace(t)
: ProcessTokenBeforeCloseBrace(t)
);
return processed;
}
示例7: CalculateLinesOfCode
public int CalculateLinesOfCode(SyntaxNode node)
{
var totalLines = node.SyntaxTree.GetLineSpan(node.FullSpan).EndLinePosition.Line -
node.SyntaxTree.GetLineSpan(node.FullSpan).StartLinePosition.Line;
var amountOfSingleLineCommentsAndEmptyLines = CountSingleLineCommentsAndEmptyLines(node.DescendantTokens());
var amountOfMultiLineCommentLines = CountMultiLineCommentLines(node.DescendantTokens());
var amountOfSingleLineBraces = CountSingleLineBraces(node);
var amountOfDocumentationComments = CountDocumentationComments(node.DescendantTokens());
var linesOfCode = totalLines
- amountOfSingleLineCommentsAndEmptyLines
- amountOfMultiLineCommentLines
- amountOfSingleLineBraces
- amountOfDocumentationComments;
return linesOfCode;
}
示例8: TryGetActiveTokens
/// <returns>
/// If <paramref name="node"/> is a method, accessor, operator, destructor, or constructor without an initializer,
/// tokens of its block body, or tokens of the expression body if applicable.
///
/// If <paramref name="node"/> is an indexer declaration the tokens of its expression body.
///
/// If <paramref name="node"/> is a property declaration the tokens of its expression body or initializer.
///
/// If <paramref name="node"/> is a constructor with an initializer,
/// tokens of the initializer concatenated with tokens of the constructor body.
///
/// If <paramref name="node"/> is a variable declarator of a field with an initializer,
/// tokens of the field initializer.
///
/// Null reference otherwise.
/// </returns>
internal override IEnumerable<SyntaxToken> TryGetActiveTokens(SyntaxNode node)
{
if (node.IsKind(SyntaxKind.VariableDeclarator))
{
// TODO: The logic is similar to BreakpointSpans.TryCreateSpanForVariableDeclaration. Can we abstract it?
var declarator = node;
var fieldDeclaration = (BaseFieldDeclarationSyntax)declarator.Parent.Parent;
var variableDeclaration = fieldDeclaration.Declaration;
if (fieldDeclaration.Modifiers.Any(SyntaxKind.ConstKeyword))
{
return null;
}
if (variableDeclaration.Variables.Count == 1)
{
if (variableDeclaration.Variables[0].Initializer == null)
{
return null;
}
return fieldDeclaration.Modifiers.Concat(variableDeclaration.DescendantTokens()).Concat(fieldDeclaration.SemicolonToken);
}
if (declarator == variableDeclaration.Variables[0])
{
return fieldDeclaration.Modifiers.Concat(variableDeclaration.Type.DescendantTokens()).Concat(node.DescendantTokens());
}
return declarator.DescendantTokens();
}
if (node.IsKind(SyntaxKind.ConstructorDeclaration))
{
var ctor = (ConstructorDeclarationSyntax)node;
if (ctor.Initializer != null)
{
return ctor.Initializer.DescendantTokens().Concat(ctor.Body.DescendantTokens());
}
return ctor.Body.DescendantTokens();
}
return SyntaxUtilities.TryGetMethodDeclarationBody(node)?.DescendantTokens();
}
示例9: ClassifyNode
private void ClassifyNode(SyntaxNode node)
{
foreach (var token in node.DescendantTokens(span: _textSpan, descendIntoTrivia: false))
{
_cancellationToken.ThrowIfCancellationRequested();
ClassifyToken(token);
}
}
示例10: LocateExcludedSpans
private static bool LocateExcludedSpans(SyntaxNode root, out ImmutableArray<TextSpan> excludedSpans)
{
ImmutableArray<TextSpan>.Builder builder = ImmutableArray.CreateBuilder<TextSpan>();
// Locate disabled text
foreach (var trivia in root.DescendantTrivia(descendIntoTrivia: true))
{
if (trivia.IsKind(SyntaxKind.DisabledTextTrivia))
{
builder.Add(trivia.Span);
}
else if (trivia.IsKind(SyntaxKind.SingleLineCommentTrivia))
{
if (trivia.ToString().StartsWith("////"))
{
// Exclude comments starting with //// because they could contain commented code which contains
// string or character literals, and we don't want to change the contents of those strings.
builder.Add(trivia.Span);
}
}
}
// Locate string literals
foreach (var token in root.DescendantTokens(descendIntoTrivia: true))
{
switch (token.Kind())
{
case SyntaxKind.XmlTextLiteralToken:
if (token.Parent.IsKind(SyntaxKind.XmlCDataSection))
{
builder.Add(token.Span);
}
break;
case SyntaxKind.CharacterLiteralToken:
case SyntaxKind.StringLiteralToken:
case SyntaxKind.InterpolatedStringTextToken:
builder.Add(token.Span);
break;
default:
break;
}
}
// Sort the results
builder.Sort();
// Combine adjacent and overlapping spans
ReduceTextSpans(builder);
excludedSpans = builder.ToImmutable();
return true;
}
示例11: Process
public void Process(SyntaxNode node)
{
var indentLevel = 0;
SyntaxToken previous = default(SyntaxToken);
foreach (var token in node.DescendantTokens())
{
switch (token.Kind())
{
case SyntaxKind.AbstractKeyword:
case SyntaxKind.AddKeyword:
case SyntaxKind.AliasKeyword:
case SyntaxKind.ArgListKeyword:
case SyntaxKind.AscendingKeyword:
case SyntaxKind.AsKeyword:
case SyntaxKind.AssemblyKeyword:
case SyntaxKind.AsyncKeyword:
case SyntaxKind.AwaitKeyword:
case SyntaxKind.BaseKeyword:
case SyntaxKind.BoolKeyword:
case SyntaxKind.BreakKeyword:
case SyntaxKind.ByKeyword:
case SyntaxKind.TildeToken:
this.ProcessUnaryOperator(token);
break;
case SyntaxKind.ExclamationToken:
this.ProcessUnaryOperator(token);
break;
case SyntaxKind.DollarToken:
throw new NotSupportedException(token.Kind().ToString());
case SyntaxKind.PercentToken:
this.ProcessBinaryOperator(token);
break;
case SyntaxKind.CaretToken:
this.ProcessBinaryOperator(token);
break;
case SyntaxKind.AmpersandToken:
switch (token.Parent.Kind())
{
case SyntaxKind.BitwiseAndExpression:
this.ProcessBinaryOperator(token);
break;
case SyntaxKind.AddressOfExpression:
this.ProcessUnaryOperator(token);
break;
default:
throw new InvalidOperationException("Unexpected parent kind " + token.Parent.Kind() + " for token " + token.Kind());
}
break;
case SyntaxKind.AsteriskToken:
switch (token.Parent.Kind())
{
case SyntaxKind.MultiplyExpression:
this.ProcessBinaryOperator(token);
break;
default:
throw new NotSupportedException("Parent kind of asterisk was " + token.Parent.Kind());
}
break;
case SyntaxKind.OpenParenToken:
this.EnterGroup(token);
break;
case SyntaxKind.CloseParenToken:
this.ExitGroup(token);
break;
case SyntaxKind.MinusToken:
switch (token.Parent.Kind())
{
case SyntaxKind.SubtractExpression:
this.ProcessBinaryOperator(token);
break;
case SyntaxKind.UnaryMinusExpression:
this.ProcessUnaryOperator(token);
break;
default:
throw new InvalidOperationException("Unexpected parent kind " + token.Parent.Kind() + " for " + token.Kind());
}
break;
case SyntaxKind.PlusToken:
switch (token.Parent.Kind())
{
case SyntaxKind.AddExpression:
this.ProcessBinaryOperator(token);
break;
case SyntaxKind.UnaryPlusExpression:
this.ProcessUnaryOperator(token);
break;
default:
throw new InvalidOperationException("Unexpected parent kind " + token.Parent.Kind() + " for " + token.Kind());
}
break;
case SyntaxKind.EqualsToken:
this.ProcessBinaryOperator(token);
break;
case SyntaxKind.OpenBraceToken:
this.EnterGroup(token);
break;
case SyntaxKind.CloseBraceToken:
this.ExitGroup(token);
break;
case SyntaxKind.OpenBracketToken:
//.........这里部分代码省略.........
示例12: GetDescendantCloseBraceTokens
private static IEnumerable<SyntaxToken> GetDescendantCloseBraceTokens(SyntaxNode node)
{
return node.DescendantTokens().Where(token => token.IsKind(SyntaxKind.CloseBraceToken));
}
示例13: CreateIdentifierLocations
private static Dictionary<string, List<int>> CreateIdentifierLocations(Document document, SyntaxNode root, CancellationToken cancellationToken)
{
var syntaxFacts = document.GetLanguageService<ISyntaxFactsService>();
var identifierMap = SharedPools.StringIgnoreCaseDictionary<List<int>>().AllocateAndClear();
foreach (var token in root.DescendantTokens(descendIntoTrivia: true))
{
if (token.IsMissing || token.Span.Length == 0)
{
continue;
}
if (syntaxFacts.IsIdentifier(token) || syntaxFacts.IsGlobalNamespaceKeyword(token))
{
var valueText = token.ValueText;
identifierMap.GetOrAdd(valueText, _ => SharedPools.BigDefault<List<int>>().AllocateAndClear()).Add(token.Span.Start);
}
}
return identifierMap;
}
示例14: FixCloseBraces
/// <summary>
/// Close braces should never have a newline between the last line of content and the
/// closing brace. Also want to remove consecutive new lines before any comments or
/// #pragma that preceeds the close brace.
/// </summary>
private static SyntaxNode FixCloseBraces(SyntaxNode syntaxNode)
{
var tokensToReplace = syntaxNode.DescendantTokens().Where((token) =>
{
return
token.Kind() == SyntaxKind.CloseBraceToken &&
token.HasLeadingTrivia &&
(IsSimpleNewLine(token.LeadingTrivia, 0) || IsSimpleNewLine(token.LeadingTrivia, token.LeadingTrivia.Count - 1));
});
return syntaxNode.ReplaceTokens(tokensToReplace, (_, y) => FixCloseBraceLeadingTrivia(y));
}
示例15: CountSingleLineBraces
private int CountSingleLineBraces(SyntaxNode root)
{
var braces = root.DescendantTokens().Where(t => t.RawKind == OpenBraceToken
|| t.RawKind == CloseBraceToken);
return braces.Select(brace => root.SyntaxTree.GetLineSpan(brace.Span).StartLinePosition.Line)
.Select(lineNumber => root.SyntaxTree.GetText().Lines[lineNumber])
.Count(line => line.ToString().Trim().Length == 1);
}