Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #76 from krwq/FixCopyRightHeaderRule
Browse files Browse the repository at this point in the history
Fix multiline copyright header rule
  • Loading branch information
krwq committed Feb 27, 2015
2 parents 67488a7 + 6aa8070 commit 299dd7a
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@ class C

var expected = @"// test
class C
{
}";
Verify(source, expected);

}

[Fact]
public void CSharpSimpleMultiline()
{
_options.CopyrightHeader = ImmutableArray.Create("test1", "test2");
var source = @"
class C
{
}";

var expected = @"// test1
// test2
class C
{
}";
Expand All @@ -48,6 +67,62 @@ class C

var expected = @"// test
class C
{
}";
Verify(source, expected);

}

[Fact]
public void CSharpPreserveExistingMultiline()
{
_options.CopyrightHeader = ImmutableArray.Create("test1", "test2");
var source = @"// test1
// test2
class C
{
}";

var expected = @"// test1
// test2
class C
{
}";
Verify(source, expected);

}

[Fact]
public void CSharpPreserveExistingWithCommentMultiline()
{
_options.CopyrightHeader = ImmutableArray.Create("test1", "test2");
var source = @"// test1
// test2
// test3
class C
{
}";

var expected = @"// test1
// test2
// test3
class C
{
}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ protected override bool IsLineComment(SyntaxTrivia trivia)
return trivia.Kind() == SyntaxKind.SingleLineCommentTrivia;
}

protected override bool IsWhiteSpaceOrNewLine(SyntaxTrivia trivia)
protected override bool IsWhitespace(SyntaxTrivia trivia)
{
return
trivia.Kind() == SyntaxKind.WhitespaceTrivia ||
trivia.Kind() == SyntaxKind.EndOfLineTrivia;
return trivia.Kind() == SyntaxKind.WhitespaceTrivia;
}

protected override bool IsNewLine(SyntaxTrivia trivia)
{
return trivia.Kind() == SyntaxKind.EndOfLineTrivia;
}

protected override SyntaxTrivia CreateLineComment(string commentText)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ protected override bool IsLineComment(SyntaxTrivia trivia)
return trivia.Kind() == SyntaxKind.CommentTrivia;
}

protected override bool IsWhiteSpaceOrNewLine(SyntaxTrivia trivia)
protected override bool IsWhitespace(SyntaxTrivia trivia)
{
return
trivia.Kind() == SyntaxKind.WhitespaceTrivia ||
trivia.Kind() == SyntaxKind.EndOfLineTrivia;
return trivia.Kind() == SyntaxKind.WhitespaceTrivia;
}

protected override bool IsNewLine(SyntaxTrivia trivia)
{
return trivia.Kind() == SyntaxKind.EndOfLineTrivia;
}

protected override SyntaxTrivia CreateLineComment(string commentText)
Expand Down
24 changes: 22 additions & 2 deletions src/Microsoft.DotNet.CodeFormatting/Rules/CopyrightHeaderRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ private List<string> GetExistingHeader(SyntaxTriviaList triviaList)
{
headerList.Add(GetCommentText(triviaList[i].ToFullString()));
i++;
MoveToNextLineOrTrivia(triviaList, ref i);
}

return headerList;
Expand Down Expand Up @@ -113,7 +114,25 @@ private SyntaxTriviaList RemoveExistingHeader(SyntaxTriviaList oldList)

private void MovePastBlankLines(SyntaxTriviaList list, ref int index)
{
while (index < list.Count && IsWhiteSpaceOrNewLine(list[index]))
while (index < list.Count && (IsWhitespace(list[index]) || IsNewLine(list[index])))
{
index++;
}
}

private void MoveToNextLineOrTrivia(SyntaxTriviaList list, ref int index)
{
MovePastWhitespaces(list, ref index);

if (index < list.Count && IsNewLine(list[index]))
{
index++;
}
}

private void MovePastWhitespaces(SyntaxTriviaList list, ref int index)
{
while (index < list.Count && IsWhitespace(list[index]))
{
index++;
}
Expand All @@ -123,7 +142,8 @@ private void MovePastBlankLines(SyntaxTriviaList list, ref int index)

protected abstract bool IsLineComment(SyntaxTrivia trivia);

protected abstract bool IsWhiteSpaceOrNewLine(SyntaxTrivia trivia);
protected abstract bool IsWhitespace(SyntaxTrivia trivia);
protected abstract bool IsNewLine(SyntaxTrivia trivia);

protected abstract SyntaxTrivia CreateLineComment(string commentText);

Expand Down

0 comments on commit 299dd7a

Please sign in to comment.