本文整理汇总了C#中SyntaxToken.IsFirstInLine方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxToken.IsFirstInLine方法的具体用法?C# SyntaxToken.IsFirstInLine怎么用?C# SyntaxToken.IsFirstInLine使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SyntaxToken
的用法示例。
在下文中一共展示了SyntaxToken.IsFirstInLine方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: HandleLessThanToken
private static void HandleLessThanToken(SyntaxTreeAnalysisContext context, SyntaxToken token)
{
if (token.IsMissing)
{
return;
}
switch (token.Parent.Kind())
{
case SyntaxKind.TypeArgumentList:
case SyntaxKind.TypeParameterList:
break;
default:
// not a generic bracket
return;
}
bool firstInLine = token.IsFirstInLine();
bool precededBySpace = firstInLine || token.IsPrecededByWhitespace();
bool followedBySpace = token.IsFollowedByWhitespace();
if (!firstInLine && precededBySpace)
{
// Opening generic brackets must not be {preceded} by a space.
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), "preceded"));
}
if (followedBySpace)
{
// Opening generic brackets must not be {followed} by a space.
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), "followed"));
}
}
开发者ID:Akkenar,项目名称:StyleCopAnalyzers,代码行数:34,代码来源:SA1014OpeningGenericBracketsMustBeSpacedCorrectly.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: 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
示例4: 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(), TokenSpacingProperties.RemoveImmediatePreceding));
}
开发者ID:EdwinEngelen,项目名称:StyleCopAnalyzers,代码行数:31,代码来源:SA1017ClosingAttributeBracketsMustBeSpacedCorrectly.cs
示例5: HandleCloseBraceToken
private static void HandleCloseBraceToken(SyntaxTreeAnalysisContext context, SyntaxToken token)
{
if (token.IsMissing)
{
return;
}
bool precededBySpace = token.IsFirstInLine() || token.IsPrecededByWhitespace();
if (token.Parent is InterpolationSyntax)
{
if (precededBySpace)
{
// Closing curly bracket must{ not} be {preceded} by a space.
var properties = TokenSpacingProperties.RemovePreceding;
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties, " not", "preceded"));
}
return;
}
bool followedBySpace = token.IsFollowedByWhitespace();
bool lastInLine = token.IsLastInLine();
bool precedesSpecialCharacter;
if (!followedBySpace && !lastInLine)
{
SyntaxToken nextToken = token.GetNextToken();
precedesSpecialCharacter =
nextToken.IsKind(SyntaxKind.CloseParenToken)
|| nextToken.IsKind(SyntaxKind.CommaToken)
|| nextToken.IsKind(SyntaxKind.SemicolonToken)
|| nextToken.IsKind(SyntaxKind.DotToken)
|| (nextToken.IsKind(SyntaxKind.QuestionToken) && nextToken.GetNextToken(includeZeroWidth: true).IsKind(SyntaxKind.DotToken))
|| nextToken.IsKind(SyntaxKind.CloseBracketToken);
}
else
{
precedesSpecialCharacter = false;
}
if (!precededBySpace)
{
// Closing curly bracket must{} be {preceded} by a space.
var properties = TokenSpacingProperties.InsertPreceding;
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties, string.Empty, "preceded"));
}
if (!lastInLine && !precedesSpecialCharacter && !followedBySpace)
{
// Closing curly bracket must{} be {followed} by a space.
var properties = TokenSpacingProperties.InsertFollowing;
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties, string.Empty, "followed"));
}
}
开发者ID:hexuefengx,项目名称:StyleCopAnalyzers,代码行数:55,代码来源:SA1013ClosingCurlyBracketsMustBeSpacedCorrectly.cs
示例6: HandleMinusToken
private static void HandleMinusToken(SyntaxTreeAnalysisContext context, SyntaxToken token)
{
if (token.IsMissing)
{
return;
}
if (!token.Parent.IsKind(SyntaxKind.UnaryMinusExpression))
{
return;
}
bool precededBySpace = true;
bool firstInLine = token.IsFirstInLine();
bool followsSpecialCharacter = false;
bool followedBySpace = token.IsFollowedByWhitespace();
bool lastInLine = token.IsLastInLine();
if (!firstInLine)
{
precededBySpace = token.IsPrecededByWhitespace(context.CancellationToken);
SyntaxToken precedingToken = token.GetPreviousToken();
followsSpecialCharacter =
precedingToken.IsKind(SyntaxKind.OpenBracketToken)
|| precedingToken.IsKind(SyntaxKind.OpenParenToken)
|| precedingToken.IsKind(SyntaxKind.CloseParenToken);
}
if (!firstInLine)
{
if (!followsSpecialCharacter && !precededBySpace)
{
// Negative sign must{} be {preceded} by a space.
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), TokenSpacingProperties.InsertPreceding, string.Empty, "preceded"));
}
else if (followsSpecialCharacter && precededBySpace)
{
// Negative sign must{ not} be {preceded} by a space.
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), TokenSpacingProperties.RemovePreceding, " not", "preceded"));
}
}
if (lastInLine || followedBySpace)
{
// Negative sign must{ not} be {followed} by a space.
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), TokenSpacingProperties.RemoveFollowing, " not", "followed"));
}
}
示例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: 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);
}
示例9: HandleLessThanToken
private static void HandleLessThanToken(SyntaxTreeAnalysisContext context, SyntaxToken token)
{
if (token.IsMissing)
{
return;
}
switch (token.Parent.Kind())
{
case SyntaxKind.TypeArgumentList:
case SyntaxKind.TypeParameterList:
break;
default:
// not a generic bracket
return;
}
bool firstInLine = token.IsFirstInLine();
bool precededBySpace = firstInLine || token.IsPrecededByWhitespace();
bool followedBySpace = token.IsFollowedByWhitespace();
if (!firstInLine && precededBySpace)
{
// Opening generic brackets must not be {preceded} by a space.
var properties = new Dictionary<string, string>
{
[OpenCloseSpacingCodeFixProvider.LocationKey] = OpenCloseSpacingCodeFixProvider.LocationPreceding,
[OpenCloseSpacingCodeFixProvider.ActionKey] = OpenCloseSpacingCodeFixProvider.ActionRemove
};
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties.ToImmutableDictionary(), "preceded"));
}
if (followedBySpace)
{
// Opening generic brackets must not be {followed} by a space.
var properties = new Dictionary<string, string>
{
[OpenCloseSpacingCodeFixProvider.LocationKey] = OpenCloseSpacingCodeFixProvider.LocationFollowing,
[OpenCloseSpacingCodeFixProvider.ActionKey] = OpenCloseSpacingCodeFixProvider.ActionRemove
};
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties.ToImmutableDictionary(), "followed"));
}
}
开发者ID:nvincent,项目名称:StyleCopAnalyzers,代码行数:44,代码来源:SA1014OpeningGenericBracketsMustBeSpacedCorrectly.cs
示例10: HandleCloseBracketToken
private void HandleCloseBracketToken(SyntaxTreeAnalysisContext context, SyntaxToken token)
{
if (token.IsMissing)
{
return;
}
// attribute brackets are handled separately
if (token.Parent.IsKind(SyntaxKind.AttributeList))
{
return;
}
bool firstInLine = token.IsFirstInLine();
bool precededBySpace = firstInLine || token.IsPrecededByWhitespace();
bool followedBySpace = token.IsFollowedByWhitespace();
bool lastInLine = token.IsLastInLine();
bool precedesSpecialCharacter;
if (!followedBySpace && !lastInLine)
{
SyntaxToken nextToken = token.GetNextToken();
switch (nextToken.Kind())
{
case SyntaxKind.CloseBracketToken:
case SyntaxKind.OpenParenToken:
case SyntaxKind.CommaToken:
case SyntaxKind.SemicolonToken:
// TODO: "certain types of operator symbols"
case SyntaxKind.DotToken:
case SyntaxKind.OpenBracketToken:
case SyntaxKind.CloseParenToken:
precedesSpecialCharacter = true;
break;
case SyntaxKind.GreaterThanToken:
precedesSpecialCharacter = nextToken.Parent.IsKind(SyntaxKind.TypeArgumentList);
break;
case SyntaxKind.QuestionToken:
precedesSpecialCharacter = nextToken.Parent.IsKind(SyntaxKind.ConditionalAccessExpression);
break;
default:
precedesSpecialCharacter = false;
break;
}
}
else
{
precedesSpecialCharacter = false;
}
if (!firstInLine && precededBySpace)
{
// Closing square bracket must{ not} be {preceded} by a space.
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), " not", "preceded"));
}
if (!lastInLine)
{
if (!precedesSpecialCharacter && !followedBySpace)
{
// Closing square bracket must{} be {followed} by a space.
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), string.Empty, "followed"));
}
else if (precedesSpecialCharacter && followedBySpace)
{
// Closing square bracket must{ not} be {followed} by a space.
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), " not", "followed"));
}
}
}
开发者ID:nukefusion,项目名称:StyleCopAnalyzers,代码行数:73,代码来源:SA1011ClosingSquareBracketsMustBeSpacedCorrectly.cs
示例11: HandleCloseBracketToken
private static void HandleCloseBracketToken(SyntaxTreeAnalysisContext context, SyntaxToken token)
{
if (token.IsMissing)
{
return;
}
// attribute brackets are handled separately
if (token.Parent.IsKind(SyntaxKind.AttributeList))
{
return;
}
bool firstInLine = token.IsFirstInLine();
bool precededBySpace = firstInLine || token.IsPrecededByWhitespace();
bool followedBySpace = token.IsFollowedByWhitespace();
bool lastInLine = token.IsLastInLine();
bool precedesSpecialCharacter;
// Tests for this rule have a lot of exclusions which are supposed to be caught by other rules
bool suppressFollowingSpaceError = true;
if (!lastInLine)
{
SyntaxToken nextToken = token.GetNextToken();
switch (nextToken.Kind())
{
case SyntaxKind.CloseBracketToken:
case SyntaxKind.OpenParenToken:
case SyntaxKind.CommaToken:
case SyntaxKind.SemicolonToken:
// TODO: "certain types of operator symbols"
case SyntaxKind.DotToken:
case SyntaxKind.OpenBracketToken:
case SyntaxKind.CloseParenToken:
precedesSpecialCharacter = true;
break;
case SyntaxKind.PlusPlusToken:
case SyntaxKind.MinusMinusToken:
precedesSpecialCharacter = true;
suppressFollowingSpaceError = false;
break;
case SyntaxKind.GreaterThanToken:
precedesSpecialCharacter = nextToken.Parent.IsKind(SyntaxKind.TypeArgumentList);
break;
case SyntaxKind.QuestionToken:
precedesSpecialCharacter = nextToken.Parent.IsKind(SyntaxKind.ConditionalAccessExpression);
break;
case SyntaxKind.CloseBraceToken:
precedesSpecialCharacter = nextToken.Parent is InterpolationSyntax;
break;
default:
precedesSpecialCharacter = false;
break;
}
}
else
{
precedesSpecialCharacter = false;
}
if (!firstInLine && precededBySpace)
{
// Closing square bracket must{ not} be {preceded} by a space.
var properties = OpenCloseSpacingCodeFixProvider.RemovePreceding;
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties, " not", "preceded"));
}
if (!lastInLine)
{
if (!precedesSpecialCharacter && !followedBySpace)
{
// Closing square bracket must{} be {followed} by a space.
var properties = OpenCloseSpacingCodeFixProvider.InsertFollowing;
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties, string.Empty, "followed"));
}
else if (precedesSpecialCharacter && followedBySpace && !suppressFollowingSpaceError)
{
// Closing square brackets must {not} be {followed} by a space
var properties = OpenCloseSpacingCodeFixProvider.RemoveFollowing;
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties, " not", "followed"));
}
}
}
开发者ID:robinsedlaczek,项目名称:StyleCopAnalyzers,代码行数:88,代码来源:SA1011ClosingSquareBracketsMustBeSpacedCorrectly.cs
示例12: HandleGreaterThanToken
private static void HandleGreaterThanToken(SyntaxTreeAnalysisContext context, SyntaxToken token)
{
if (token.IsMissing)
{
return;
}
switch (token.Parent.Kind())
{
case SyntaxKind.TypeArgumentList:
case SyntaxKind.TypeParameterList:
break;
default:
// not a generic bracket
return;
}
bool firstInLine = token.IsFirstInLine();
bool lastInLine = token.IsLastInLine();
bool precededBySpace = firstInLine || token.IsPrecededByWhitespace();
bool followedBySpace = token.IsFollowedByWhitespace();
bool allowTrailingNoSpace;
bool allowTrailingSpace;
if (!lastInLine)
{
SyntaxToken nextToken = token.GetNextToken();
switch (nextToken.Kind())
{
case SyntaxKind.OpenParenToken:
// DotToken isn't listed above, but it's required for reasonable member access formatting
case SyntaxKind.DotToken:
// CommaToken isn't listed above, but it's required for reasonable nested generic type arguments formatting
case SyntaxKind.CommaToken:
// OpenBracketToken isn't listed above, but it's required for reasonable array type formatting
case SyntaxKind.OpenBracketToken:
// SemicolonToken isn't listed above, but it's required for reasonable using alias declaration formatting
case SyntaxKind.SemicolonToken:
allowTrailingNoSpace = true;
allowTrailingSpace = false;
break;
case SyntaxKind.CloseParenToken:
case SyntaxKind.GreaterThanToken:
allowTrailingNoSpace = true;
allowTrailingSpace = true;
break;
case SyntaxKind.QuestionToken:
allowTrailingNoSpace = nextToken.Parent.IsKind(SyntaxKind.NullableType);
allowTrailingSpace = true;
break;
default:
allowTrailingNoSpace = false;
allowTrailingSpace = true;
break;
}
}
else
{
allowTrailingNoSpace = true;
allowTrailingSpace = true;
}
if (!firstInLine && precededBySpace)
{
// Closing generic bracket must{ not} be {preceded} by a space.
var properties = new Dictionary<string, string>
{
[OpenCloseSpacingCodeFixProvider.LocationKey] = OpenCloseSpacingCodeFixProvider.LocationPreceding,
[OpenCloseSpacingCodeFixProvider.ActionKey] = OpenCloseSpacingCodeFixProvider.ActionRemove
};
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties.ToImmutableDictionary(), " not", "preceded"));
}
if (!lastInLine)
{
if (!allowTrailingNoSpace && !followedBySpace)
{
// Closing generic bracket must{} be {followed} by a space.
var properties = new Dictionary<string, string>
{
[OpenCloseSpacingCodeFixProvider.LocationKey] = OpenCloseSpacingCodeFixProvider.LocationFollowing,
[OpenCloseSpacingCodeFixProvider.ActionKey] = OpenCloseSpacingCodeFixProvider.ActionInsert
};
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties.ToImmutableDictionary(), string.Empty, "followed"));
}
else if (!allowTrailingSpace && followedBySpace)
{
// Closing generic bracket must{ not} be {followed} by a space.
var properties = new Dictionary<string, string>
{
[OpenCloseSpacingCodeFixProvider.LocationKey] = OpenCloseSpacingCodeFixProvider.LocationFollowing,
[OpenCloseSpacingCodeFixProvider.ActionKey] = OpenCloseSpacingCodeFixProvider.ActionRemove
};
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties.ToImmutableDictionary(), " not", "followed"));
}
}
//.........这里部分代码省略.........
开发者ID:chrfin,项目名称:StyleCopAnalyzers,代码行数:101,代码来源:SA1015ClosingGenericBracketsMustBeSpacedCorrectly.cs
示例13: HandleOpenBraceToken
private static void HandleOpenBraceToken(SyntaxTreeAnalysisContext context, SyntaxToken token)
{
if (token.IsMissing)
{
return;
}
bool followedBySpace = token.IsFollowedByWhitespace();
if (token.Parent is InterpolationSyntax)
{
if (followedBySpace)
{
// Opening curly bracket must{} be {followed} by a space.
var properties = new Dictionary<string, string>
{
[OpenCloseSpacingCodeFixProvider.LocationKey] = OpenCloseSpacingCodeFixProvider.LocationFollowing,
[OpenCloseSpacingCodeFixProvider.ActionKey] = OpenCloseSpacingCodeFixProvider.ActionRemove
};
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties.ToImmutableDictionary(), " not", "followed"));
}
return;
}
bool precededBySpace = token.IsFirstInLine() || token.IsPrecededByWhitespace();
if (!precededBySpace)
{
// Opening curly bracket must{} be {preceded} by a space.
var properties = new Dictionary<string, string>
{
[OpenCloseSpacingCodeFixProvider.LocationKey] = OpenCloseSpacingCodeFixProvider.LocationPreceding,
[OpenCloseSpacingCodeFixProvider.ActionKey] = OpenCloseSpacingCodeFixProvider.ActionInsert
};
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties.ToImmutableDictionary(), string.Empty, "preceded"));
}
if (!token.IsLastInLine() && !followedBySpace)
{
// Opening curly bracket must{} be {followed} by a space.
var properties = new Dictionary<string, string>
{
[OpenCloseSpacingCodeFixProvider.LocationKey] = OpenCloseSpacingCodeFixProvider.LocationFollowing,
[OpenCloseSpacingCodeFixProvider.ActionKey] = OpenCloseSpacingCodeFixProvider.ActionInsert
};
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties.ToImmutableDictionary(), string.Empty, "followed"));
}
}
示例14: HandleCloseParenToken
private static void HandleCloseParenToken(SyntaxTreeAnalysisContext context, SyntaxToken token)
{
if (token.IsMissing)
{
return;
}
bool precededBySpace = token.IsFirstInLine() || token.IsPrecededByWhitespace();
bool followedBySpace = token.IsFollowedByWhitespace();
bool lastInLine = token.IsLastInLine();
bool precedesStickyCharacter;
bool allowEndOfLine = false;
bool suppressFollowingSpaceError = false;
SyntaxToken nextToken = token.GetNextToken();
switch (nextToken.Kind())
{
case SyntaxKind.OpenParenToken:
case SyntaxKind.CloseParenToken:
case SyntaxKind.OpenBracketToken:
case SyntaxKind.CloseBracketToken:
case SyntaxKind.SemicolonToken:
case SyntaxKind.CommaToken:
precedesStickyCharacter = true;
break;
case SyntaxKind.QuestionToken:
if (nextToken.Parent.IsKind(SyntaxKind.ConditionalAccessExpression))
{
// allow a space for this case, but only if the ')' character is the last on the line
allowEndOfLine = true;
precedesStickyCharacter = true;
}
else
{
precedesStickyCharacter = false;
}
break;
case SyntaxKind.PlusToken:
precedesStickyCharacter = nextToken.Parent.IsKind(SyntaxKind.UnaryPlusExpression);
// this will be reported as SA1022
suppressFollowingSpaceError = true;
break;
case SyntaxKind.MinusToken:
precedesStickyCharacter = nextToken.Parent.IsKind(SyntaxKind.UnaryMinusExpression);
// this will be reported as SA1021
suppressFollowingSpaceError = true;
break;
case SyntaxKind.DotToken:
// allow a space for this case, but only if the ')' character is the last on the line
allowEndOfLine = true;
precedesStickyCharacter = true;
break;
case SyntaxKind.ColonToken:
bool requireSpace =
nextToken.Parent.IsKind(SyntaxKind.ConditionalExpression)
|| nextToken.Parent.IsKind(SyntaxKind.BaseConstructorInitializer)
|| nextToken.Parent.IsKind(SyntaxKind.ThisConstructorInitializer);
precedesStickyCharacter = !requireSpace;
break;
case SyntaxKind.PlusPlusToken:
case SyntaxKind.MinusMinusToken:
precedesStickyCharacter = true;
suppressFollowingSpaceError = false;
break;
case SyntaxKind.CloseBraceToken:
precedesStickyCharacter = nextToken.Parent is InterpolationSyntax;
break;
default:
precedesStickyCharacter = false;
break;
}
switch (token.Parent.Kind())
{
case SyntaxKind.CastExpression:
precedesStickyCharacter = true;
break;
default:
break;
}
foreach (var trivia in token.TrailingTrivia)
{
if (trivia.IsKind(SyntaxKind.EndOfLineTrivia))
{
break;
}
//.........这里部分代码省略.........
示例15: 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"));
}
}