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

Commit a55d026

Browse files
committed
Basic copyright header support
This extends the copyright header rule support to Visual Basic.
1 parent 2eaf9a2 commit a55d026

13 files changed

+380
-88
lines changed

src/Microsoft.DotNet.CodeFormatting.Tests/CodeFormattingTestBase.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ private void AssertSolutionEqual(Solution expectedSolution, Solution actualSolut
9393

9494
protected abstract Task<Document> RewriteDocumentAsync(Document document);
9595

96-
protected void Verify(string[] sources, string[] expected, bool runFormatter)
96+
protected void Verify(string[] sources, string[] expected, bool runFormatter, string languageName)
9797
{
98-
var inputSolution = CreateSolution(sources);
99-
var expectedSolution = CreateSolution(expected);
98+
var inputSolution = CreateSolution(sources, languageName);
99+
var expectedSolution = CreateSolution(expected, languageName);
100100
var actualSolution = Format(inputSolution, runFormatter).Result;
101101

102102
if (actualSolution == null)
@@ -105,9 +105,9 @@ protected void Verify(string[] sources, string[] expected, bool runFormatter)
105105
AssertSolutionEqual(expectedSolution, actualSolution);
106106
}
107107

108-
protected void Verify(string source, string expected, bool runFormatter = true)
108+
protected void Verify(string source, string expected, bool runFormatter = true, string languageName = LanguageNames.CSharp)
109109
{
110-
Verify(new string[] { source }, new string[] { expected }, runFormatter);
110+
Verify(new string[] { source }, new string[] { expected }, runFormatter, languageName);
111111
}
112112
}
113113

src/Microsoft.DotNet.CodeFormatting.Tests/Microsoft.DotNet.CodeFormatting.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
<Compile Include="CodeFormattingTestBase.cs" />
108108
<Compile Include="Rules\BracesRuleTests.cs" />
109109
<Compile Include="Rules\CombinationTest.cs" />
110+
<Compile Include="Rules\CopyrightHeaderRuleTests.cs" />
110111
<Compile Include="Rules\ExplicitThisRuleTests.cs" />
111112
<Compile Include="Rules\ExplicitVisibilityRuleTests.cs" />
112113
<Compile Include="Rules\HasNewLineBeforeFirstNamespaceFormattingRuleTests.cs" />

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ static CombinationTest()
2424

2525
public CombinationTest()
2626
{
27-
s_formattingEngine.CopyrightHeader = ImmutableArray.Create("", "// header");
27+
s_formattingEngine.CopyrightHeader = ImmutableArray.Create("// header");
2828
s_formattingEngine.FormatLogger = new EmptyFormatLogger();
2929
s_formattingEngine.PreprocessorConfigurations = ImmutableArray<string[]>.Empty;
3030
}
@@ -50,8 +50,7 @@ void M() {
5050
}
5151
}";
5252

53-
var expected = @"
54-
// header
53+
var expected = @"// header
5554
5655
internal class C
5756
{
@@ -79,8 +78,7 @@ void M() {
7978
}
8079
}";
8180

82-
var expected = @"
83-
// header
81+
var expected = @"// header
8482
8583
internal class C
8684
{
@@ -106,8 +104,7 @@ void M() { }
106104
#endif
107105
}";
108106

