Skip to content

Commit 5fcb356

Browse files
committed
Improve IsFirstInLine and IsLastInLine performance
1 parent bceaf44 commit 5fcb356

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers/Helpers/TokenHelpers.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace StyleCop.Analyzers.Helpers
55
{
66
using Microsoft.CodeAnalysis;
77
using Microsoft.CodeAnalysis.CSharp;
8+
using Microsoft.CodeAnalysis.Text;
89

910
/// <summary>
1011
/// Provides helper methods to work with token.
@@ -18,8 +19,8 @@ internal static class TokenHelper
1819
/// <returns>true if token is first in line, otherwise false.</returns>
1920
internal static bool IsFirstInLine(this SyntaxToken token)
2021
{
21-
SyntaxToken previousToken = token.GetPreviousToken();
22-
return previousToken.IsKind(SyntaxKind.None) || previousToken.GetEndLine() < token.GetLine();
22+
return token.SyntaxTree == null
23+
|| token.SyntaxTree.GetLineSpan(token.FullSpan).StartLinePosition.Character == 0;
2324
}
2425

2526
/// <summary>
@@ -29,8 +30,9 @@ internal static bool IsFirstInLine(this SyntaxToken token)
2930
/// <returns>true if token is last in line, otherwise false.</returns>
3031
internal static bool IsLastInLine(this SyntaxToken token)
3132
{
32-
SyntaxToken nextToken = token.GetNextToken();
33-
return nextToken.IsKind(SyntaxKind.None) || token.GetEndLine() < nextToken.GetLine();
33+
return token.SyntaxTree == null
34+
|| token.SyntaxTree.GetLineSpan(token.FullSpan).EndLinePosition.Character == 0
35+
|| token.SyntaxTree.Length == token.FullSpan.End;
3436
}
3537

3638
/// <summary>

0 commit comments

Comments
 (0)