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

Commit 32a15e1

Browse files
committed
More changes
1 parent d35b4d3 commit 32a15e1

File tree

4 files changed

+17
-28
lines changed

4 files changed

+17
-28
lines changed

src/Microsoft.DotNet.CodeFormatting/FormattingEngineImplementation.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ internal sealed class FormattingEngineImplementation : IFormattingEngine
2929
[ImportingConstructor]
3030
public FormattingEngineImplementation(
3131
[ImportMany] IEnumerable<IFormattingFilter> filters,
32-
[ImportMany] IEnumerable<Lazy<ISyntaxFormattingRule, IOrderMetadata>> syntaxRules,
33-
[ImportMany] IEnumerable<Lazy<ILocalSemanticFormattingRule, IOrderMetadata>> localSemanticRules,
34-
[ImportMany] IEnumerable<Lazy<IGlobalSemanticFormattingRule, IOrderMetadata>> globalSemanticRules)
32+
[ImportMany] IEnumerable<Lazy<IFormattingRule, IOrderMetadata>> allRules)
3533
{
3634
_filters = filters;
37-
_syntaxRules = syntaxRules.OrderBy(r => r.Metadata.Order).Select(r => r.Value);
38-
_localSemanticRules = localSemanticRules.OrderBy(r => r.Metadata.Order).Select(r => r.Value);
39-
_globalSemanticRules = globalSemanticRules.OrderBy(r => r.Metadata.Order).Select(r => r.Value);
35+
var rulesSorted = allRules.OrderBy(r => r.Metadata.Order).Select(r => r.Value).ToList();
36+
37+
_syntaxRules = rulesSorted.OfType<ISyntaxFormattingRule>().ToList();
38+
_localSemanticRules = rulesSorted.OfType<ILocalSemanticFormattingRule>().ToList();
39+
_globalSemanticRules = rulesSorted.OfType<IGlobalSemanticFormattingRule>().ToList();
4040
}
4141

4242
public Task FormatSolutionAsync(Solution solution, CancellationToken cancellationToken)

src/Microsoft.DotNet.CodeFormatting/IFormattingRule.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99

1010
namespace Microsoft.DotNet.CodeFormatting
1111
{
12-
// TODO: delete
12+
// TODO: this is a hack. Need to delete it
1313
internal interface IFormattingRule
1414
{
15-
Task<Document> ProcessAsync(Document document, CancellationToken cancellationToken);
15+
1616
}
1717

1818
/// <summary>

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Microsoft.DotNet.CodeFormatting.Rules
1313
{
1414
[RuleOrder(RuleOrder.RemoveExplicitThisRule)]
15-
public sealed class ExplicitThisRule : IFormattingRule
15+
public sealed class ExplicitThisRule : ILocalSemanticFormattingRule
1616
{
1717
private sealed class ExplicitThisRewriter : CSharpSyntaxRewriter
1818
{
@@ -66,22 +66,17 @@ private bool IsPrivateField(MemberAccessExpressionSyntax memberSyntax)
6666
}
6767
}
6868

69-
public async Task<Document> ProcessAsync(Document document, CancellationToken cancellationToken)
69+
public async Task<SyntaxNode> ProcessAsync(Document document, SyntaxNode syntaxNode, CancellationToken cancellationToken)
7070
{
71-
var syntaxNode = await document.GetSyntaxRootAsync(cancellationToken) as CSharpSyntaxNode;
72-
if (syntaxNode == null)
73-
{
74-
return document;
75-
}
76-
7771
var rewriter = new ExplicitThisRewriter(document, cancellationToken);
7872
var newNode = rewriter.Visit(syntaxNode);
7973
if (!rewriter.AddedAnnotations)
8074
{
81-
return document;
75+
return syntaxNode;
8276
}
8377

84-
return await Simplifier.ReduceAsync(document.WithSyntaxRoot(newNode), cancellationToken: cancellationToken);
78+
document = await Simplifier.ReduceAsync(document.WithSyntaxRoot(newNode), cancellationToken: cancellationToken);
79+
return await document.GetSyntaxRootAsync(cancellationToken);
8580
}
8681
}
8782
}

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
namespace Microsoft.DotNet.CodeFormatting.Rules
1919
{
2020
[RuleOrder(RuleOrder.PrivateFieldNamingRule)]
21-
internal sealed class PrivateFieldNamingRule : IFormattingRule
21+
internal sealed class PrivateFieldNamingRule : IGlobalSemanticFormattingRule
2222
{
2323
/// <summary>
2424
/// This will add an annotation to any private field that needs to be renamed.
@@ -159,27 +159,21 @@ public override SyntaxToken VisitToken(SyntaxToken token)
159159
}
160160
}
161161

162-
public async Task<Document> ProcessAsync(Document document, CancellationToken cancellationToken)
162+
public async Task<Solution> ProcessAsync(Document document, SyntaxNode syntaxRoot, CancellationToken cancellationToken)
163163
{
164-
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken) as CSharpSyntaxNode;
165-
if (syntaxRoot == null)
166-
{
167-
return document;
168-
}
169-
170164
int count;
171165
var newSyntaxRoot = PrivateFieldAnnotationsRewriter.AddAnnotations(syntaxRoot, out count);
172166

173167
if (count == 0)
174168
{
175-
return document;
169+
return document.Project.Solution;
176170
}
177171

178172
var documentId = document.Id;
179173
var solution = document.Project.Solution;
180174
solution = solution.WithDocumentSyntaxRoot(documentId, newSyntaxRoot);
181175
solution = await RenameFields(solution, documentId, count, cancellationToken);
182-
return solution.GetDocument(documentId);
176+
return solution;
183177
}
184178

185179
private static async Task<Solution> RenameFields(Solution solution, DocumentId documentId, int count, CancellationToken cancellationToken)

0 commit comments

Comments
 (0)