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

Commit d35b4d3

Browse files
committed
Convert to new rule format
1 parent 8b09def commit d35b4d3

14 files changed

+54
-103
lines changed

src/Microsoft.DotNet.CodeFormatting/FormattingEngineImplementation.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,19 @@ namespace Microsoft.DotNet.CodeFormatting
2121
internal sealed class FormattingEngineImplementation : IFormattingEngine
2222
{
2323
private readonly IEnumerable<IFormattingFilter> _filters;
24-
private readonly IEnumerable<IFormattingRule> _rules;
2524
private readonly IEnumerable<ISyntaxFormattingRule> _syntaxRules;
2625
private readonly IEnumerable<ILocalSemanticFormattingRule> _localSemanticRules;
2726
private readonly IEnumerable<IGlobalSemanticFormattingRule> _globalSemanticRules;
28-
private readonly bool _verbose;
2927
private readonly Stopwatch _watch = new Stopwatch();
3028

3129
[ImportingConstructor]
3230
public FormattingEngineImplementation(
3331
[ImportMany] IEnumerable<IFormattingFilter> filters,
34-
[ImportMany] IEnumerable<Lazy<IFormattingRule, IOrderMetadata>> rules,
3532
[ImportMany] IEnumerable<Lazy<ISyntaxFormattingRule, IOrderMetadata>> syntaxRules,
3633
[ImportMany] IEnumerable<Lazy<ILocalSemanticFormattingRule, IOrderMetadata>> localSemanticRules,
3734
[ImportMany] IEnumerable<Lazy<IGlobalSemanticFormattingRule, IOrderMetadata>> globalSemanticRules)
3835
{
3936
_filters = filters;
40-
_rules = rules.OrderBy(r => r.Metadata.Order).Select(r => r.Value);
4137
_syntaxRules = syntaxRules.OrderBy(r => r.Metadata.Order).Select(r => r.Value);
4238
_localSemanticRules = localSemanticRules.OrderBy(r => r.Metadata.Order).Select(r => r.Value);
4339
_globalSemanticRules = globalSemanticRules.OrderBy(r => r.Metadata.Order).Select(r => r.Value);
@@ -115,7 +111,7 @@ private void StartDocument(Document document, int depth = 1)
115111
private void EndDocument()
116112
{
117113
_watch.Stop();
118-
if (_verbose && _watch.Elapsed.TotalSeconds > 1)
114+
if (_watch.Elapsed.TotalSeconds > 1)
119115
{
120116
Console.WriteLine(" {0} seconds", _watch.Elapsed.TotalSeconds);
121117
}
@@ -227,13 +223,8 @@ private async Task<Solution> RunGlobalSemanticPass(Solution solution, IReadOnlyL
227223
}
228224

229225
StartDocument(document, depth: 2);
230-
var newRoot = await globalSemanticRule.ProcessAsync(document, syntaxRoot, cancellationToken);
226+
solution = await globalSemanticRule.ProcessAsync(document, syntaxRoot, cancellationToken);
231227
EndDocument();
232-
233-
if (syntaxRoot != newRoot)
234-
{
235-
solution = solution.WithDocumentSyntaxRoot(documentId, newRoot);
236-
}
237228
}
238229

239230
return solution;

src/Microsoft.DotNet.CodeFormatting/IFormattingRule.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@ internal interface IFormattingRule
1616
}
1717

1818
/// <summary>
19-
/// Rules which need no semantic information and operate on parse trees only
19+
/// Rules which need no semantic information and operate on parse trees only.
2020
/// </summary>
2121
internal interface ISyntaxFormattingRule
2222
{
2323
SyntaxNode Process(SyntaxNode syntaxRoot);
2424
}
2525

2626
/// <summary>
27-
/// Rules which possibly need semantic information but only operate on a
28-
/// specific document.
27+
/// Rules which possibly need semantic information but only operate on a specific document. Also
28+
/// used for rules that need to see a <see cref="Document"/> and <see cref="SyntaxNode"/> which
29+
/// are in sync with each other,
2930
/// </summary>
3031
internal interface ILocalSemanticFormattingRule
3132
{
@@ -37,6 +38,6 @@ internal interface ILocalSemanticFormattingRule
3738
/// </summary>
3839
internal interface IGlobalSemanticFormattingRule
3940
{
40-
Task<SyntaxNode> ProcessAsync(Document document, SyntaxNode syntaxRoot, CancellationToken cancellationToken);
41+
Task<Solution> ProcessAsync(Document document, SyntaxNode syntaxRoot, CancellationToken cancellationToken);
4142
}
4243
}

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,13 @@
1313
namespace Microsoft.DotNet.CodeFormatting.Rules
1414
{
1515
[RuleOrder(RuleOrder.BraceNewLineRule)]
16-
internal sealed class BraceNewLineRule : IFormattingRule
16+
internal sealed class BraceNewLineRule : ISyntaxFormattingRule
1717
{
18-
public async Task<Document> ProcessAsync(Document document, CancellationToken cancellationToken)
18+
public SyntaxNode Process(SyntaxNode syntaxNode)
1919
{
20-
var syntaxNode = await document.GetSyntaxRootAsync(cancellationToken);
21-
if (syntaxNode == null)
22-
{
23-
return document;
24-
}
25-
2620
syntaxNode = FixOpenBraces(syntaxNode);
2721
syntaxNode = FixCloseBraces(syntaxNode);
28-
return document.WithSyntaxRoot(syntaxNode);
22+
return syntaxNode;
2923
}
3024

3125
private static SyntaxNode FixOpenBraces(SyntaxNode syntaxNode)

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
namespace Microsoft.DotNet.CodeFormatting.Rules
1616
{
1717
[RuleOrder(RuleOrder.ExplicitVisibilityRule)]
18-
internal sealed class ExplicitVisibilityRule : IFormattingRule
18+
internal sealed class ExplicitVisibilityRule : ILocalSemanticFormattingRule
1919
{
2020
private sealed class VisibilityRewriter : CSharpSyntaxRewriter
2121
{
@@ -235,17 +235,11 @@ private static MemberDeclarationSyntax EnsureVisibility<T>(T node, SyntaxTokenLi
235235
}
236236
}
237237

238-
public async Task<Document> ProcessAsync(Document document, CancellationToken cancellationToken)
238+
public Task<SyntaxNode> ProcessAsync(Document document, SyntaxNode syntaxNode, CancellationToken cancellationToken)
239239
{
240-
var syntaxNode = await document.GetSyntaxRootAsync(cancellationToken) as CSharpSyntaxNode;
241-
if (syntaxNode == null)
242-
{
243-
return document;
244-
}
245-
246240
var rewriter = new VisibilityRewriter(document, cancellationToken);
247241
var newNode = rewriter.Visit(syntaxNode);
248-
return document.WithSyntaxRoot(newNode);
242+
return Task.FromResult(newNode);
249243
}
250244
}
251245
}

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,20 @@
1414
namespace Microsoft.DotNet.CodeFormatting.Rules
1515
{
1616
[RuleOrder(RuleOrder.HasCopyrightHeaderFormattingRule)]
17-
internal sealed class HasCopyrightHeaderFormattingRule : IFormattingRule
17+
internal sealed class HasCopyrightHeaderFormattingRule : ISyntaxFormattingRule
1818
{
1919
private static readonly string[] s_copyrightHeader =
2020
{
2121
"// Copyright (c) Microsoft. All rights reserved.",
2222
"// Licensed under the MIT license. See LICENSE file in the project root for full license information."
2323
};
2424

25-
public async Task<Document> ProcessAsync(Document document, CancellationToken cancellationToken)
25+
public SyntaxNode Process(SyntaxNode syntaxNode)
2626
{
27-
var syntaxNode = await document.GetSyntaxRootAsync(cancellationToken) as CSharpSyntaxNode;
28-
if (syntaxNode == null)
29-
return document;
30-
3127
if (HasCopyrightHeader(syntaxNode))
32-
return document;
28+
return syntaxNode;
3329

34-
var newNode = AddCopyrightHeader(syntaxNode);
35-
return document.WithSyntaxRoot(newNode);
30+
return AddCopyrightHeader(syntaxNode);
3631
}
3732

3833
private static bool HasCopyrightHeader(SyntaxNode syntaxNode)
@@ -46,7 +41,7 @@ private static bool HasCopyrightHeader(SyntaxNode syntaxNode)
4641
.SequenceEqual(s_copyrightHeader);
4742
}
4843

49-
private static SyntaxNode AddCopyrightHeader(CSharpSyntaxNode syntaxNode)
44+
private static SyntaxNode AddCopyrightHeader(SyntaxNode syntaxNode)
5045
{
5146
var newTrivia = GetCopyrightHeader().Concat(syntaxNode.GetLeadingTrivia());
5247
return syntaxNode.WithLeadingTrivia(newTrivia);

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,15 @@
1515
namespace Microsoft.DotNet.CodeFormatting.Rules
1616
{
1717
[RuleOrder(RuleOrder.HasNewLineBeforeFirstNamespaceFormattingRule)]
18-
internal sealed class HasNewLineBeforeFirstNamespaceFormattingRule : IFormattingRule
18+
internal sealed class HasNewLineBeforeFirstNamespaceFormattingRule : ISyntaxFormattingRule
1919
{
20-
public async Task<Document> ProcessAsync(Document document, CancellationToken cancellationToken)
20+
public SyntaxNode Process(SyntaxNode syntaxRoot)
2121
{
22-
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken) as CSharpSyntaxNode;
23-
if (syntaxRoot == null)
24-
return document;
25-
2622
var firstNamespace = syntaxRoot.DescendantNodesAndSelf().OfType<NamespaceDeclarationSyntax>().FirstOrDefault();
2723
IEnumerable<SyntaxTrivia> newTrivia = Enumerable.Empty<SyntaxTrivia>();
2824

2925
if (firstNamespace == null)
30-
return document;
26+
return syntaxRoot;
3127

3228
if (firstNamespace.HasLeadingTrivia)
3329
{
@@ -52,7 +48,7 @@ public async Task<Document> ProcessAsync(Document document, CancellationToken ca
5248
newTrivia = newTrivia.AddNewLine();
5349
}
5450

55-
return document.WithSyntaxRoot(syntaxRoot.ReplaceNode(firstNamespace, firstNamespace.WithLeadingTrivia(newTrivia)));
51+
return syntaxRoot.ReplaceNode(firstNamespace, firstNamespace.WithLeadingTrivia(newTrivia));
5652
}
5753

5854
private IEnumerable<SyntaxTrivia> GetLeadingTriviaWithEndNewLines(IEnumerable<SyntaxTrivia> trivia)

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,15 @@
1616
namespace Microsoft.DotNet.CodeFormatting.Rules
1717
{
1818
[RuleOrder(RuleOrder.HasNewLineBeforeFirstUsingFormattingRule)]
19-
internal sealed class HasNewLineBeforeFirstUsingFormattingRule : IFormattingRule
19+
internal sealed class HasNewLineBeforeFirstUsingFormattingRule : ISyntaxFormattingRule
2020
{
2121
private const string FormatError = "Could not format using";
2222

23-
public async Task<Document> ProcessAsync(Document document, CancellationToken cancellationToken)
23+
public SyntaxNode Process(SyntaxNode syntaxRoot)
2424
{
25-
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken) as CSharpSyntaxNode;
26-
if (syntaxRoot == null)
27-
return document;
28-
2925
var firstUsing = syntaxRoot.DescendantNodesAndSelf().OfType<UsingDirectiveSyntax>().FirstOrDefault();
3026
if (firstUsing == null)
31-
return document;
27+
return syntaxRoot;
3228

3329
IEnumerable<SyntaxTrivia> newTrivia = Enumerable.Empty<SyntaxTrivia>();
3430

@@ -39,9 +35,8 @@ public async Task<Document> ProcessAsync(Document document, CancellationToken ca
3935
var disabledUsingSpan = trivia.FirstOrDefault(t => t.CSharpKind() == SyntaxKind.DisabledTextTrivia && t.ToFullString().Contains("using")).Span;
4036
if (!disabledUsingSpan.IsEmpty)
4137
{
42-
var line = syntaxRoot.SyntaxTree.GetLineSpan(disabledUsingSpan, cancellationToken).StartLinePosition.Line + 1;
43-
FormatError.WriteConsoleError(line, document.Name);
44-
return document;
38+
Console.WriteLine("!!!Error with using");
39+
return syntaxRoot;
4540
}
4641

4742
if (SyntaxKind.EndOfLineTrivia == trivia.Last().CSharpKind())
@@ -59,7 +54,7 @@ public async Task<Document> ProcessAsync(Document document, CancellationToken ca
5954
}
6055
}
6156

62-
return document.WithSyntaxRoot(syntaxRoot.ReplaceNode(firstUsing, firstUsing.WithLeadingTrivia(newTrivia)));
57+
return syntaxRoot.ReplaceNode(firstUsing, firstUsing.WithLeadingTrivia(newTrivia));
6358
}
6459

6560
private IEnumerable<SyntaxTrivia> GetLeadingTriviaWithEndNewLines(IEnumerable<SyntaxTrivia> trivia)

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
namespace Microsoft.DotNet.CodeFormatting.Rules
1717
{
1818
[RuleOrder(RuleOrder.HasNoCustomCopyrightHeaderFormattingRule)]
19-
internal sealed class HasNoCustomCopyrightHeaderFormattingRule : IFormattingRule
19+
internal sealed class HasNoCustomCopyrightHeaderFormattingRule : ISyntaxFormattingRule
2020
{
2121
private static string RulerMarker { get; set; }
2222
private static string StartMarker { get; set; }
@@ -26,26 +26,21 @@ internal sealed class HasNoCustomCopyrightHeaderFormattingRule : IFormattingRule
2626

2727
private const string FileSyntaxError = "There should be exactly 3 lines in CopyrightHeader.md.";
2828

29-
public async Task<Document> ProcessAsync(Document document, CancellationToken cancellationToken)
29+
public SyntaxNode Process(SyntaxNode syntaxNode)
3030
{
31-
var syntaxNode = await document.GetSyntaxRootAsync(cancellationToken) as CSharpSyntaxNode;
32-
if (syntaxNode == null)
33-
return document;
34-
3531
// SetHeaders
3632
if (!SetHeaders())
37-
return document;
33+
return syntaxNode;
3834

3935
var triviaList = syntaxNode.GetLeadingTrivia();
4036

4137
SyntaxTrivia start;
4238
SyntaxTrivia end;
4339
if (!TryGetStartAndEndOfXmlHeader(triviaList, out start, out end))
44-
return document;
40+
return syntaxNode;
4541

4642
var filteredList = Filter(triviaList, start, end);
47-
var newSyntaxNode = syntaxNode.WithLeadingTrivia(filteredList);
48-
return document.WithSyntaxRoot(newSyntaxNode);
43+
return syntaxNode.WithLeadingTrivia(filteredList);
4944
}
5045

5146
private static IEnumerable<SyntaxTrivia> Filter(SyntaxTriviaList triviaList, SyntaxTrivia start, SyntaxTrivia end)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
namespace Microsoft.DotNet.CodeFormatting.Rules
1717
{
1818
[RuleOrder(RuleOrder.HasNoIllegalHeadersFormattingRule)]
19-
internal sealed class HasNoIllegalHeadersFormattingRule : IFormattingRule
19+
internal sealed class HasNoIllegalHeadersFormattingRule
2020
{
2121
// We are going to replace this header with the actual filename of the document being processed
2222
private const string FileNameIllegalHeader = "<<<filename>>>";

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,15 @@
1414
namespace Microsoft.DotNet.CodeFormatting.Rules
1515
{
1616
[RuleOrder(RuleOrder.HasUsingsOutsideOfNamespaceFormattingRule)]
17-
internal sealed class HasUsingsOutsideOfNamespaceFormattingRule : IFormattingRule
17+
internal sealed class HasUsingsOutsideOfNamespaceFormattingRule : ISyntaxFormattingRule
1818
{
19-
public async Task<Document> ProcessAsync(Document document, CancellationToken cancellationToken)
19+
public SyntaxNode Process(SyntaxNode syntaxNode)
2020
{
21-
var root = await document.GetSyntaxRootAsync(cancellationToken) as CompilationUnitSyntax;
22-
21+
var root = syntaxNode as CompilationUnitSyntax;
2322
if (root == null)
24-
return document;
23+
return syntaxNode;
2524

2625
var newRoot = root;
27-
2826
while (true)
2927
{
3028
var namespaceWithUsings = newRoot.Members.OfType<NamespaceDeclarationSyntax>().FirstOrDefault(n => n.Usings.Any());
@@ -34,7 +32,7 @@ public async Task<Document> ProcessAsync(Document document, CancellationToken ca
3432
// Moving a using with an alias out of a namespace is an operation which requires
3533
// semantic knowledge to get correct.
3634
if (namespaceWithUsings.Usings.Any(x => x.Alias != null))
37-
return document;
35+
return syntaxNode;
3836

3937
// Remove nested usings
4038

@@ -67,7 +65,7 @@ public async Task<Document> ProcessAsync(Document document, CancellationToken ca
6765
newRoot = newRoot.AddUsings(usings);
6866
}
6967

70-
return document.WithSyntaxRoot(newRoot);
68+
return newRoot;
7169
}
7270
}
7371
}

0 commit comments

Comments
 (0)