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

Commit d9b2c30

Browse files
committed
Passing a project should only use that project
When passing a project file name the formatter was still using the resulting Workspace to drive the formatter. In the case a project is a part of a larger solution loading a project will load the larger solution, not the individual project. This means that every possible document in the solution was processed instead of the project. Changed the formatter to process the project only in that case.
1 parent 9195533 commit d9b2c30

File tree

3 files changed

+48
-12
lines changed

3 files changed

+48
-12
lines changed

src/CodeFormatter/Program.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,19 @@ private static int Main(string[] args)
6767
private static async Task RunAsync(string projectOrSolutionPath, IEnumerable<string> ruleTypes, IEnumerable<string> filenames, CancellationToken cancellationToken)
6868
{
6969
var workspace = MSBuildWorkspace.Create();
70+
var engine = FormattingEngine.Create(ruleTypes, filenames);
7071

7172
string extension = Path.GetExtension(projectOrSolutionPath);
7273
if (StringComparer.OrdinalIgnoreCase.Equals(extension, ".sln"))
7374
{
74-
await workspace.OpenSolutionAsync(projectOrSolutionPath, cancellationToken);
75+
var solution = await workspace.OpenSolutionAsync(projectOrSolutionPath, cancellationToken);
76+
await engine.FormatSolutionAsync(solution, cancellationToken);
7577
}
7678
else
7779
{
78-
await workspace.OpenProjectAsync(projectOrSolutionPath, cancellationToken);
80+
var project = await workspace.OpenProjectAsync(projectOrSolutionPath, cancellationToken);
81+
await engine.FormatProjectAsync(project, cancellationToken);
7982
}
80-
81-
var engine = FormattingEngine.Create(ruleTypes, filenames);
82-
await engine.RunAsync(workspace, cancellationToken);
8383
}
8484
}
8585
}

src/Microsoft.DotNet.CodeFormatting/FormattingEngineImplementation.cs

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Collections.Immutable;
67
using System.ComponentModel.Composition;
8+
using System.Diagnostics;
79
using System.IO;
810
using System.Linq;
911
using System.Text;
@@ -21,7 +23,6 @@ internal sealed class FormattingEngineImplementation : IFormattingEngine
2123
private readonly IEnumerable<IFormattingFilter> _filters;
2224
private readonly IEnumerable<IFormattingRule> _rules;
2325

24-
2526
[ImportingConstructor]
2627
public FormattingEngineImplementation([ImportMany] IEnumerable<IFormattingFilter> filters,
2728
[ImportMany] IEnumerable<Lazy<IFormattingRule, IOrderMetadata>> rules)
@@ -30,22 +31,44 @@ public FormattingEngineImplementation([ImportMany] IEnumerable<IFormattingFilter
3031
_rules = rules.OrderBy(r => r.Metadata.Order).Select(r => r.Value);
3132
}
3233

33-
public async Task<bool> RunAsync(Workspace workspace, CancellationToken cancellationToken)
34+
public Task<bool> FormatSolutionAsync(Solution solution, CancellationToken cancellationToken)
35+
{
36+
var documentIds = solution.Projects.SelectMany(x => x.DocumentIds).ToList();
37+
return FormatAsync(solution.Workspace, documentIds, cancellationToken);
38+
}
39+
40+
public Task<bool> FormatProjectAsync(Project project, CancellationToken cancellationToken)
41+
{
42+
return FormatAsync(project.Solution.Workspace, project.DocumentIds, cancellationToken);
43+
}
44+
45+
private async Task<bool> FormatAsync(Workspace workspace, IReadOnlyList<DocumentId> documentIds, CancellationToken cancellationToken)
3446
{
3547
var solution = workspace.CurrentSolution;
36-
var documentIds = solution.Projects.SelectMany(p => p.DocumentIds);
3748
var hasChanges = false;
49+
var longRuleList = new List<Tuple<string, TimeSpan>>();
3850

3951
foreach (var id in documentIds)
4052
{
4153
var document = solution.GetDocument(id);
4254
var shouldBeProcessed = await ShouldBeProcessedAsync(document);
4355
if (!shouldBeProcessed)
56+
{
4457
continue;
58+
}
4559

46-
Console.WriteLine("Processing document: " + document.Name);
47-
var newDocument = await RewriteDocumentAsync(document, cancellationToken);
60+
longRuleList.Clear();
61+
var watch = new Stopwatch();
62+
watch.Start();
63+
Console.Write("Processing document: " + document.Name);
64+
var newDocument = await RewriteDocumentAsync(document, longRuleList, cancellationToken);
4865
hasChanges |= newDocument != document;
66+
watch.Stop();
67+
Console.WriteLine(" {0} seconds", watch.Elapsed.TotalSeconds);
68+
foreach (var tuple in longRuleList)
69+
{
70+
Console.WriteLine("\t{0} {1} seconds", tuple.Item1, tuple.Item2.TotalSeconds);
71+
}
4972

5073
solution = newDocument.Project.Solution;
5174
}
@@ -70,12 +93,24 @@ private async Task<bool> ShouldBeProcessedAsync(Document document)
7093
return true;
7194
}
7295

73-
private async Task<Document> RewriteDocumentAsync(Document document, CancellationToken cancellationToken)
96+
private async Task<Document> RewriteDocumentAsync(Document document, List<Tuple<string, TimeSpan>> longRuleList, CancellationToken cancellationToken)
7497
{
7598
var docText = await document.GetTextAsync();
7699
var originalEncoding = docText.Encoding;
100+
var watch = new Stopwatch();
77101
foreach (var rule in _rules)
102+
{
103+
watch.Start();
78104
document = await rule.ProcessAsync(document, cancellationToken);
105+
watch.Stop();
106+
var timeSpan = watch.Elapsed;
107+
if (timeSpan.TotalSeconds > 1.0)
108+
{
109+
longRuleList.Add(Tuple.Create(rule.GetType().Name, timeSpan));
110+
}
111+
112+
watch.Reset();
113+
}
79114

80115
return await ChangeEncoding(document, originalEncoding);
81116
}

src/Microsoft.DotNet.CodeFormatting/IFormattingEngine.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace Microsoft.DotNet.CodeFormatting
1111
{
1212
public interface IFormattingEngine
1313
{
14-
Task<bool> RunAsync(Workspace workspace, CancellationToken cancellationToken);
14+
Task<bool> FormatSolutionAsync(Solution solution, CancellationToken cancellationToken);
15+
Task<bool> FormatProjectAsync(Project porject, CancellationToken cancellationToken);
1516
}
1617
}

0 commit comments

Comments
 (0)