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

Commit 0020995

Browse files
committed
Allow copyright header customization
This change adds internal support for customizing the copyright header the formatter adds. This still needs to get an official command line option but that can be added later. The CombinationTests suite now passes again as well. When the test was first written the copyright rule was disabled as Roslyn used a different copyright. When I re-enabled that rule I forgot to run the test and hence it ended up in a broken state.
1 parent 6f9bbb3 commit 0020995

File tree

6 files changed

+69
-12
lines changed

6 files changed

+69
-12
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Threading.Tasks;
77
using Microsoft.CodeAnalysis;
88
using Xunit;
9+
using System.Collections.Immutable;
910

1011
namespace Microsoft.DotNet.CodeFormatting.Tests
1112
{
@@ -21,6 +22,11 @@ static CombinationTest()
2122
s_formattingEngine = (FormattingEngineImplementation)FormattingEngine.Create(Enumerable.Empty<string>(), Enumerable.Empty<string>());
2223
}
2324

25+
public CombinationTest()
26+
{
27+
s_formattingEngine.CopyrightHeader = ImmutableArray.Create("", "// header");
28+
}
29+
2430
protected override async Task<Document> RewriteDocumentAsync(Document document)
2531
{
2632
var solution = await s_formattingEngine.FormatCoreAsync(
@@ -43,6 +49,8 @@ void M() {
4349
}";
4450

4551
var expected = @"
52+
// header
53+
4654
internal class C
4755
{
4856
private int _field;
@@ -59,6 +67,7 @@ private void M()
5967
[Fact]
6068
public void FieldAssignment()
6169
{
70+
6271
var text = @"
6372
class C {
6473
int field;
@@ -69,6 +78,8 @@ void M() {
6978
}";
7079

7180
var expected = @"
81+
// header
82+
7283
internal class C
7384
{
7485
private int _field;

src/Microsoft.DotNet.CodeFormatting/FormattingEngineImplementation.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,35 @@ namespace Microsoft.DotNet.CodeFormatting
2020
[Export(typeof(IFormattingEngine))]
2121
internal sealed class FormattingEngineImplementation : IFormattingEngine
2222
{
23+
private readonly Options _options;
2324
private readonly IEnumerable<IFormattingFilter> _filters;
2425
private readonly IEnumerable<ISyntaxFormattingRule> _syntaxRules;
2526
private readonly IEnumerable<ILocalSemanticFormattingRule> _localSemanticRules;
2627
private readonly IEnumerable<IGlobalSemanticFormattingRule> _globalSemanticRules;
2728
private readonly Stopwatch _watch = new Stopwatch();
2829
private bool _verbose;
2930

31+
public ImmutableArray<string> CopyrightHeader
32+
{
33+
get { return _options.CopyrightHeader; }
34+
set { _options.CopyrightHeader = value; }
35+
}
36+
3037
public bool Verbose
3138
{
3239
get { return _verbose; }
3340
set { _verbose = value; }
3441
}
3542

3643
[ImportingConstructor]
37-
public FormattingEngineImplementation(
44+
internal FormattingEngineImplementation(
45+
Options options,
3846
[ImportMany] IEnumerable<IFormattingFilter> filters,
3947
[ImportMany] IEnumerable<Lazy<ISyntaxFormattingRule, IOrderMetadata>> syntaxRules,
4048
[ImportMany] IEnumerable<Lazy<ILocalSemanticFormattingRule, IOrderMetadata>> localSemanticRules,
4149
[ImportMany] IEnumerable<Lazy<IGlobalSemanticFormattingRule, IOrderMetadata>> globalSemanticRules)
4250
{
51+
_options = options;
4352
_filters = filters;
4453
_syntaxRules = syntaxRules.OrderBy(r => r.Metadata.Order).Select(r => r.Value).ToList();
4554
_localSemanticRules = localSemanticRules.OrderBy(r => r.Metadata.Order).Select(r => r.Value).ToList();

src/Microsoft.DotNet.CodeFormatting/IFormattingEngine.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
using System.Threading.Tasks;
77

88
using Microsoft.CodeAnalysis;
9+
using System.Collections.Immutable;
910

1011
namespace Microsoft.DotNet.CodeFormatting
1112
{
1213
public interface IFormattingEngine
1314
{
15+
ImmutableArray<string> CopyrightHeader { get; set; }
1416
bool Verbose { get; set; }
1517
Task FormatSolutionAsync(Solution solution, CancellationToken cancellationToken);
1618
Task FormatProjectAsync(Project porject, CancellationToken cancellationToken);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
<Compile Include="IFormattingFilter.cs" />
7979
<Compile Include="IFormattingRule.cs" />
8080
<Compile Include="Filters\IgnoreDesignerGeneratedCodeFilter.cs" />
81+
<Compile Include="Options.cs" />
8182
<Compile Include="RuleOrderAttribute.cs" />
8283
<Compile Include="Properties\AssemblyInfo.cs" />
8384
<Compile Include="IOrderMetadata.cs" />
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.Immutable;
4+
using System.ComponentModel.Composition;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace Microsoft.DotNet.CodeFormatting
10+
{
11+
/// <summary>
12+
/// This is a MEF importable type which contains all of the options for formatting
13+
/// </summary>
14+
[Export(typeof(Options))]
15+
internal sealed class Options
16+
{
17+
private static readonly string[] s_defaultCopyrightHeader =
18+
{
19+
"// Copyright (c) Microsoft. All rights reserved.",
20+
"// Licensed under the MIT license. See LICENSE file in the project root for full license information."
21+
};
22+
23+
internal ImmutableArray<string> CopyrightHeader { get; set; }
24+
25+
[ImportingConstructor]
26+
internal Options()
27+
{
28+
CopyrightHeader = ImmutableArray.Create(s_defaultCopyrightHeader);
29+
}
30+
}
31+
}

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ namespace Microsoft.DotNet.CodeFormatting.Rules
1616
[SyntaxRuleOrder(SyntaxRuleOrder.HasCopyrightHeaderFormattingRule)]
1717
internal sealed class HasCopyrightHeaderFormattingRule : ISyntaxFormattingRule
1818
{
19-
private static readonly string[] s_copyrightHeader =
19+
private readonly Options _options;
20+
21+
[ImportingConstructor]
22+
internal HasCopyrightHeaderFormattingRule(Options options)
2023
{
21-
"// Copyright (c) Microsoft. All rights reserved.",
22-
"// Licensed under the MIT license. See LICENSE file in the project root for full license information."
23-
};
24+
_options = options;
25+
}
2426

2527
public SyntaxNode Process(SyntaxNode syntaxNode)
2628
{
@@ -30,26 +32,27 @@ public SyntaxNode Process(SyntaxNode syntaxNode)
3032
return AddCopyrightHeader(syntaxNode);
3133
}
3234

33-
private static bool HasCopyrightHeader(SyntaxNode syntaxNode)
35+
private bool HasCopyrightHeader(SyntaxNode syntaxNode)
3436
{
3537
var leadingComments = syntaxNode.GetLeadingTrivia().Where(t => t.CSharpKind() == SyntaxKind.SingleLineCommentTrivia).ToArray();
36-
if (leadingComments.Length < s_copyrightHeader.Length)
38+
var header = _options.CopyrightHeader;
39+
if (leadingComments.Length < header.Length)
3740
return false;
3841

39-
return leadingComments.Take(s_copyrightHeader.Length)
42+
return leadingComments.Take(header.Length)
4043
.Select(t => t.ToFullString())
41-
.SequenceEqual(s_copyrightHeader);
44+
.SequenceEqual(header);
4245
}
4346

44-
private static SyntaxNode AddCopyrightHeader(SyntaxNode syntaxNode)
47+
private SyntaxNode AddCopyrightHeader(SyntaxNode syntaxNode)
4548
{
4649
var newTrivia = GetCopyrightHeader().Concat(syntaxNode.GetLeadingTrivia());
4750
return syntaxNode.WithLeadingTrivia(newTrivia);
4851
}
4952

50-
private static IEnumerable<SyntaxTrivia> GetCopyrightHeader()
53+
private IEnumerable<SyntaxTrivia> GetCopyrightHeader()
5154
{
52-
foreach (var headerLine in s_copyrightHeader)
55+
foreach (var headerLine in _options.CopyrightHeader)
5356
{
5457
yield return SyntaxFactory.Comment(headerLine);
5558
yield return SyntaxFactory.CarriageReturnLineFeed;

0 commit comments

Comments
 (0)