Skip to content

Commit

Permalink
Fix behavior of SA1506 when a blank line follows a line comment
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Sep 12, 2015
1 parent cdb724f commit a1189aa
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -572,9 +572,14 @@ public class TestClass
public void TestMethod1() { }
/// <summary>more documentation.</summary>
/* another comment */
public void TestMethod2() { }
/// <summary>more documentation.</summary>
// another comment (a specific regression test for https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1456)
public void TestMethod3() { }
}
";

Expand All @@ -592,6 +597,10 @@ public void TestMethod1() { }
/// <summary>more documentation.</summary>
/* another comment */
public void TestMethod2() { }
/// <summary>more documentation.</summary>
// another comment (a specific regression test for https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/1456)
public void TestMethod3() { }
}
";

Expand All @@ -600,6 +609,7 @@ public void TestMethod2() { }
this.CSharpDiagnostic().WithLocation(3, 1),
this.CSharpDiagnostic().WithLocation(10, 1),
this.CSharpDiagnostic().WithLocation(15, 1),
this.CSharpDiagnostic().WithLocation(20, 1),
};

await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,32 +52,50 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
var triviaList = token.LeadingTrivia;

var index = triviaList.IndexOf(SyntaxKind.SingleLineDocumentationCommentTrivia);
for (; index < triviaList.Count - 1; index++)

int currentLineStart = index + 1;
bool onBlankLine = true;
for (int currentIndex = currentLineStart; currentIndex < triviaList.Count; currentIndex++)
{
if (triviaList[index].IsKind(SyntaxKind.SingleLineCommentTrivia)
|| triviaList[index].IsKind(SyntaxKind.MultiLineCommentTrivia))
switch (triviaList[currentIndex].Kind())
{
break;
}
}
case SyntaxKind.EndOfLineTrivia:
if (onBlankLine)
{
for (int i = 0; i < currentIndex - currentLineStart + 1; i++)
{
triviaList = triviaList.RemoveAt(currentLineStart);
currentIndex = currentLineStart - 1;
}

while (!triviaList[index].IsKind(SyntaxKind.EndOfLineTrivia))
{
index--;
}
continue;
}
else
{
currentLineStart = currentIndex + 1;
onBlankLine = true;
break;
}

var lastEndOfLine = index;
case SyntaxKind.WhitespaceTrivia:
break;

while (!triviaList[index].IsKind(SyntaxKind.SingleLineDocumentationCommentTrivia))
{
index--;
default:
if (triviaList[currentIndex].HasBuiltinEndLine())
{
currentLineStart = currentIndex + 1;
onBlankLine = true;
break;
}
else
{
onBlankLine = false;
break;
}
}
}

var lastDocumentation = index;

var newLeadingTrivia = triviaList.Take(lastDocumentation + 1).Concat(triviaList.Skip(lastEndOfLine + 1));
var newSyntaxRoot = syntaxRoot.ReplaceToken(token, token.WithLeadingTrivia(newLeadingTrivia));

var newSyntaxRoot = syntaxRoot.ReplaceToken(token, token.WithLeadingTrivia(triviaList));
return document.WithSyntaxRoot(newSyntaxRoot);
}
}
Expand Down

0 comments on commit a1189aa

Please sign in to comment.