109-
var expected = @"
110-
// header
107+
var expected = @"// header
111108
112109
internal class C
113110
{
@@ -131,8 +128,7 @@ internal void M() {
131128
#endif
132129
}";
133130

134-
var expected = @"
135-
// header
131+
var expected = @"// header
136132
137133
internal class C
138134
{
@@ -163,8 +159,7 @@ void M() {
163159
#endif
164160
}";
165161

166-
var expected = @"
167-
// header
162+
var expected = @"// header
168163
169164
internal class C
170165
{
@@ -202,8 +197,7 @@ void M() {
202197
#endif
203198
}";
204199

205-
var expected = @"
206-
// header
200+
var expected = @"// header
207201
208202
internal class C
209203
{
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
using Microsoft.CodeAnalysis;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Collections.Immutable;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using Xunit;
9+
10+
namespace Microsoft.DotNet.CodeFormatting.Tests
11+
{
12+
public sealed class CopyrightHeaderRuleTests : SyntaxRuleTestBase
13+
{
14+
private readonly Options _options = new Options();
15+
16+
internal override ISyntaxFormattingRule Rule
17+
{
18+
get { return new Rules.CopyrightHeaderRule(_options); }
19+
}
20+
21+
[Fact]
22+
public void CSharpSimple()
23+
{
24+
_options.CopyrightHeader = ImmutableArray.Create("test");
25+
var source = @"
26+
class C
27+
{
28+
}";
29+
30+
var expected = @"// test
31+
32+
class C
33+
{
34+
}";
35+
Verify(source, expected);
36+
37+
}
38+
39+
[Fact]
40+
public void CSharpPreserveExisting()
41+
{
42+
_options.CopyrightHeader = ImmutableArray.Create("test");
43+
var source = @"// test
44+
45+
class C
46+
{
47+
}";
48+
49+
var expected = @"// test
50+
51+
class C
52+
{
53+
}";
54+
Verify(source, expected);
55+
56+
}
57+
58+
[Fact]
59+
public void CSharpDontDoubleComment()
60+
{
61+
_options.CopyrightHeader = ImmutableArray.Create("// test");
62+
var source = @"
63+
class C
64+
{
65+
}";
66+
67+
var expected = @"// test
68+
69+
class C
70+
{
71+
}";
72+
Verify(source, expected);
73+
}
74+
75+
[Fact]
76+
public void VisualBasicSimple()
77+
{
78+
_options.CopyrightHeader = ImmutableArray.Create("test");
79+
var source = @"
80+
Public Class C
81+
End Class";
82+
83+
var expected = @"' test
84+
85+
Public Class C
86+
End Class";
87+
88+
Verify(source, expected, languageName: LanguageNames.VisualBasic);
89+
}
90+
91+
[Fact]
92+
public void VisualBasicNormalizeComment()
93+
{
94+
_options.CopyrightHeader = ImmutableArray.Create("// test");
95+
var source = @"
96+
Public Class C
97+
End Class";
98+
99+
var expected = @"' test
100+
101+
Public Class C
102+
End Class";
103+
104+
Verify(source, expected, languageName: LanguageNames.VisualBasic);
105+
}
106+
107+
[Fact]
108+
public void VisualBasicPreserveExisting()
109+
{
110+
_options.CopyrightHeader = ImmutableArray.Create("// test");
111+
var source = @"' test
112+
113+
Public Class C
114+
End Class";
115+
116+
var expected = @"' test
117+
118+
Public Class C
119+
End Class";
120+
121+
Verify(source, expected, languageName: LanguageNames.VisualBasic);
122+
}
123+
}
124+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
using Microsoft.CodeAnalysis;
45
using System;
56
using System.Collections.Generic;
67
using System.Linq;
@@ -338,7 +339,7 @@ public partial class C { }
338339
public partial class C { }
339340
";
340341

341-
Verify(new[] { text1, text2 }, new[] { expected1, expected2 }, runFormatter: false);
342+
Verify(new[] { text1, text2 }, new[] { expected1, expected2 }, runFormatter: false, languageName: LanguageNames.CSharp);
342343
}
343344

344345
[Fact]

src/Microsoft.DotNet.CodeFormatting/FormattingEngineImplementation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ private Task<SyntaxNode> GetSyntaxRootAndFilter(IFormattingRule formattingRule,
157157
return Task.FromResult<SyntaxNode>(null);
158158
}
159159

160-
return GetSyntaxRootAndFilter(formattingRule, document, cancellationToken);
160+
return GetSyntaxRootAndFilter(document, cancellationToken);
161161
}
162162

163163
private void StartDocument()

src/Microsoft.DotNet.CodeFormatting/Microsoft.DotNet.CodeFormatting.csproj

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@
4848
<Reference Include="Microsoft.CodeAnalysis.Desktop">
4949
<HintPath>..\packages\Microsoft.CodeAnalysis.Common.1.0.0-beta1-20141031-01\lib\net45\Microsoft.CodeAnalysis.Desktop.dll</HintPath>
5050
</Reference>
51+
<Reference Include="Microsoft.CodeAnalysis.VisualBasic">
52+
<HintPath>..\packages\Microsoft.CodeAnalysis.VisualBasic.1.0.0-beta1-20141031-01\lib\net45\Microsoft.CodeAnalysis.VisualBasic.dll</HintPath>
53+
</Reference>
54+
<Reference Include="Microsoft.CodeAnalysis.VisualBasic.Desktop">
55+
<HintPath>..\packages\Microsoft.CodeAnalysis.VisualBasic.1.0.0-beta1-20141031-01\lib\net45\Microsoft.CodeAnalysis.VisualBasic.Desktop.dll</HintPath>
56+
</Reference>
5157
<Reference Include="Microsoft.CodeAnalysis.Workspaces">
5258
<HintPath>..\packages\Microsoft.CodeAnalysis.Workspaces.Common.1.0.0-beta1-20141031-01\lib\net45\Microsoft.CodeAnalysis.Workspaces.dll</HintPath>
5359
</Reference>
@@ -86,10 +92,13 @@
8692
<Compile Include="RuleOrderAttribute.cs" />
8793
<Compile Include="Properties\AssemblyInfo.cs" />
8894
<Compile Include="IOrderMetadata.cs" />
95+
<Compile Include="Rules\SyntaxFormattingRule.cs" />
8996
<Compile Include="Rules\BraceNewLineRule.cs" />
97+
<Compile Include="Rules\CopyrightHeaderRule.VisualBasic.cs" />
98+
<Compile Include="Rules\CopyrightHeaderRule.CSharp.cs" />
9099
<Compile Include="Rules\CSharpOnlyFormattingRule.cs" />
91100
<Compile Include="Rules\ExplicitVisibilityRule.cs" />
92-
<Compile Include="Rules\HasCopyrightHeaderFormattingRule.cs" />
101+
<Compile Include="Rules\CopyrightHeaderRule.cs" />
93102
<Compile Include="Rules\HasNewLineBeforeFirstNamespaceFormattingRule.cs" />
94103
<Compile Include="Rules\HasNewLineBeforeFirstUsingFormattingRule.cs" />
95104
<Compile Include="Rules\HasNoIllegalHeadersFormattingRule.cs" />
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.ComponentModel.Composition;
7+
using System.Linq;
8+
using System.Threading;
9+
using System.Threading.Tasks;
10+
11+
using Microsoft.CodeAnalysis;
12+
using Microsoft.CodeAnalysis.CSharp;
13+
14+
namespace Microsoft.DotNet.CodeFormatting.Rules
15+
{
16+
internal partial class CopyrightHeaderRule
17+
{
18+
private sealed class CSharpRule
19+
{
20+
private readonly Options _options;
21+
22+
internal CSharpRule(Options options)
23+
{
24+
_options = options;
25+
}
26+
27+
internal SyntaxNode Process(SyntaxNode syntaxNode)
28+
{
29+
if (_options.CopyrightHeader.IsDefaultOrEmpty)
30+
{
31+
return syntaxNode;
32+
}
33+
34+
if (HasCopyrightHeader(syntaxNode))
35+
return syntaxNode;
36+
37+
return AddCopyrightHeader(syntaxNode);
38+
}
39+
40+
private bool HasCopyrightHeader(SyntaxNode syntaxNode)
41+
{
42+
var leadingComments = syntaxNode.GetLeadingTrivia().Where(t => t.CSharpKind() == SyntaxKind.SingleLineCommentTrivia).ToArray();
43+
var header = _options.CopyrightHeader;
44+
if (leadingComments.Length < header.Length)
45+
return false;
46+
47+
return leadingComments.Take(header.Length)
48+
.Select(t => GetCommentText(t.ToFullString()))
49+
.SequenceEqual(header.Select(GetCommentText));
50+
}
51+
52+
private SyntaxNode AddCopyrightHeader(SyntaxNode syntaxNode)
53+
{
54+
var newTrivia = GetCopyrightHeader().Concat(syntaxNode.GetLeadingTrivia());
55+
return syntaxNode.WithLeadingTrivia(newTrivia);
56+
}
57+
58+
private IEnumerable<SyntaxTrivia> GetCopyrightHeader()
59+
{
60+
foreach (var headerLine in _options.CopyrightHeader)
61+
{
62+
yield return SyntaxFactory.Comment("// " + GetCommentText(headerLine));
63+
yield return SyntaxFactory.CarriageReturnLineFeed;
64+
}
65+
}
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)