本文整理汇总了C#中SyntaxNode.GetFirstToken方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxNode.GetFirstToken方法的具体用法?C# SyntaxNode.GetFirstToken怎么用?C# SyntaxNode.GetFirstToken使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SyntaxNode
的用法示例。
在下文中一共展示了SyntaxNode.GetFirstToken方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TreeData
public TreeData(SyntaxNode root)
{
Contract.ThrowIfNull(root);
_root = root;
_firstToken = _root.GetFirstToken(includeZeroWidth: true);
_lastToken = _root.GetLastToken(includeZeroWidth: true);
}
示例2: AddTypeParameterConstraintClauseOperation
private void AddTypeParameterConstraintClauseOperation(List<IndentBlockOperation> list, SyntaxNode node)
{
var typeParameterConstraintClause = node as TypeParameterConstraintClauseSyntax;
if (typeParameterConstraintClause != null)
{
var declaringNode = typeParameterConstraintClause.Parent;
var baseToken = declaringNode.GetFirstToken();
AddIndentBlockOperation(list, baseToken, node.GetFirstToken(), node.GetLastToken());
}
}
示例3: AddChangeSignatureIndentOperation
private void AddChangeSignatureIndentOperation(List<IndentBlockOperation> list, SyntaxNode node)
{
if (node.Parent != null)
{
var baseToken = node.Parent.GetFirstToken();
var startToken = node.GetFirstToken();
var endToken = node.GetLastToken();
var span = CommonFormattingHelpers.GetSpanIncludingTrailingAndLeadingTriviaOfAdjacentTokens(startToken, endToken);
span = TextSpan.FromBounds(Math.Max(baseToken.Span.End, span.Start), span.End);
list.Add(FormattingOperations.CreateRelativeIndentBlockOperation(baseToken, startToken, endToken, span, indentationDelta: 1, option: IndentBlockOption.RelativeToFirstTokenOnBaseTokenLine));
}
}
示例4: Node
public Node(SyntaxNode root) :
base(root)
{
Contract.ThrowIfFalse(root.GetFirstToken(includeZeroWidth: true).RawKind != 0);
}
示例5: AddSpecificNodesSuppressOperations
private void AddSpecificNodesSuppressOperations(List<SuppressOperation> list, SyntaxNode node)
{
var ifStatementNode = node as IfStatementSyntax;
if (ifStatementNode != null)
{
AddSuppressWrappingIfOnSingleLineOperation(list, ifStatementNode.IfKeyword, ifStatementNode.Statement.GetLastToken(includeZeroWidth: true));
if (ifStatementNode.Else != null)
{
AddSuppressWrappingIfOnSingleLineOperation(list, ifStatementNode.Else.ElseKeyword, ifStatementNode.Else.Statement.GetLastToken(includeZeroWidth: true));
}
return;
}
var constructorInitializerNode = node as ConstructorInitializerSyntax;
if (constructorInitializerNode != null)
{
var constructorDeclarationNode = constructorInitializerNode.Parent as ConstructorDeclarationSyntax;
if (constructorDeclarationNode?.Body != null)
{
AddSuppressWrappingIfOnSingleLineOperation(list, constructorInitializerNode.ColonToken, constructorDeclarationNode.Body.CloseBraceToken);
}
return;
}
var whileStatementNode = node as DoStatementSyntax;
if (whileStatementNode != null)
{
AddSuppressWrappingIfOnSingleLineOperation(list, whileStatementNode.GetFirstToken(includeZeroWidth: true), whileStatementNode.Statement.GetLastToken(includeZeroWidth: true));
return;
}
var memberDeclNode = node as MemberDeclarationSyntax;
if (memberDeclNode != null)
{
// Attempt to keep the part of a member that follows hte attributes on a single
// line if that's how it's currently written.
var tokens = memberDeclNode.GetFirstAndLastMemberDeclarationTokensAfterAttributes();
AddSuppressWrappingIfOnSingleLineOperation(list, tokens.Item1, tokens.Item2);
var attributes = memberDeclNode.GetAttributes();
if (attributes.Count > 0)
{
// Also, If the member is on single line with its attributes on it, then keep
// it on a single line. This is for code like the following:
//
// [Import] public int Field1;
// [Import] public int Field2;
AddSuppressWrappingIfOnSingleLineOperation(list,
node.GetFirstToken(includeZeroWidth: true),
node.GetLastToken(includeZeroWidth: true));
}
var propertyDeclNode = node as PropertyDeclarationSyntax;
if (propertyDeclNode?.Initializer != null && propertyDeclNode?.AccessorList != null)
{
AddSuppressWrappingIfOnSingleLineOperation(list, tokens.Item1, propertyDeclNode.AccessorList.GetLastToken());
}
return;
}
var accessorDeclNode = node as AccessorDeclarationSyntax;
if (accessorDeclNode != null)
{
AddSuppressWrappingIfOnSingleLineOperation(list, accessorDeclNode.Keyword, accessorDeclNode.GetLastToken(includeZeroWidth: true));
return;
}
var switchSection = node as SwitchSectionSyntax;
if (switchSection != null)
{
if (switchSection.Labels.Count < 2)
{
AddSuppressWrappingIfOnSingleLineOperation(list, switchSection.GetFirstToken(includeZeroWidth: true), switchSection.GetLastToken(includeZeroWidth: true));
return;
}
else
{
// Add Separate suppression for each Label and for the last label, add the <>
for (int i = 0; i < switchSection.Labels.Count - 1; ++i)
{
if (switchSection.Labels[i] != null)
{
AddSuppressWrappingIfOnSingleLineOperation(list, switchSection.Labels[i].GetFirstToken(includeZeroWidth: true), switchSection.Labels[i].GetLastToken(includeZeroWidth: true));
}
}
// For the last label add the rest of the statements of the switch
if (switchSection.Labels[switchSection.Labels.Count - 1] != null)
{
AddSuppressWrappingIfOnSingleLineOperation(list, switchSection.Labels[switchSection.Labels.Count - 1].GetFirstToken(includeZeroWidth: true), switchSection.GetLastToken(includeZeroWidth: true));
}
return;
}
}
//.........这里部分代码省略.........
示例6: AddInitializerSuppressOperations
private void AddInitializerSuppressOperations(List<SuppressOperation> list, SyntaxNode parent, IEnumerable<SyntaxNode> items)
{
// make creation node itself to not break into multiple line, if it is on same line
AddSuppressWrappingIfOnSingleLineOperation(list, parent.GetFirstToken(includeZeroWidth: true), parent.GetLastToken(includeZeroWidth: true));
// make each initializer expression to not break into multiple line if it is on same line
foreach (var item in items)
{
var firstToken = item.GetFirstToken(includeZeroWidth: true);
var lastToken = item.GetLastToken(includeZeroWidth: true);
if (!firstToken.Equals(lastToken))
{
AddSuppressWrappingIfOnSingleLineOperation(list, firstToken, lastToken);
}
}
}
示例7: GetCallerLocation
private static SourceLocation GetCallerLocation(SyntaxNode syntax, ThreeState enableCallerInfo)
{
switch (enableCallerInfo)
{
case ThreeState.False:
return null;
case ThreeState.True:
return new SourceLocation(syntax.GetFirstToken());
}
Debug.Assert(enableCallerInfo == ThreeState.Unknown);
switch (syntax.Kind())
{
case SyntaxKind.InvocationExpression:
return new SourceLocation(((InvocationExpressionSyntax)syntax).ArgumentList.OpenParenToken);
case SyntaxKind.ObjectCreationExpression:
return new SourceLocation(((ObjectCreationExpressionSyntax)syntax).NewKeyword);
case SyntaxKind.BaseConstructorInitializer:
case SyntaxKind.ThisConstructorInitializer:
return new SourceLocation(((ConstructorInitializerSyntax)syntax).ArgumentList.OpenParenToken);
case SyntaxKind.ElementAccessExpression:
return new SourceLocation(((ElementAccessExpressionSyntax)syntax).ArgumentList.OpenBracketToken);
case SyntaxKind.FromClause:
case SyntaxKind.GroupClause:
case SyntaxKind.JoinClause:
case SyntaxKind.JoinIntoClause:
case SyntaxKind.LetClause:
case SyntaxKind.OrderByClause:
case SyntaxKind.SelectClause:
case SyntaxKind.WhereClause:
return new SourceLocation(syntax.GetFirstToken());
default:
return null;
}
}
示例8: AddBraceSuppressOperations
private void AddBraceSuppressOperations(List<SuppressOperation> list, SyntaxNode node)
{
var bracePair = node.GetBracePair();
if (!bracePair.IsValidBracePair())
{
return;
}
var firstTokenOfNode = node.GetFirstToken(includeZeroWidth: true);
if (node.IsLambdaBodyBlock())
{
// include lambda itself.
firstTokenOfNode = node.Parent.GetFirstToken(includeZeroWidth: true);
}
// suppress wrapping on whole construct that owns braces and also brace pair itself if it is on same line
AddSuppressWrappingIfOnSingleLineOperation(list, firstTokenOfNode, bracePair.Item2);
AddSuppressWrappingIfOnSingleLineOperation(list, bracePair.Item1, bracePair.Item2);
}
示例9: FindTokenOnLeftOfPosition
/// <summary>
/// Find closest token (including one in structured trivia) left of given position
/// </summary>
private SyntaxToken FindTokenOnLeftOfPosition(ISyntaxFactsService syntaxFactsService, SyntaxNode root, int position)
{
// find token on left
var token = syntaxFactsService.FindTokenOnLeftOfPosition(root, position, includeSkipped: true, includeDirectives: true, includeDocumentationComments: true);
if (token.RawKind == 0)
{
// if there is no token on left, return the first token
return root.GetFirstToken(includeZeroWidth: true, includeSkipped: true, includeDirectives: true, includeDocumentationComments: true);
}
return token;
}
示例10: GetNonOverlappingSpans
/// <summary>
/// make sure annotations are positioned outside of any spans. if not, merge two adjacent spans to one
/// </summary>
private IEnumerable<TextSpan> GetNonOverlappingSpans(ISyntaxFactsService syntaxFactsService, SyntaxNode root, IEnumerable<TextSpan> spans, CancellationToken cancellationToken)
{
// create interval tree for spans
var intervalTree = SimpleIntervalTree.Create(TextSpanIntervalIntrospector.Instance, spans);
// find tokens that are outside of spans
var tokenSpans = new List<TextSpan>();
foreach (var span in spans)
{
cancellationToken.ThrowIfCancellationRequested();
SyntaxToken startToken;
SyntaxToken endToken;
TextSpan spanAlignedToTokens = GetSpanAlignedToTokens(syntaxFactsService, root, span, out startToken, out endToken);
SyntaxToken previousToken = startToken.GetPreviousToken(includeZeroWidth: true, includeSkipped: true, includeDirectives: true, includeDocumentationComments: true);
SyntaxToken nextToken = endToken.GetNextToken(includeZeroWidth: true, includeSkipped: true, includeDirectives: true, includeDocumentationComments: true);
// make sure previous and next token we found are not overlap with any existing spans. if it does, merge two spans
previousToken = (previousToken.RawKind == 0) ? root.GetFirstToken(includeZeroWidth: true) : previousToken;
var start = intervalTree.GetOverlappingIntervals(previousToken.SpanStart, previousToken.Span.Length).Any() ?
previousToken.SpanStart : startToken.SpanStart;
nextToken = (nextToken.RawKind == 0) ? root.GetLastToken(includeZeroWidth: true) : nextToken;
var end = intervalTree.GetOverlappingIntervals(nextToken.SpanStart, nextToken.Span.Length).Any() ?
nextToken.Span.End : endToken.Span.End;
tokenSpans.Add(TextSpan.FromBounds(start, end));
}
return tokenSpans.ToNormalizedSpans();
}
示例11: AnnotateNodeForTextSpans
/// <summary>
/// inject annotations to the node so that it can re-calculate spans for each code cleaners after each tree transformation
/// </summary>
private ValueTuple<SyntaxNode, List<ValueTuple<SyntaxAnnotation, SyntaxAnnotation>>> AnnotateNodeForTextSpans(
ISyntaxFactsService syntaxFactsService, SyntaxNode root, IEnumerable<TextSpan> spans, CancellationToken cancellationToken)
{
// get spans where tokens around the spans are not overlapping with spans
var nonOverlappingSpans = GetNonOverlappingSpans(syntaxFactsService, root, spans, cancellationToken);
// build token annotation map
var tokenAnnotationMap = new Dictionary<SyntaxToken, List<SyntaxAnnotation>>();
var annotations = new List<ValueTuple<SyntaxAnnotation, SyntaxAnnotation>>();
foreach (var span in nonOverlappingSpans)
{
cancellationToken.ThrowIfCancellationRequested();
SyntaxToken previousToken;
SyntaxToken startToken;
SyntaxToken endToken;
SyntaxToken nextToken;
GetTokensAroundSpan(root, span, out previousToken, out startToken, out endToken, out nextToken);
// create marker to insert
SpanMarker startMarker = new SpanMarker(type: (previousToken.RawKind == 0) ? SpanMarkerType.BeginningOfFile : SpanMarkerType.Normal,
oppositeMarkerType: (nextToken.RawKind == 0) ? SpanMarkerType.EndOfFile : SpanMarkerType.Normal);
SpanMarker endMarker = new SpanMarker(type: (nextToken.RawKind == 0) ? SpanMarkerType.EndOfFile : SpanMarkerType.Normal,
oppositeMarkerType: (previousToken.RawKind == 0) ? SpanMarkerType.BeginningOfFile : SpanMarkerType.Normal);
// set proper tokens
previousToken = (previousToken.RawKind == 0) ? root.GetFirstToken(includeZeroWidth: true) : previousToken;
nextToken = (nextToken.RawKind == 0) ? root.GetLastToken(includeZeroWidth: true) : nextToken;
// build token to marker map
tokenAnnotationMap.GetOrAdd(previousToken, _ => new List<SyntaxAnnotation>()).Add(startMarker.Annotation);
tokenAnnotationMap.GetOrAdd(nextToken, _ => new List<SyntaxAnnotation>()).Add(endMarker.Annotation);
// remember markers
annotations.Add(new ValueTuple<SyntaxAnnotation, SyntaxAnnotation>(startMarker.Annotation, endMarker.Annotation));
}
// do a quick check
// if, after all merges, spans are merged into one span that covers whole tree, return right away
if (CleanupWholeNode(annotations))
{
// this will indicate that no annotation is needed
return default(ValueTuple<SyntaxNode, List<ValueTuple<SyntaxAnnotation, SyntaxAnnotation>>>);
}
// inject annotations
var newNode = InjectAnnotations(root, tokenAnnotationMap);
return ValueTuple.Create(newNode, annotations);
}
示例12: SetAlignmentBlockOperation
private void SetAlignmentBlockOperation(List<IndentBlockOperation> list, SyntaxNode baseNode, SyntaxNode body)
{
var option = IndentBlockOption.RelativeToFirstTokenOnBaseTokenLine;
var baseToken = baseNode.GetFirstToken(includeZeroWidth: true);
var firstToken = body.GetFirstToken(includeZeroWidth: true);
var lastToken = body.GetLastToken(includeZeroWidth: true);
SetAlignmentBlockOperation(list, baseToken, firstToken, lastToken, option);
}
示例13: AddIndentBlockOperations
private static void AddIndentBlockOperations(List<IndentBlockOperation> list, SyntaxNode node)
{
// only add indent block operation if the base token is the first token on line
var text = node.SyntaxTree.GetText();
var baseToken = node.Parent.GetFirstToken(includeZeroWidth: true);
list.Add(FormattingOperations.CreateRelativeIndentBlockOperation(
baseToken,
node.GetFirstToken(includeZeroWidth: true).GetNextToken(includeZeroWidth: true),
node.GetLastToken(includeZeroWidth: true).GetPreviousToken(includeZeroWidth: true),
indentationDelta: 1,
option: IndentBlockOption.RelativeToFirstTokenOnBaseTokenLine));
}
示例14: IsInAnonymousFunctionOrQuery
internal static bool IsInAnonymousFunctionOrQuery(int position, SyntaxNode lambdaExpressionOrQueryNode)
{
Debug.Assert(lambdaExpressionOrQueryNode.IsAnonymousFunction() || lambdaExpressionOrQueryNode.IsQuery());
SyntaxToken firstIncluded;
CSharpSyntaxNode body;
switch (lambdaExpressionOrQueryNode.Kind())
{
case SyntaxKind.SimpleLambdaExpression:
SimpleLambdaExpressionSyntax simple = (SimpleLambdaExpressionSyntax)lambdaExpressionOrQueryNode;
firstIncluded = simple.Parameter.Identifier;
body = simple.Body;
break;
case SyntaxKind.ParenthesizedLambdaExpression:
ParenthesizedLambdaExpressionSyntax parenthesized = (ParenthesizedLambdaExpressionSyntax)lambdaExpressionOrQueryNode;
firstIncluded = parenthesized.ParameterList.OpenParenToken;
body = parenthesized.Body;
break;
case SyntaxKind.AnonymousMethodExpression:
AnonymousMethodExpressionSyntax anon = (AnonymousMethodExpressionSyntax)lambdaExpressionOrQueryNode;
firstIncluded = anon.DelegateKeyword;
body = anon.Block;
break;
default:
// OK, so we have some kind of query clause. They all start with a keyword token, so we'll skip that.
firstIncluded = lambdaExpressionOrQueryNode.GetFirstToken().GetNextToken();
return IsBetweenTokens(position, firstIncluded, lambdaExpressionOrQueryNode.GetLastToken().GetNextToken());
}
var bodyStatement = body as StatementSyntax;
var firstExcluded = bodyStatement != null ?
GetFirstExcludedToken(bodyStatement) :
(SyntaxToken)SyntaxNavigator.Instance.GetNextToken(body, predicate: null, stepInto: null);
return IsBetweenTokens(position, firstIncluded, firstExcluded);
}
示例15: AddAnchorIndentationOperation
private void AddAnchorIndentationOperation(List<AnchorIndentationOperation> list, SyntaxNode node)
{
AddAnchorIndentationOperation(list, node.GetFirstToken(includeZeroWidth: true), node.GetLastToken(includeZeroWidth: true));
}