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

Commit b5b7e56

Browse files
committed
Fix multiline copyright header rule
1 parent 32fec62 commit b5b7e56

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

src/Microsoft.DotNet.CodeFormatting.Tests/Rules/CopyrightHeaderRuleTests.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,25 @@ class C
2929

3030
var expected = @"// test
3131
32+
class C
33+
{
34+
}";
35+
Verify(source, expected);
36+
37+
}
38+
39+
[Fact]
40+
public void CSharpSimpleMultiline()
41+
{
42+
_options.CopyrightHeader = ImmutableArray.Create("test1", "test2");
43+
var source = @"
44+
class C
45+
{
46+
}";
47+
48+
var expected = @"// test1
49+
// test2
50+
3251
class C
3352
{
3453
}";
@@ -48,6 +67,62 @@ class C
4867

4968
var expected = @"// test
5069
70+
class C
71+
{
72+
}";
73+
Verify(source, expected);
74+
75+
}
76+
77+
[Fact]
78+
public void CSharpPreserveExistingMultiline()
79+
{
80+
_options.CopyrightHeader = ImmutableArray.Create("test1", "test2");
81+
var source = @"// test1
82+
// test2
83+
84+
class C
85+
{
86+
}";
87+
88+
var expected = @"// test1
89+
// test2
90+
91+
class C
92+
{
93+
}";
94+
Verify(source, expected);
95+
96+
}
97+
98+
[Fact]
99+
public void CSharpPreserveExistingWithCommentMultiline()
100+
{
101+
_options.CopyrightHeader = ImmutableArray.Create("test1", "test2");
102+
var source = @"// test1
103+
// test2
104+
105+
106+
107+
108+
109+
// test3
110+
111+
112+
class C
113+
{
114+
}";
115+
116+
var expected = @"// test1
117+
// test2
118+
119+
120+
121+
122+
123+
// test3
124+
125+
51126
class C
52127
{
53128
}";

src/Microsoft.DotNet.CodeFormatting/Rules/CopyrightHeaderRule.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ private List<string> GetExistingHeader(SyntaxTriviaList triviaList)
7979
{
8080
headerList.Add(GetCommentText(triviaList[i].ToFullString()));
8181
i++;
82+
MoveToNextLineOrTrivia(triviaList, ref i);
8283
}
8384

8485
return headerList;
@@ -113,10 +114,19 @@ private SyntaxTriviaList RemoveExistingHeader(SyntaxTriviaList oldList)
113114

114115
private void MovePastBlankLines(SyntaxTriviaList list, ref int index)
115116
{
116-
while (index < list.Count && IsWhiteSpaceOrNewLine(list[index]))
117+
while (MoveToNextLineOrTrivia(list, ref index))
118+
{
119+
}
120+
}
121+
122+
private bool MoveToNextLineOrTrivia(SyntaxTriviaList list, ref int index)
123+
{
124+
bool moved = index < list.Count && IsWhiteSpaceOrNewLine(list[index]);
125+
if (moved)
117126
{
118127
index++;
119128
}
129+
return moved;
120130
}
121131

122132
protected abstract SyntaxTriviaList CreateTriviaList(IEnumerable<SyntaxTrivia> e);

0 commit comments

Comments
 (0)