本文整理汇总了C#中SyntaxToken.GetNextToken方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxToken.GetNextToken方法的具体用法?C# SyntaxToken.GetNextToken怎么用?C# SyntaxToken.GetNextToken使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SyntaxToken
的用法示例。
在下文中一共展示了SyntaxToken.GetNextToken方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetCollapsibleStart
private static int GetCollapsibleStart(SyntaxToken firstToken)
{
// If the *next* token has any leading comments, we use the end of the last one.
// If not, we check *this* token to see if it has any trailing comments and use the last one;
// otherwise, we use the end of this token.
var start = firstToken.Span.End;
var nextToken = firstToken.GetNextToken();
if (nextToken.Kind() != SyntaxKind.None && nextToken.HasLeadingTrivia)
{
var lastLeadingCommentTrivia = nextToken.LeadingTrivia.GetLastComment();
if (lastLeadingCommentTrivia != null)
{
start = lastLeadingCommentTrivia.Value.Span.End;
}
}
if (firstToken.HasTrailingTrivia)
{
var lastTrailingCommentTrivia = firstToken.TrailingTrivia.GetLastComment();
if (lastTrailingCommentTrivia != null)
{
start = lastTrailingCommentTrivia.Value.Span.End;
}
}
return start;
}
示例2: VisitToken
public override SyntaxToken VisitToken(SyntaxToken token)
{
token = base.VisitToken(token);
if (token.IsMissing)
{
return token;
}
if (token.Kind != SyntaxKind.CloseBraceToken)
{
return token;
}
var nextToken = token.GetNextToken(includeSkipped: true);
var tokenLine = syntaxTree.GetText().GetLineNumberFromPosition(token.Span.Start);
var nextTokenLine = syntaxTree.GetText().GetLineNumberFromPosition(nextToken.Span.Start);
var nextTokenIsCloseBrace = nextToken.Kind == SyntaxKind.CloseBraceToken;
var expectedDiff = nextTokenIsCloseBrace ? 1 : 2;
if (nextTokenLine == tokenLine + expectedDiff)
{
return token;
}
var nonNewLineTrivia = token.TrailingTrivia.Where(t => t.Kind != SyntaxKind.EndOfLineTrivia);
var newTrivia = nonNewLineTrivia.Concat(Enumerable.Repeat(Syntax.EndOfLine("\r\n"), expectedDiff));
return token.WithTrailingTrivia(Syntax.TriviaList(newTrivia));
}
示例3: FormatToken
public IList<TextChange> FormatToken(Workspace workspace, SyntaxToken token, CancellationToken cancellationToken)
{
Contract.ThrowIfTrue(token.Kind() == SyntaxKind.None || token.Kind() == SyntaxKind.EndOfFileToken);
// get previous token
var previousToken = token.GetPreviousToken(includeZeroWidth: true);
if (previousToken.Kind() == SyntaxKind.None)
{
// no previous token. nothing to format
return SpecializedCollections.EmptyList<TextChange>();
}
// This is a heuristic to prevent brace completion from breaking user expectation/muscle memory in common scenarios (see Devdiv:823958).
// Formatter uses FindToken on the position, which returns token to left, if there is nothing to the right and returns token to the right
// if there exists one. If the shape is "{|}", we're including '}' in the formatting range. Avoid doing that to improve verbatim typing
// in the following special scenarios.
int adjustedEndPosition = token.Span.End;
if (token.IsKind(SyntaxKind.OpenBraceToken) &&
(token.Parent.IsInitializerForArrayOrCollectionCreationExpression() ||
token.Parent is AnonymousObjectCreationExpressionSyntax))
{
var nextToken = token.GetNextToken(includeZeroWidth: true);
if (nextToken.IsKind(SyntaxKind.CloseBraceToken))
{
// Format upto '{' and exclude '}'
adjustedEndPosition = token.SpanStart;
}
}
var smartTokenformattingRules = (new SmartTokenFormattingRule()).Concat(_formattingRules);
return Formatter.GetFormattedTextChanges(_root, new TextSpan[] { TextSpan.FromBounds(previousToken.SpanStart, adjustedEndPosition) }, workspace, _optionSet, smartTokenformattingRules, cancellationToken);
}
示例4: FromIndentBlockOperations
public int? FromIndentBlockOperations(
SyntaxTree tree, SyntaxToken token, int position, CancellationToken cancellationToken)
{
// we use operation service to see whether it is a starting point of new indentation.
// ex)
// if (true)
// {
// | <= this is new starting point of new indentation
var operation = GetIndentationDataFor(tree.GetRoot(cancellationToken), token, position);
// try find indentation based on indentation operation
if (operation != null)
{
// make sure we found new starting point of new indentation.
// such operation should start span after the token (a token that is right before the new indentation),
// contains current position, and position should be before the existing next token
if (token.Span.End <= operation.TextSpan.Start &&
operation.TextSpan.IntersectsWith(position) &&
position <= token.GetNextToken(includeZeroWidth: true).SpanStart)
{
return GetIndentationOfCurrentPosition(tree, token, position, cancellationToken);
}
}
return null;
}
示例5: ProcessTokenAfterOpenBrace
private static SyntaxToken ProcessTokenAfterOpenBrace(SyntaxToken token)
{
if (!token.HasLeadingTrivia) { return token; }
var nextToken = token.GetNextToken();
return token;
}
示例6: Fix
async Task<Document> Fix(Document document, SyntaxToken syntax, CancellationToken cancellationToken)
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var nextToken = syntax.GetNextToken();
return document.WithSyntaxRoot(root
.ReplaceTokens(new[] { syntax, nextToken }, (t, _) =>
{
if (t == syntax)
return SyntaxFactory.Token(SyntaxKind.None);
if (t == nextToken)
return nextToken.WithLeadingTrivia(syntax.LeadingTrivia.AddRange(nextToken.LeadingTrivia));
return default(SyntaxToken);
}));
}
示例7: GetTransformedDocumentAsync
private static Task<Document> GetTransformedDocumentAsync(Document document, SyntaxNode root, SyntaxToken token)
{
Dictionary<SyntaxToken, SyntaxToken> replacements = new Dictionary<SyntaxToken, SyntaxToken>();
// check for a following space
bool missingFollowingSpace = true;
if (token.HasTrailingTrivia)
{
if (token.TrailingTrivia.First().IsKind(SyntaxKind.WhitespaceTrivia))
{
missingFollowingSpace = false;
}
else if (token.TrailingTrivia.First().IsKind(SyntaxKind.EndOfLineTrivia))
{
missingFollowingSpace = false;
}
}
else
{
SyntaxToken nextToken = token.GetNextToken();
if (nextToken.IsKind(SyntaxKind.CommaToken) || nextToken.IsKind(SyntaxKind.GreaterThanToken) || nextToken.IsKind(SyntaxKind.CloseBracketToken))
{
// make an exception for things like typeof(Func<,>), typeof(Func<,,>), and int[,]
missingFollowingSpace = false;
}
}
if (!token.IsFirstInLine())
{
SyntaxToken precedingToken = token.GetPreviousToken();
if (precedingToken.TrailingTrivia.Any(SyntaxKind.WhitespaceTrivia))
{
SyntaxToken corrected = precedingToken.WithoutTrailingWhitespace().WithoutFormatting();
replacements[precedingToken] = corrected;
}
}
if (missingFollowingSpace)
{
SyntaxToken intermediate = token.WithoutTrailingWhitespace();
SyntaxToken corrected =
intermediate
.WithTrailingTrivia(intermediate.TrailingTrivia.Insert(0, SyntaxFactory.Space));
replacements[token] = corrected;
}
var transformed = root.ReplaceTokens(replacements.Keys, (original, maybeRewritten) => replacements[original]);
return Task.FromResult(document.WithSyntaxRoot(transformed));
}
示例8: HasFollowingEndTagTrivia
private bool HasFollowingEndTagTrivia(XmlElementSyntax parentElement, SyntaxToken lessThanSlashToken)
{
var expectedEndTagText = "</" + parentElement.StartTag.Name.LocalName.ValueText + ">";
var token = lessThanSlashToken.GetNextToken(includeDocumentationComments: true);
while (token.Parent.IsKind(SyntaxKind.XmlText))
{
if (token.ValueText == expectedEndTagText)
{
return true;
}
token = token.GetNextToken(includeDocumentationComments: true);
}
return false;
}
示例9: FromAlignTokensOperations
public int? FromAlignTokensOperations(SyntaxTree tree, SyntaxToken token)
{
// let's check whether there is any missing token under us and whether
// there is an align token operation for that missing token.
var nextToken = token.GetNextToken(includeZeroWidth: true);
if (nextToken.RawKind != 0 &&
nextToken.Width() <= 0)
{
// looks like we have one. find whether there is a align token operation for this token
var alignmentBaseToken = GetAlignmentBaseTokenFor(nextToken);
if (alignmentBaseToken.RawKind != 0)
{
return tree.GetTokenColumn(alignmentBaseToken, _tabSize);
}
}
return null;
}
示例10: GetSpan
private static TextSpan GetSpan(SyntaxToken firstToken, SyntaxToken lastToken)
{
var previousToken = firstToken.GetPreviousToken();
var nextToken = lastToken.GetNextToken();
if (previousToken.RawKind != 0)
{
firstToken = previousToken;
}
if (nextToken.RawKind != 0)
{
lastToken = nextToken;
}
return TextSpan.FromBounds(firstToken.SpanStart, lastToken.Span.End);
}
示例11: ReformatElement
private SyntaxNode ReformatElement(SyntaxNode syntaxRoot, SyntaxNode element, SyntaxToken openBraceToken, SyntaxToken closeBraceToken, IndentationOptions indentationOptions)
{
var tokenSubstitutions = new Dictionary<SyntaxToken, SyntaxToken>();
var parentLastToken = openBraceToken.GetPreviousToken();
var parentEndLine = parentLastToken.GetLineSpan().EndLinePosition.Line;
var blockStartLine = openBraceToken.GetLineSpan().StartLinePosition.Line;
// reformat parent if it is on the same line as the block.
if (parentEndLine == blockStartLine)
{
var newTrailingTrivia = parentLastToken.TrailingTrivia
.WithoutTrailingWhitespace()
.Add(SyntaxFactory.CarriageReturnLineFeed);
tokenSubstitutions.Add(parentLastToken, parentLastToken.WithTrailingTrivia(newTrailingTrivia));
}
var parentIndentationLevel = IndentationHelper.GetIndentationSteps(indentationOptions, element);
var indentationString = IndentationHelper.GenerateIndentationString(indentationOptions, parentIndentationLevel);
var contentIndentationString = IndentationHelper.GenerateIndentationString(indentationOptions, parentIndentationLevel + 1);
// reformat opening brace
tokenSubstitutions.Add(openBraceToken, this.FormatBraceToken(openBraceToken, indentationString));
// reformat start of content
var startOfContentToken = openBraceToken.GetNextToken();
if (startOfContentToken != closeBraceToken)
{
var newStartOfContentTokenLeadingTrivia = startOfContentToken.LeadingTrivia
.WithoutTrailingWhitespace()
.Add(SyntaxFactory.Whitespace(contentIndentationString));
tokenSubstitutions.Add(startOfContentToken, startOfContentToken.WithLeadingTrivia(newStartOfContentTokenLeadingTrivia));
}
// reformat end of content
var endOfContentToken = closeBraceToken.GetPreviousToken();
if (endOfContentToken != openBraceToken)
{
var newEndOfContentTokenTrailingTrivia = endOfContentToken.TrailingTrivia
.WithoutTrailingWhitespace()
.Add(SyntaxFactory.CarriageReturnLineFeed);
// check if the token already exists (occurs when there is only one token in the block)
if (tokenSubstitutions.ContainsKey(endOfContentToken))
{
tokenSubstitutions[endOfContentToken] = tokenSubstitutions[endOfContentToken].WithTrailingTrivia(newEndOfContentTokenTrailingTrivia);
}
else
{
tokenSubstitutions.Add(endOfContentToken, endOfContentToken.WithTrailingTrivia(newEndOfContentTokenTrailingTrivia));
}
}
// reformat closing brace
tokenSubstitutions.Add(closeBraceToken, this.FormatBraceToken(closeBraceToken, indentationString));
var rewriter = new TokenRewriter(tokenSubstitutions);
var newSyntaxRoot = rewriter.Visit(syntaxRoot);
return newSyntaxRoot;
}
示例12: HandleCommaToken
private static void HandleCommaToken(SyntaxTreeAnalysisContext context, SyntaxToken token)
{
if (token.IsMissing)
{
return;
}
// check for a following space
bool missingFollowingSpace = true;
if (token.HasTrailingTrivia)
{
if (token.TrailingTrivia.First().IsKind(SyntaxKind.WhitespaceTrivia))
{
missingFollowingSpace = false;
}
else if (token.TrailingTrivia.First().IsKind(SyntaxKind.EndOfLineTrivia))
{
missingFollowingSpace = false;
}
}
else
{
SyntaxToken nextToken = token.GetNextToken();
if (nextToken.IsKind(SyntaxKind.CommaToken) || nextToken.IsKind(SyntaxKind.GreaterThanToken) || nextToken.IsKind(SyntaxKind.CloseBracketToken))
{
// make an exception for things like typeof(Func<,>), typeof(Func<,,>), and int[,]
missingFollowingSpace = false;
}
}
bool hasPrecedingSpace = false;
if (!token.IsFirstInLine())
{
hasPrecedingSpace = token.IsPrecededByWhitespace();
}
if (hasPrecedingSpace)
{
// comma must{ not} be {preceded} by a space
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), TokenSpacingCodeFixProvider.RemovePreceding, " not", "preceded"));
}
if (missingFollowingSpace)
{
// comma must{} be {followed} by a space
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), TokenSpacingCodeFixProvider.InsertFollowing, string.Empty, "followed"));
}
}
示例13: CheckToken
private static void CheckToken(SyntaxNodeAnalysisContext context, SyntaxToken token, bool withLeadingWhitespace, bool allowAtEndOfLine, bool withTrailingWhitespace, string tokenText = null)
{
var precedingToken = token.GetPreviousToken();
var precedingTriviaList = TriviaHelper.MergeTriviaLists(precedingToken.TrailingTrivia, token.LeadingTrivia);
var followingToken = token.GetNextToken();
var followingTriviaList = TriviaHelper.MergeTriviaLists(token.TrailingTrivia, followingToken.LeadingTrivia);
if (withLeadingWhitespace)
{
// Don't report missing leading whitespace when the token is the first token on a text line.
if (!token.IsFirstInLine()
&& ((precedingTriviaList.Count == 0) || !precedingTriviaList.Last().IsKind(SyntaxKind.WhitespaceTrivia)))
{
var properties = ImmutableDictionary.Create<string, string>()
.Add(CodeFixAction, InsertBeforeTag);
context.ReportDiagnostic(Diagnostic.Create(DescriptorPrecededByWhitespace, token.GetLocation(), properties, tokenText ?? token.Text));
}
}
else
{
// don't report leading whitespace when the token is the first token on a text line
if (!token.IsOnlyPrecededByWhitespaceInLine()
&& ((precedingTriviaList.Count > 0) && precedingTriviaList.Last().IsKind(SyntaxKind.WhitespaceTrivia)))
{
var properties = ImmutableDictionary.Create<string, string>()
.Add(CodeFixAction, RemoveBeforeTag);
context.ReportDiagnostic(Diagnostic.Create(DescriptorNotPrecededByWhitespace, token.GetLocation(), properties, tokenText ?? token.Text));
}
}
if (!allowAtEndOfLine && token.TrailingTrivia.Any(SyntaxKind.EndOfLineTrivia))
{
var properties = ImmutableDictionary.Create<string, string>();
// Do not register a code fix action if there are non whitespace or end of line tokens present.
if (followingTriviaList.All(t => t.IsKind(SyntaxKind.WhitespaceTrivia) || t.IsKind(SyntaxKind.EndOfLineTrivia)))
{
properties = properties.Add(CodeFixAction, withTrailingWhitespace ? RemoveEndOfLineWithTrailingSpaceTag : RemoveEndOfLineTag);
}
context.ReportDiagnostic(Diagnostic.Create(DescriptorNotAtEndOfLine, token.GetLocation(), properties, tokenText ?? token.Text));
return;
}
if (withTrailingWhitespace)
{
if ((followingTriviaList.Count == 0) || !(followingTriviaList.First().IsKind(SyntaxKind.WhitespaceTrivia) || followingTriviaList.First().IsKind(SyntaxKind.EndOfLineTrivia)))
{
var properties = ImmutableDictionary.Create<string, string>()
.Add(CodeFixAction, InsertAfterTag);
context.ReportDiagnostic(Diagnostic.Create(DescriptorFollowedByWhitespace, token.GetLocation(), properties, tokenText ?? token.Text));
}
}
else
{
if ((followingTriviaList.Count > 0) && followingTriviaList.First().IsKind(SyntaxKind.WhitespaceTrivia))
{
var properties = ImmutableDictionary.Create<string, string>()
.Add(CodeFixAction, RemoveAfterTag);
context.ReportDiagnostic(Diagnostic.Create(DescriptorNotFollowedByWhitespace, token.GetLocation(), properties, tokenText ?? token.Text));
}
}
}
示例14: HandleThrowKeywordToken
private static void HandleThrowKeywordToken(SyntaxTreeAnalysisContext context, SyntaxToken token)
{
if (token.IsMissing)
{
return;
}
/* if the next token is ;, then treat as disallowed:
* throw;
*/
SyntaxToken nextToken = token.GetNextToken();
if (nextToken.IsKind(SyntaxKind.SemicolonToken))
{
HandleDisallowedSpaceToken(context, token);
return;
}
// otherwise treat as required
HandleRequiredSpaceToken(context, token);
}
示例15: HandleNewKeywordToken
private static void HandleNewKeywordToken(SyntaxTreeAnalysisContext context, SyntaxToken token)
{
if (token.IsMissing)
{
return;
}
// if the next token is [ or (, then treat as disallowed
SyntaxToken nextToken = token.GetNextToken();
if (nextToken.IsKind(SyntaxKind.OpenBracketToken) || nextToken.IsKind(SyntaxKind.OpenParenToken))
{
if (token.Parent.IsKind(SyntaxKind.ImplicitArrayCreationExpression))
{
// This is handled by SA1026
return;
}
HandleDisallowedSpaceToken(context, token);
return;
}
// otherwise treat as required
HandleRequiredSpaceToken(context, token);
}