本文整理汇总了C#中SyntaxToken.GetPreviousToken方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxToken.GetPreviousToken方法的具体用法?C# SyntaxToken.GetPreviousToken怎么用?C# SyntaxToken.GetPreviousToken使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SyntaxToken
的用法示例。
在下文中一共展示了SyntaxToken.GetPreviousToken方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: HandleOpenBracketToken
private static void HandleOpenBracketToken(SyntaxTreeAnalysisContext context, SyntaxToken token)
{
bool firstInLine = token.IsFirstInLine();
bool precededBySpace = true;
bool ignorePrecedingSpaceProblem = false;
if (!firstInLine)
{
precededBySpace = token.IsPrecededByWhitespace();
// ignore if handled by SA1026
ignorePrecedingSpaceProblem = precededBySpace && token.GetPreviousToken().IsKind(SyntaxKind.NewKeyword);
}
bool followedBySpace = token.IsFollowedByWhitespace();
bool lastInLine = token.IsLastInLine();
if (!firstInLine && precededBySpace && !ignorePrecedingSpaceProblem && !lastInLine && followedBySpace)
{
// Opening square bracket must {neither preceded nor followed} by a space.
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), "neither preceded nor followed"));
}
else if (!firstInLine && precededBySpace && !ignorePrecedingSpaceProblem)
{
// Opening square bracket must {not be preceded} by a space.
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), "not be preceded"));
}
else if (!lastInLine && followedBySpace)
{
// Opening square bracket must {not be followed} by a space.
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), "not be followed"));
}
}
开发者ID:nvincent,项目名称:StyleCopAnalyzers,代码行数:33,代码来源:SA1010OpeningSquareBracketsMustBeSpacedCorrectly.cs
示例2: HandleOpenBracketToken
private static void HandleOpenBracketToken(SyntaxTreeAnalysisContext context, SyntaxToken token)
{
bool firstInLine = token.IsFirstInLine();
bool precededBySpace = true;
bool ignorePrecedingSpaceProblem = false;
if (!firstInLine)
{
precededBySpace = token.IsPrecededByWhitespace(context.CancellationToken);
// ignore if handled by SA1026
ignorePrecedingSpaceProblem = precededBySpace && token.GetPreviousToken().IsKind(SyntaxKind.NewKeyword);
}
bool followedBySpace = token.IsFollowedByWhitespace();
bool lastInLine = token.IsLastInLine();
if (!firstInLine && precededBySpace && !ignorePrecedingSpaceProblem && !IsPartOfIndexInitializer(token))
{
// Opening square bracket must {not be preceded} by a space.
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), TokenSpacingProperties.RemovePreceding, "not be preceded"));
}
if (!lastInLine && followedBySpace)
{
// Opening square bracket must {not be followed} by a space.
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), TokenSpacingProperties.RemoveFollowing, "not be followed"));
}
}
开发者ID:EdwinEngelen,项目名称:StyleCopAnalyzers,代码行数:29,代码来源:SA1010OpeningSquareBracketsMustBeSpacedCorrectly.cs
示例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: AnalyzeOpenBrace
private static void AnalyzeOpenBrace(SyntaxTreeAnalysisContext context, SyntaxToken openBrace)
{
var prevToken = openBrace.GetPreviousToken();
var triviaList = prevToken.IsKind(SyntaxKind.None) ? openBrace.LeadingTrivia : TriviaHelper.MergeTriviaLists(prevToken.TrailingTrivia, openBrace.LeadingTrivia);
var done = false;
var eolCount = 0;
for (var i = triviaList.Count - 1; !done && (i >= 0); i--)
{
switch (triviaList[i].Kind())
{
case SyntaxKind.WhitespaceTrivia:
break;
case SyntaxKind.EndOfLineTrivia:
eolCount++;
break;
default:
if (triviaList[i].IsDirective)
{
// These have a built-in end of line
eolCount++;
}
done = true;
break;
}
}
if (eolCount < 2)
{
return;
}
context.ReportDiagnostic(Diagnostic.Create(Descriptor, openBrace.GetLocation()));
}
开发者ID:iaingalloway,项目名称:StyleCopAnalyzers,代码行数:35,代码来源:SA1509OpeningCurlyBracketsMustNotBePrecededByBlankLine.cs
示例5: HandleCloseBracketToken
private static void HandleCloseBracketToken(SyntaxTreeAnalysisContext context, SyntaxToken token)
{
if (token.IsMissing)
{
return;
}
if (!token.Parent.IsKind(SyntaxKind.AttributeList))
{
return;
}
if (token.IsFirstInLine())
{
return;
}
SyntaxToken precedingToken = token.GetPreviousToken();
if (!precedingToken.HasTrailingTrivia)
{
return;
}
if (!precedingToken.TrailingTrivia.Last().IsKind(SyntaxKind.WhitespaceTrivia))
{
return;
}
// Closing attribute brackets must not be preceded by a space.
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation()));
}
开发者ID:iaingalloway,项目名称:StyleCopAnalyzers,代码行数:31,代码来源:SA1017ClosingAttributeBracketsMustBeSpacedCorrectly.cs
示例6: FixCloseBraceLeadingTrivia
private static SyntaxToken FixCloseBraceLeadingTrivia(SyntaxToken token)
{
if (!token.HasLeadingTrivia)
{
return token;
}
var triviaList = token.LeadingTrivia;
if (triviaList.All(x => x.IsKind(SyntaxKind.WhitespaceTrivia) || x.IsKind(SyntaxKind.EndOfLineTrivia)))
{
// Simplest case. It's all new lines and white space.
if (EndsWithSimpleNewLine(token.GetPreviousToken().TrailingTrivia))
{
triviaList = SyntaxTriviaList.Empty;
}
else
{
// new line and we are done.
triviaList = SyntaxFactory.TriviaList(SyntaxUtil.GetBestNewLineTrivia(token));
}
}
else
{
triviaList = RemoveNewLinesFromTop(triviaList);
triviaList = RemoveNewLinesFromBottom(triviaList);
}
return token.WithLeadingTrivia(triviaList);
}
示例7: IsAfterEndRegionBeforeMethodDeclaration
private bool IsAfterEndRegionBeforeMethodDeclaration(SyntaxToken previousToken, SyntaxToken currentToken)
{
if (previousToken.Kind() == SyntaxKind.EndOfDirectiveToken)
{
var previousPreviousToken = previousToken.GetPreviousToken();
return previousPreviousToken.Kind() == SyntaxKind.EndRegionKeyword;
}
return false;
}
示例8: GetTransformedDocumentAsync
private static Task<Document> GetTransformedDocumentAsync(Document document, SyntaxNode root, SyntaxToken keywordToken)
{
SyntaxToken hashToken = keywordToken.GetPreviousToken(includeDirectives: true);
if (!hashToken.IsKind(SyntaxKind.HashToken))
{
return Task.FromResult(document);
}
SyntaxToken corrected = hashToken.WithoutTrailingWhitespace().WithoutFormatting();
Document updatedDocument = document.WithSyntaxRoot(root.ReplaceToken(hashToken, corrected));
return Task.FromResult(updatedDocument);
}
示例9: VisitToken
public override SyntaxToken VisitToken(SyntaxToken token)
{
switch (token.CSharpKind())
{
case SyntaxKind.OpenBraceToken:
if (token.GetPreviousToken().CSharpKind() == SyntaxKind.CloseParenToken)
{
return token.WithLeadingTrivia(SyntaxFactory.ElasticLineFeed);
}
break;
}
return token;
}
示例10: GetTokenKind
private TokenKind GetTokenKind(SyntaxToken token)
{
// Customer customer = new Customer();
if (token.Parent.Parent.Kind == SyntaxKind.ObjectCreationExpression)
return TokenKind.ObjectCreation;
// string result = customer.GetFulleName();
if ((token.Parent.Parent.Kind == SyntaxKind.MemberAccessExpression
&& token.Parent.Parent.Parent.Kind == SyntaxKind.InvocationExpression
&& token.GetPreviousToken().Kind == SyntaxKind.DotToken) ||
(token.Parent.Parent.Kind == SyntaxKind.InvocationExpression))
return TokenKind.MethodCall;
return TokenKind.None;
}
示例11: GetTokenWithLineBreaks
private SyntaxToken GetTokenWithLineBreaks(SyntaxToken token)
{
var currentToken = token.GetPreviousToken(includeZeroWidth: true);
while (currentToken.RawKind != 0)
{
if (currentToken.ToFullString().IndexOf('\n') >= 0)
{
return currentToken;
}
currentToken = currentToken.GetPreviousToken(includeZeroWidth: true);
}
return default(SyntaxToken);
}
示例12: GetTransformedDocumentAsync
private static Task<Document> GetTransformedDocumentAsync(Document document, SyntaxNode root, SyntaxToken token)
{
bool precededBySpace;
bool followsSpecialCharacter;
Dictionary<SyntaxToken, SyntaxToken> replacements = new Dictionary<SyntaxToken, SyntaxToken>();
if (!token.IsFirstInLine())
{
SyntaxToken precedingToken = token.GetPreviousToken();
precededBySpace = precedingToken.TrailingTrivia.Any(SyntaxKind.WhitespaceTrivia);
followsSpecialCharacter =
precedingToken.IsKind(SyntaxKind.OpenBracketToken)
|| precedingToken.IsKind(SyntaxKind.OpenParenToken)
|| precedingToken.IsKind(SyntaxKind.CloseParenToken);
if (followsSpecialCharacter && precededBySpace)
{
SyntaxToken correctedPreceding = precedingToken.WithoutTrailingWhitespace().WithoutFormatting();
replacements.Add(precedingToken, correctedPreceding);
}
else if (!followsSpecialCharacter && !precededBySpace)
{
SyntaxToken correctedPreceding = precedingToken.WithoutTrailingWhitespace();
SyntaxTrivia whitespace = SyntaxFactory.ElasticSpace;
correctedPreceding =
correctedPreceding
.WithTrailingTrivia(correctedPreceding.TrailingTrivia.Add(whitespace))
.WithoutFormatting();
replacements.Add(precedingToken, correctedPreceding);
}
}
if (token.TrailingTrivia.Any(SyntaxKind.WhitespaceTrivia) || token.TrailingTrivia.Any(SyntaxKind.EndOfLineTrivia))
{
SyntaxToken corrected = token.WithoutTrailingWhitespace(removeEndOfLineTrivia: true).WithoutFormatting();
replacements.Add(token, corrected);
}
var transformed = root.ReplaceTokens(replacements.Keys, (original, maybeRewritten) => replacements[original]);
Document updatedDocument = document.WithSyntaxRoot(transformed);
return Task.FromResult(updatedDocument);
}
示例13: GetFirstTokenOnTextLine
/// <summary>
/// Gets the first token on the textline that the given token is on.
/// </summary>
/// <param name="token">The token used to determine the textline.</param>
/// <returns>The first token on the textline of the given token.</returns>
public static SyntaxToken GetFirstTokenOnTextLine(SyntaxToken token)
{
while (true)
{
var precedingToken = token.GetPreviousToken();
if (precedingToken.IsKind(SyntaxKind.None))
{
return token;
}
if (precedingToken.GetLine() < token.GetLine())
{
return token;
}
token = precedingToken;
}
}
示例14: Between
public static AnalysisResult Between(SyntaxToken token1, SyntaxToken token2)
{
if (!token1.HasTrailingTrivia && !token2.HasLeadingTrivia)
{
return default(AnalysisResult);
}
var result = default(AnalysisResult);
if (token1.IsMissing && token1.FullWidth() == 0)
{
// Consider the following case:
//
// return // <- note the missing semicolon
// }
//
// in this case, the compiler will insert a missing semicolon token at the
// start of the line containing the close curly. This is problematic as it
// means that if we're looking at the token-pair for the semicolon and close-
// curly, then we'll think there is no newline here. Because we think there
// is no newline, we won't attempt to indent in a manner that preserves tabs
// (if the user has 'use tabs for indent' enabled).
//
// Here we detect if our previous token is an empty missing token. If so,
// we look back to the previous non-missing token to see if it ends with a
// newline. If so, we keep track of that so we'll appropriately indent later
// on.
var previousNonMissingToken = token1.GetPreviousToken(includeZeroWidth: false, includeSkipped: true);
if (previousNonMissingToken.TrailingTrivia.Count > 0 &&
previousNonMissingToken.TrailingTrivia.Last().Kind() == SyntaxKind.EndOfLineTrivia)
{
result.LineBreaks = 1;
}
}
else
{
Analyze(token1.TrailingTrivia, ref result);
}
Analyze(token2.LeadingTrivia, ref result);
return result;
}
示例15: AnalyzeCloseBrace
private static void AnalyzeCloseBrace(SyntaxNodeAnalysisContext context, SyntaxToken closeBraceToken)
{
var previousToken = closeBraceToken.GetPreviousToken();
if ((closeBraceToken.GetLineSpan().StartLinePosition.Line - previousToken.GetLineSpan().EndLinePosition.Line) < 2)
{
// there will be no blank lines when the closing brace and the preceding token are not at least two lines apart.
return;
}
var separatingTrivia = TriviaHelper.MergeTriviaLists(previousToken.TrailingTrivia, closeBraceToken.LeadingTrivia);
// skip all leading whitespace for the close brace
// the index must be checked because two tokens can be more than two lines apart and
// still only be separated by whitespace trivia due to compilation errors
var index = separatingTrivia.Count - 1;
while (index >= 0 && separatingTrivia[index].IsKind(SyntaxKind.WhitespaceTrivia))
{
index--;
}
var done = false;
var eolCount = 0;
while (!done && index >= 0)
{
switch (separatingTrivia[index].Kind())
{
case SyntaxKind.WhitespaceTrivia:
break;
case SyntaxKind.EndOfLineTrivia:
eolCount++;
break;
default:
done = true;
break;
}
index--;
}
if (eolCount > 1)
{
context.ReportDiagnostic(Diagnostic.Create(Descriptor, closeBraceToken.GetLocation()));
}
}
开发者ID:JaRau,项目名称:StyleCopAnalyzers,代码行数:44,代码来源:SA1508ClosingCurlyBracketsMustNotBePrecededByBlankLine.cs