Skip to content

Commit da9bc63

Browse files
committed
Merge pull request #2053 from pdelvo/whitespaceperf
Improve performance of IsPrecededByWhitespace
2 parents da94bf6 + 8e61d3b commit da9bc63

13 files changed

Lines changed: 20 additions & 18 deletions

StyleCop.Analyzers/StyleCop.Analyzers/Helpers/TokenHelper.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace StyleCop.Analyzers.Helpers
55
{
6+
using System.Threading;
67
using Microsoft.CodeAnalysis;
78
using Microsoft.CodeAnalysis.CSharp;
89

@@ -61,17 +62,18 @@ internal static bool IsLastInLine(this SyntaxToken token)
6162
/// Gets a value indicating whether the <paramref name="token"/> is preceded by a whitespace.
6263
/// </summary>
6364
/// <param name="token">The token to process.</param>
65+
/// <param name="cancellationToken">The cancellation token that the operation will observe.</param>
6466
/// <returns>true if token is preceded by a whitespace, otherwise false.</returns>
65-
internal static bool IsPrecededByWhitespace(this SyntaxToken token)
67+
internal static bool IsPrecededByWhitespace(this SyntaxToken token, CancellationToken cancellationToken)
6668
{
67-
SyntaxTriviaList triviaList = token.LeadingTrivia;
68-
if (triviaList.Count > 0)
69+
// Perf: Directly access the text instead of the trivia.
70+
int pos = token.Span.Start - 1;
71+
if (pos < 0 || token.SyntaxTree == null)
6972
{
70-
return triviaList.Last().IsKind(SyntaxKind.WhitespaceTrivia);
73+
return false;
7174
}
7275

73-
triviaList = token.GetPreviousToken().TrailingTrivia;
74-
return triviaList.Count > 0 && triviaList.Last().IsKind(SyntaxKind.WhitespaceTrivia);
76+
return char.IsWhiteSpace(token.SyntaxTree.GetText(cancellationToken)[pos]);
7577
}
7678

7779
/// <summary>

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1001CommasMustBeSpacedCorrectly.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ private static void HandleCommaToken(SyntaxTreeAnalysisContext context, SyntaxTo
9696
}
9797
}
9898

99-
if (token.IsFirstInLine() || token.IsPrecededByWhitespace())
99+
if (token.IsFirstInLine() || token.IsPrecededByWhitespace(context.CancellationToken))
100100
{
101101
// comma must{ not} be {preceded} by whitespace
102102
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), TokenSpacingProperties.RemovePrecedingPreserveLayout, " not", "preceded"));

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1009ClosingParenthesisMustBeSpacedCorrectly.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ private static void HandleCloseParenToken(SyntaxTreeAnalysisContext context, Syn
7676
return;
7777
}
7878

79-
bool precededBySpace = token.IsFirstInLine() || token.IsPrecededByWhitespace();
79+
bool precededBySpace = token.IsFirstInLine() || token.IsPrecededByWhitespace(context.CancellationToken);
8080
bool followedBySpace = token.IsFollowedByWhitespace();
8181
bool lastInLine = token.IsLastInLine();
8282
bool precedesStickyCharacter;

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1010OpeningSquareBracketsMustBeSpacedCorrectly.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ private static void HandleOpenBracketToken(SyntaxTreeAnalysisContext context, Sy
7676

7777
if (!firstInLine)
7878
{
79-
precededBySpace = token.IsPrecededByWhitespace();
79+
precededBySpace = token.IsPrecededByWhitespace(context.CancellationToken);
8080

8181
// ignore if handled by SA1026
8282
ignorePrecedingSpaceProblem = precededBySpace && token.GetPreviousToken().IsKind(SyntaxKind.NewKeyword);

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1011ClosingSquareBracketsMustBeSpacedCorrectly.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private static void HandleCloseBracketToken(SyntaxTreeAnalysisContext context, S
8282
}
8383

8484
bool firstInLine = token.IsFirstInLine();
85-
bool precededBySpace = firstInLine || token.IsPrecededByWhitespace();
85+
bool precededBySpace = firstInLine || token.IsPrecededByWhitespace(context.CancellationToken);
8686
bool followedBySpace = token.IsFollowedByWhitespace();
8787
bool lastInLine = token.IsLastInLine();
8888
bool precedesSpecialCharacter;

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1012OpeningBracesMustBeSpacedCorrectly.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ private static void HandleOpenBraceToken(SyntaxTreeAnalysisContext context, Synt
8888
return;
8989
}
9090

91-
bool precededBySpace = token.IsFirstInLine() || token.IsPrecededByWhitespace();
91+
bool precededBySpace = token.IsFirstInLine() || token.IsPrecededByWhitespace(context.CancellationToken);
9292

9393
if (!precededBySpace)
9494
{

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1013ClosingBracesMustBeSpacedCorrectly.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private static void HandleCloseBraceToken(SyntaxTreeAnalysisContext context, Syn
7373
return;
7474
}
7575

76-
bool precededBySpace = token.IsFirstInLine() || token.IsPrecededByWhitespace();
76+
bool precededBySpace = token.IsFirstInLine() || token.IsPrecededByWhitespace(context.CancellationToken);
7777

7878
if (token.Parent is InterpolationSyntax)
7979
{

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1014OpeningGenericBracketsMustBeSpacedCorrectly.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private static void HandleLessThanToken(SyntaxTreeAnalysisContext context, Synta
8181
}
8282

8383
bool firstInLine = token.IsFirstInLine();
84-
bool precededBySpace = firstInLine || token.IsPrecededByWhitespace();
84+
bool precededBySpace = firstInLine || token.IsPrecededByWhitespace(context.CancellationToken);
8585
bool followedBySpace = token.IsFollowedByWhitespace();
8686

8787
if (!firstInLine && precededBySpace)

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1015ClosingGenericBracketsMustBeSpacedCorrectly.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private static void HandleGreaterThanToken(SyntaxTreeAnalysisContext context, Sy
8484

8585
bool firstInLine = token.IsFirstInLine();
8686
bool lastInLine = token.IsLastInLine();
87-
bool precededBySpace = firstInLine || token.IsPrecededByWhitespace();
87+
bool precededBySpace = firstInLine || token.IsPrecededByWhitespace(context.CancellationToken);
8888
bool followedBySpace = token.IsFollowedByWhitespace();
8989
bool allowTrailingNoSpace;
9090
bool allowTrailingSpace;

StyleCop.Analyzers/StyleCop.Analyzers/SpacingRules/SA1019MemberAccessSymbolsMustBeSpacedCorrectly.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private static void HandleQuestionToken(SyntaxTreeAnalysisContext context, Synta
9898
private static void HandleMemberAccessSymbol(SyntaxTreeAnalysisContext context, SyntaxToken token)
9999
{
100100
bool firstInLine = token.IsFirstInLine();
101-
bool precededBySpace = firstInLine || token.IsPrecededByWhitespace();
101+
bool precededBySpace = firstInLine || token.IsPrecededByWhitespace(context.CancellationToken);
102102
bool followedBySpace = token.IsFollowedByWhitespace();
103103

104104
if (!firstInLine && precededBySpace)

0 commit comments

Comments
 (0)