File tree Expand file tree Collapse file tree 2 files changed +35
-2
lines changed
Microsoft.DotNet.CodeFormatting.Tests/Rules
Microsoft.DotNet.CodeFormatting/Rules Expand file tree Collapse file tree 2 files changed +35
-2
lines changed Original file line number Diff line number Diff line change @@ -157,6 +157,28 @@ class C
157157
158158 var expected = @"// test
159159
160+ class C
161+ {
162+ }" ;
163+ Verify ( source , expected ) ;
164+ }
165+
166+ [ Fact ]
167+ public void CSharpHeaderBeginsWithTargetHeader ( )
168+ {
169+ _options . CopyrightHeader = ImmutableArray . Create ( "// test" , "// test2" ) ;
170+ var source = @"// test
171+ // test2
172+ // file summary
173+
174+ class C
175+ {
176+ }" ;
177+
178+ var expected = @"// test
179+ // test2
180+ // file summary
181+
160182class C
161183{
162184}" ;
Original file line number Diff line number Diff line change @@ -18,7 +18,7 @@ internal sealed partial class CopyrightHeaderRule : SyntaxFormattingRule, ISynta
1818 private abstract class CommonRule
1919 {
2020 /// <summary>
21- /// This is the normalized copyright header that has no comment delimeters .
21+ /// This is the normalized copyright header that has no comment delimiters .
2222 /// </summary>
2323 private readonly ImmutableArray < string > _header ;
2424
@@ -43,7 +43,18 @@ internal SyntaxNode Process(SyntaxNode syntaxNode)
4343 private bool HasCopyrightHeader ( SyntaxNode syntaxNode )
4444 {
4545 var existingHeader = GetExistingHeader ( syntaxNode . GetLeadingTrivia ( ) ) ;
46- return _header . SequenceEqual ( existingHeader ) ;
46+ return SequnceStartsWith ( _header , existingHeader ) ;
47+ }
48+
49+ private bool SequnceStartsWith ( ImmutableArray < string > header , List < string > existingHeader )
50+ {
51+ // Only try if the existing header is at least as long as the new copyright header
52+ if ( existingHeader . Count >= header . Count ( ) )
53+ {
54+ return ! header . Where ( ( headerLine , i ) => existingHeader [ i ] != headerLine ) . Any ( ) ;
55+ }
56+
57+ return false ;
4758 }
4859
4960 private SyntaxNode AddCopyrightHeader ( SyntaxNode syntaxNode )
You can’t perform that action at this time.
0 commit comments