本文整理汇总了C#中SyntaxToken.IsLastInLine方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxToken.IsLastInLine方法的具体用法?C# SyntaxToken.IsLastInLine怎么用?C# SyntaxToken.IsLastInLine使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SyntaxToken
的用法示例。
在下文中一共展示了SyntaxToken.IsLastInLine方法的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: HandleOpenBracketToken
private static void HandleOpenBracketToken(SyntaxTreeAnalysisContext context, SyntaxToken token)
{
if (token.IsMissing)
{
return;
}
if (!token.Parent.IsKind(SyntaxKind.AttributeList))
{
return;
}
if (token.IsLastInLine())
{
return;
}
if (!token.HasTrailingTrivia)
{
return;
}
if (!token.TrailingTrivia[0].IsKind(SyntaxKind.WhitespaceTrivia))
{
return;
}
// Opening attribute brackets must not be followed by a space.
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation()));
}
开发者ID:nvincent,项目名称:StyleCopAnalyzers,代码行数:30,代码来源:SA1016OpeningAttributeBracketsMustBeSpacedCorrectly.cs
示例4: 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"));
}
}
示例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: 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"));
}
}
示例7: 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
示例8: 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;
}
//.........这里部分代码省略.........
示例9: 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 brace must{} be {followed} by a space.
var properties = TokenSpacingProperties.RemoveFollowing;
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties, " not", "followed"));
}
return;
}
bool precededBySpace = token.IsFirstInLine() || token.IsPrecededByWhitespace();
if (!precededBySpace)
{
// Opening brace must{} be {preceded} by a space.
var properties = TokenSpacingProperties.InsertPreceding;
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties, string.Empty, "preceded"));
}
if (!token.IsLastInLine() && !followedBySpace)
{
// Opening brace must{} be {followed} by a space.
var properties = TokenSpacingProperties.InsertFollowing;
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties, string.Empty, "followed"));
}
}
示例10: HandleAsteriskToken
private static void HandleAsteriskToken(SyntaxTreeAnalysisContext context, SyntaxToken token)
{
if (token.IsMissing)
{
return;
}
bool allowAtLineStart;
bool allowAtLineEnd;
bool allowPrecedingSpace;
bool allowTrailingSpace;
switch (token.Parent.Kind())
{
case SyntaxKind.PointerType:
allowAtLineStart = false;
allowAtLineEnd = true;
allowPrecedingSpace = false;
var nextToken = token.GetNextToken();
switch (nextToken.Kind())
{
case SyntaxKind.OpenBracketToken:
case SyntaxKind.OpenParenToken:
allowTrailingSpace = false;
break;
default:
allowTrailingSpace = true;
break;
}
break;
case SyntaxKind.PointerIndirectionExpression:
allowAtLineStart = true;
allowAtLineEnd = false;
allowTrailingSpace = false;
var prevToken = token.GetPreviousToken();
switch (prevToken.Kind())
{
case SyntaxKind.OpenBracketToken:
case SyntaxKind.OpenParenToken:
case SyntaxKind.CloseParenToken:
allowPrecedingSpace = false;
break;
default:
allowPrecedingSpace = true;
break;
}
break;
default:
return;
}
bool firstInLine = token.IsFirstInLine();
bool precededBySpace = firstInLine || token.IsPrecededByWhitespace();
bool followedBySpace = token.IsFollowedByWhitespace();
bool lastInLine = token.IsLastInLine();
if (!allowAtLineStart && firstInLine)
{
// Dereference symbol '*' must {not appear at the beginning of a line}.
var properties = new Dictionary<string, string>
{
[OpenCloseSpacingCodeFixProvider.LocationKey] = OpenCloseSpacingCodeFixProvider.LocationPreceding,
[OpenCloseSpacingCodeFixProvider.ActionKey] = OpenCloseSpacingCodeFixProvider.ActionRemove
};
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties.ToImmutableDictionary(), "not appear at the beginning of a line"));
}
else if (!allowPrecedingSpace && precededBySpace)
{
// Dereference symbol '*' 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 be preceded by a space"));
}
if (!allowAtLineEnd && lastInLine)
{
// Dereference symbol '*' must {not appear at the end of a line}.
var properties = new Dictionary<string, string>
{
[OpenCloseSpacingCodeFixProvider.LocationKey] = OpenCloseSpacingCodeFixProvider.LocationFollowing,
[OpenCloseSpacingCodeFixProvider.ActionKey] = OpenCloseSpacingCodeFixProvider.ActionRemove
};
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), properties.ToImmutableDictionary(), "not appear at the end of a line"));
}
else if (!allowTrailingSpace && followedBySpace)
{
// Dereference symbol '*' 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 be followed by a space"));
}
//.........这里部分代码省略.........
开发者ID:nvincent,项目名称:StyleCopAnalyzers,代码行数:101,代码来源:SA1023DereferenceAndAccessOfSymbolsMustBeSpacedCorrectly.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: UpdateReplaceMap
private static void UpdateReplaceMap(Dictionary<SyntaxToken, SyntaxToken> replaceMap, SyntaxToken token, Diagnostic diagnostic)
{
string location;
if (!diagnostic.Properties.TryGetValue(TokenSpacingProperties.LocationKey, out location))
{
return;
}
string action;
if (!diagnostic.Properties.TryGetValue(TokenSpacingProperties.ActionKey, out action))
{
return;
}
string layout;
if (!diagnostic.Properties.TryGetValue(TokenSpacingProperties.LayoutKey, out layout))
{
layout = TokenSpacingProperties.LayoutPack;
}
SyntaxTriviaList triviaList;
switch (location)
{
case TokenSpacingProperties.LocationPreceding:
var prevToken = token.GetPreviousToken();
switch (action)
{
case TokenSpacingProperties.ActionInsert:
if (!replaceMap.ContainsKey(prevToken))
{
replaceMap[token] = token.WithLeadingTrivia(token.LeadingTrivia.Add(SyntaxFactory.Space));
}
break;
case TokenSpacingProperties.ActionRemove:
bool tokenIsFirstInLine = token.IsFirstInLine();
bool preserveLayout = layout == TokenSpacingProperties.LayoutPreserve;
triviaList = prevToken.TrailingTrivia.AddRange(token.LeadingTrivia);
if (triviaList.Any(t => t.IsDirective))
{
break;
}
replaceMap[prevToken] = prevToken.WithTrailingTrivia();
if ((!preserveLayout || !tokenIsFirstInLine)
&& triviaList.All(i => i.IsKind(SyntaxKind.WhitespaceTrivia) || i.IsKind(SyntaxKind.EndOfLineTrivia)))
{
replaceMap[token] = token.WithLeadingTrivia();
}
else if (tokenIsFirstInLine && token.IsLastInLine())
{
/* This block covers the case where `token` is the only non-trivia token on its line. However,
* the line may still contain non-whitespace trivia which we want the removal process to
* preserve. This code fix only removes the whitespace surrounding `token` if it is the only
* non-whitespace token on the line.
*/
int lastNewLineLeading = token.LeadingTrivia.LastIndexOf(SyntaxKind.EndOfLineTrivia);
int firstNewLineFollowing = token.TrailingTrivia.IndexOf(SyntaxKind.EndOfLineTrivia);
bool onlyWhitespace = true;
for (int i = lastNewLineLeading + 1; i < token.LeadingTrivia.Count; i++)
{
onlyWhitespace &= token.LeadingTrivia[i].IsKind(SyntaxKind.WhitespaceTrivia);
}
firstNewLineFollowing = firstNewLineFollowing == -1 ? token.TrailingTrivia.Count : firstNewLineFollowing;
for (int i = 0; i < firstNewLineFollowing; i++)
{
onlyWhitespace &= token.TrailingTrivia[i].IsKind(SyntaxKind.WhitespaceTrivia);
}
if (onlyWhitespace)
{
// Move the token, and remove the other tokens from its line. Keep all other surrounding
// trivia. Keep the last newline that precedes token, but not the first that follows it.
SyntaxTriviaList trailingTrivia = prevToken.TrailingTrivia;
if (lastNewLineLeading >= 0)
{
trailingTrivia = trailingTrivia.AddRange(token.LeadingTrivia.Take(lastNewLineLeading + 1));
}
// firstNewLineFollowing was adjusted above to account for the missing case.
trailingTrivia = trailingTrivia.AddRange(token.TrailingTrivia.Take(firstNewLineFollowing));
replaceMap[token] = token.WithLeadingTrivia().WithTrailingTrivia(trailingTrivia);
}
else
{
// Just move the token and keep all surrounding trivia.
SyntaxTriviaList trailingTrivia = triviaList.AddRange(token.TrailingTrivia);
replaceMap[token] = token.WithLeadingTrivia().WithTrailingTrivia(trailingTrivia);
}
}
else
{
SyntaxTriviaList trailingTrivia = triviaList.AddRange(token.TrailingTrivia.WithoutLeadingWhitespace(endOfLineIsWhitespace: false));
replaceMap[token] = token.WithLeadingTrivia().WithTrailingTrivia(trailingTrivia);
}
break;
//.........这里部分代码省略.........
示例13: HandleOpenParenToken
private static void HandleOpenParenToken(SyntaxTreeAnalysisContext context, SyntaxToken token)
{
if (token.IsMissing)
{
return;
}
if (token.IsLastInLine())
{
// ignore open parenthesis when last on line.
return;
}
var prevToken = token.GetPreviousToken();
var leadingTriviaList = TriviaHelper.MergeTriviaLists(prevToken.TrailingTrivia, token.LeadingTrivia);
var isFirstOnLine = false;
if (prevToken.GetLineSpan().EndLinePosition.Line < token.GetLineSpan().StartLinePosition.Line)
{
var done = false;
for (var i = leadingTriviaList.Count - 1; !done && (i >= 0); i--)
{
switch (leadingTriviaList[i].Kind())
{
case SyntaxKind.WhitespaceTrivia:
break;
case SyntaxKind.EndOfLineTrivia:
isFirstOnLine = true;
done = true;
break;
default:
done = true;
break;
}
}
}
bool haveLeadingSpace;
bool partOfUnaryExpression;
bool startOfIndexer;
var prevTokenIsOpenParen = prevToken.IsKind(SyntaxKind.OpenParenToken);
switch (token.Parent.Kind())
{
case SyntaxKind.IfStatement:
case SyntaxKind.DoStatement:
case SyntaxKind.WhileStatement:
case SyntaxKind.ForStatement:
case SyntaxKind.ForEachStatement:
case SyntaxKind.SwitchStatement:
case SyntaxKind.FixedStatement:
case SyntaxKind.LockStatement:
case SyntaxKind.UsingStatement:
case SyntaxKind.CatchDeclaration:
case SyntaxKind.CatchFilterClause:
haveLeadingSpace = true;
break;
case SyntaxKind.ArgumentList:
case SyntaxKind.AttributeArgumentList:
case SyntaxKind.CheckedExpression:
case SyntaxKind.UncheckedExpression:
case SyntaxKind.ConstructorConstraint:
case SyntaxKind.DefaultExpression:
case SyntaxKind.SizeOfExpression:
case SyntaxKind.TypeOfExpression:
haveLeadingSpace = false;
break;
case SyntaxKind.ParenthesizedExpression:
partOfUnaryExpression = prevToken.Parent is PrefixUnaryExpressionSyntax;
startOfIndexer = prevToken.IsKind(SyntaxKind.OpenBracketToken);
var partOfCastExpression = prevToken.IsKind(SyntaxKind.CloseParenToken) && prevToken.Parent.IsKind(SyntaxKind.CastExpression);
haveLeadingSpace = !partOfUnaryExpression && !startOfIndexer && !partOfCastExpression;
break;
case SyntaxKind.CastExpression:
partOfUnaryExpression = prevToken.Parent is PrefixUnaryExpressionSyntax;
startOfIndexer = prevToken.IsKind(SyntaxKind.OpenBracketToken);
var consecutiveCast = prevToken.IsKind(SyntaxKind.CloseParenToken) && prevToken.Parent.IsKind(SyntaxKind.CastExpression);
var partOfInterpolation = prevToken.IsKind(SyntaxKind.OpenBraceToken) && prevToken.Parent.IsKind(SyntaxKind.Interpolation);
haveLeadingSpace = !partOfUnaryExpression && !startOfIndexer && !consecutiveCast && !partOfInterpolation;
break;
case SyntaxKind.ParameterList:
var partOfLambdaExpression = token.Parent.Parent.IsKind(SyntaxKind.ParenthesizedLambdaExpression);
haveLeadingSpace = partOfLambdaExpression;
break;
default:
haveLeadingSpace = false;
break;
}
// Ignore spacing before if another opening parenthesis is before this.
//.........这里部分代码省略.........
开发者ID:nvincent,项目名称:StyleCopAnalyzers,代码行数:101,代码来源:SA1008OpeningParenthesisMustBeSpacedCorrectly.cs
示例14: 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
示例15: HandleOpenParenToken
private static void HandleOpenParenToken(SyntaxTreeAnalysisContext context, SyntaxToken token)
{
if (token.IsMissing)
{
return;
}
if (token.IsLastInLine())
{
// ignore open parenthesis when last on line.
return;
}
var prevToken = token.GetPreviousToken();
// Don't check leading spaces when preceded by a keyword that is already handled by SA1000
bool precededByKeyword;
switch (prevToken.Kind())
{
case SyntaxKind.AwaitKeyword:
case SyntaxKind.CaseKeyword:
case SyntaxKind.CatchKeyword:
case SyntaxKind.CheckedKeyword:
case SyntaxKind.DefaultKeyword:
case SyntaxKind.FixedKeyword:
case SyntaxKind.ForKeyword:
case SyntaxKind.ForEachKeyword:
case SyntaxKind.FromKeyword:
case SyntaxKind.GroupKeyword:
case SyntaxKind.IfKeyword:
case SyntaxKind.InKeyword:
case SyntaxKind.IntoKeyword:
case SyntaxKind.JoinKeyword:
case SyntaxKind.LetKeyword:
case SyntaxKind.LockKeyword:
case SyntaxKind.NameOfKeyword:
case SyntaxKind.NewKeyword:
case SyntaxKind.OrderByKeyword:
case SyntaxKind.ReturnKeyword:
case SyntaxKind.SelectKeyword:
case SyntaxKind.SizeOfKeyword:
case SyntaxKind.StackAllocKeyword:
case SyntaxKind.SwitchKeyword:
case SyntaxKind.ThrowKeyword:
case SyntaxKind.TypeOfKeyword:
case SyntaxKind.UncheckedKeyword:
case SyntaxKind.UsingKeyword:
case SyntaxKind.WhereKeyword:
case SyntaxKind.WhileKeyword:
case SyntaxKind.YieldKeyword:
precededByKeyword = true;
break;
default:
precededByKeyword = false;
break;
}
var leadingTriviaList = TriviaHelper.MergeTriviaLists(prevToken.TrailingTrivia, token.LeadingTrivia);
var isFirstOnLine = false;
if (prevToken.GetLineSpan().EndLinePosition.Line < token.GetLineSpan().StartLinePosition.Line)
{
var done = false;
for (var i = leadingTriviaList.Count - 1; !done && (i >= 0); i--)
{
switch (leadingTriviaList[i].Kind())
{
case SyntaxKind.WhitespaceTrivia:
break;
case SyntaxKind.EndOfLineTrivia:
isFirstOnLine = true;
done = true;
break;
default:
done = true;
break;
}
}
}
bool haveLeadingSpace;
bool partOfUnaryExpression;
bool startOfIndexer;
var prevTokenIsOpenParen = prevToken.IsKind(SyntaxKind.OpenParenToken);
switch (token.Parent.Kind())
{
case SyntaxKind.IfStatement:
case SyntaxKind.DoStatement:
case SyntaxKind.WhileStatement:
case SyntaxKind.ForStatement:
case SyntaxKind.ForEachStatement:
case SyntaxKind.SwitchStatement:
case SyntaxKind.FixedStatement:
case SyntaxKind.LockStatement:
case SyntaxKind.UsingStatement:
//.........这里部分代码省略.........
开发者ID:EdwinEngelen,项目名称:StyleCopAnalyzers,代码行数:101,代码来源:SA1008OpeningParenthesisMustBeSpacedCorrectly.cs