Skip to content

Commit ee0bdfc

Browse files
committed
Implement CR feedback
1 parent 5fcb356 commit ee0bdfc

3 files changed

Lines changed: 74 additions & 5 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Test.HelperTests
5+
{
6+
using System.Linq;
7+
using Analyzers.Helpers;
8+
using Microsoft.CodeAnalysis;
9+
using Microsoft.CodeAnalysis.CSharp;
10+
using Microsoft.CodeAnalysis.CSharp.Syntax;
11+
using Xunit;
12+
13+
public class TokenHelperTests
14+
{
15+
[Fact]
16+
public void TestIsFirstInLineSpecialCase()
17+
{
18+
string testCode = @"class MyClass {
19+
} /// <summary>
20+
/// Text
21+
/// </summary>
22+
struct MyStruct { }";
23+
var syntaxTree = CSharpSyntaxTree.ParseText(testCode);
24+
var root = syntaxTree.GetRoot();
25+
26+
Assert.True(root.FindToken(64).IsFirstInLine());
27+
}
28+
29+
[Fact]
30+
public void TestIsLastInLineSpecialCase()
31+
{
32+
string testCode = @"class MyClass {
33+
} /// <summary>
34+
/// Text
35+
/// </summary>
36+
struct /**
37+
Foo
38+
**/ MyStruct { }";
39+
var syntaxTree = CSharpSyntaxTree.ParseText(testCode);
40+
var root = syntaxTree.GetRoot();
41+
42+
Assert.True(root.FindToken(64).IsLastInLine());
43+
}
44+
}
45+
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/StyleCop.Analyzers.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@
169169
<Compile Include="Helpers\DiagnosticVerifier.Helper.cs" />
170170
<Compile Include="Helpers\ExclusionTestAnalyzer.cs" />
171171
<Compile Include="Helpers\TestDiagnosticProvider.cs" />
172+
<Compile Include="HelperTests\TokenHelperTests.cs" />
172173
<Compile Include="HelperTests\TriviaHelperTests.cs" />
173174
<Compile Include="LayoutRules\SA1500\SA1500UnitTests.Blocks.cs" />
174175
<Compile Include="LayoutRules\SA1500\SA1500UnitTests.Classes.cs" />

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

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,15 @@ internal static class TokenHelper
1919
/// <returns>true if token is first in line, otherwise false.</returns>
2020
internal static bool IsFirstInLine(this SyntaxToken token)
2121
{
22-
return token.SyntaxTree == null
23-
|| token.SyntaxTree.GetLineSpan(token.FullSpan).StartLinePosition.Character == 0;
22+
var fullLineSpan = token.SyntaxTree.GetLineSpan(token.FullSpan);
23+
24+
if (token.SyntaxTree == null || fullLineSpan.StartLinePosition.Character == 0)
25+
{
26+
return true;
27+
}
28+
29+
var tokenLineSpan = token.SyntaxTree.GetLineSpan(token.Span);
30+
return tokenLineSpan.StartLinePosition.Line != fullLineSpan.StartLinePosition.Line;
2431
}
2532

2633
/// <summary>
@@ -30,9 +37,25 @@ internal static bool IsFirstInLine(this SyntaxToken token)
3037
/// <returns>true if token is last in line, otherwise false.</returns>
3138
internal static bool IsLastInLine(this SyntaxToken token)
3239
{
33-
return token.SyntaxTree == null
34-
|| token.SyntaxTree.GetLineSpan(token.FullSpan).EndLinePosition.Character == 0
35-
|| token.SyntaxTree.Length == token.FullSpan.End;
40+
var fullLineSpan = token.SyntaxTree.GetLineSpan(token.FullSpan);
41+
42+
if (token.SyntaxTree == null || fullLineSpan.EndLinePosition.Character == 0)
43+
{
44+
return true;
45+
}
46+
47+
var tokenLineSpan = token.SyntaxTree.GetLineSpan(token.Span);
48+
49+
if (tokenLineSpan.EndLinePosition.Line != fullLineSpan.EndLinePosition.Line
50+
|| token.SyntaxTree.Length == token.FullSpan.End)
51+
{
52+
return true;
53+
}
54+
55+
// Use the slow path because we cant be sure if a multi line trivia is following this
56+
// token.
57+
var nextToken = token.GetNextToken();
58+
return nextToken.IsKind(SyntaxKind.None) || token.GetEndLine() < nextToken.GetLine();
3659
}
3760

3861
/// <summary>

0 commit comments

Comments
 (0)