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

Commit d24b9ee

Browse files
committed
Merge pull request #34 from jaredpar/perf-work
Performance Work
2 parents f3864e9 + 98e13fa commit d24b9ee

File tree

42 files changed

+1252
-600
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1252
-600
lines changed

src/CodeFormatter/Program.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,21 @@ 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);
71+
engine.Verbose = true;
7072

7173
string extension = Path.GetExtension(projectOrSolutionPath);
7274
if (StringComparer.OrdinalIgnoreCase.Equals(extension, ".sln"))
7375
{
74-
await workspace.OpenSolutionAsync(projectOrSolutionPath, cancellationToken);
76+
var solution = await workspace.OpenSolutionAsync(projectOrSolutionPath, cancellationToken);
77+
await engine.FormatSolutionAsync(solution, cancellationToken);
7578
}
7679
else
7780
{
78-
await workspace.OpenProjectAsync(projectOrSolutionPath, cancellationToken);
81+
workspace.LoadMetadataForReferencedProjects = true;
82+
var project = await workspace.OpenProjectAsync(projectOrSolutionPath, cancellationToken);
83+
await engine.FormatProjectAsync(project, cancellationToken);
7984
}
80-
81-
var engine = FormattingEngine.Create(ruleTypes, filenames);
82-
await engine.RunAsync(workspace, cancellationToken);
8385
}
8486
}
8587
}

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

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Threading;
99
using System.Threading.Tasks;
1010
using Microsoft.CodeAnalysis;
11+
using Microsoft.CodeAnalysis.Formatting;
1112
using Microsoft.CodeAnalysis.Text;
1213
using Microsoft.DotNet.CodeFormatting;
1314
using Xunit;
@@ -25,13 +26,6 @@ public abstract class CodeFormattingTestBase
2526
private const string VBFileExtension = ".vb";
2627
private const string TestProjectName = "TestProject";
2728

28-
internal abstract IFormattingRule GetFormattingRule();
29-
30-
internal static IFormattingRule GetDefaultVSFormatter()
31-
{
32-
return new Rules.IsFormattedFormattingRule();
33-
}
34-
3529
protected virtual IEnumerable<MetadataReference> GetSolutionMetadataReferences()
3630
{
3731
yield return s_CorlibReference;
@@ -55,34 +49,32 @@ private Solution CreateSolution(string[] sources, string language = LanguageName
5549
var fileName = FileNamePrefix + count + fileExtension;
5650
var documentId = DocumentId.CreateNewId(projectId, fileName);
5751
solution = solution.AddDocument(documentId, fileName, SourceText.From(source));
52+
count++;
5853
}
5954

6055
return solution;
6156
}
6257

63-
private static async Task<Solution> Format(Solution solution, IFormattingRule rule, bool runFormatter)
58+
private async Task<Solution> Format(Solution solution, bool runFormatter)
6459
{
6560
var documentIds = solution.Projects.SelectMany(p => p.DocumentIds);
6661

6762
foreach (var id in documentIds)
6863
{
6964
var document = solution.GetDocument(id);
70-
var newDocument = await RewriteDocumentAsync(document, rule, runFormatter);
71-
solution = newDocument.Project.Solution;
65+
document = await RewriteDocumentAsync(document);
66+
if (runFormatter)
67+
{
68+
document = await Formatter.FormatAsync(document);
69+
}
70+
71+
solution = document.Project.Solution;
7272
}
7373

7474
return solution;
7575
}
7676

77-
private static async Task<Document> RewriteDocumentAsync(Document document, IFormattingRule rule, bool runFormatter)
78-
{
79-
document = await rule.ProcessAsync(document, CancellationToken.None);
80-
if (runFormatter)
81-
return await GetDefaultVSFormatter().ProcessAsync(document, CancellationToken.None);
82-
return document;
83-
}
84-
85-
private static void AssertSolutionEqual(Solution expectedSolution, Solution actualSolution)
77+
private void AssertSolutionEqual(Solution expectedSolution, Solution actualSolution)
8678
{
8779
var expectedDocuments = expectedSolution.Projects.SelectMany(p => p.Documents);
8880
var actualDocuments = actualSolution.Projects.SelectMany(p => p.Documents);
@@ -99,26 +91,67 @@ private static void AssertSolutionEqual(Solution expectedSolution, Solution actu
9991
}
10092
}
10193

102-
private void Verify(string[] sources, string[] expected, IFormattingRule rule, bool runFormatter)
94+
protected abstract Task<Document> RewriteDocumentAsync(Document document);
95+
96+
protected void Verify(string[] sources, string[] expected, bool runFormatter)
10397
{
10498
var inputSolution = CreateSolution(sources);
10599
var expectedSolution = CreateSolution(expected);
106-
var actualSolution = Format(inputSolution, rule, runFormatter).Result;
100+
var actualSolution = Format(inputSolution, runFormatter).Result;
107101

108102
if (actualSolution == null)
109103
Assert.False(true, "Solution is null. Test Failed.");
110104

111105
AssertSolutionEqual(expectedSolution, actualSolution);
112106
}
113107

114-
protected void Verify(string[] source, string[] expected, bool runFormatter)
108+
protected void Verify(string source, string expected, bool runFormatter = true)
115109
{
116-
Verify(source, expected, GetFormattingRule(), runFormatter);
110+
Verify(new string[] { source }, new string[] { expected }, runFormatter);
117111
}
112+
}
118113

119-
protected void Verify(string source, string expected, bool runFormatter = true)
114+
public abstract class SyntaxRuleTestBase : CodeFormattingTestBase
115+
{
116+
internal abstract ISyntaxFormattingRule Rule
120117
{
121-
Verify(new string[] { source }, new string[] { expected }, runFormatter);
118+
get;
119+
}
120+
121+
protected override async Task<Document> RewriteDocumentAsync(Document document)
122+
{
123+
var syntaxRoot = await document.GetSyntaxRootAsync();
124+
syntaxRoot = Rule.Process(syntaxRoot);
125+
return document.WithSyntaxRoot(syntaxRoot);
126+
}
127+
}
128+
129+
public abstract class LocalSemanticRuleTestBase : CodeFormattingTestBase
130+
{
131+
internal abstract ILocalSemanticFormattingRule Rule
132+
{
133+
get;
134+
}
135+
136+
protected override async Task<Document> RewriteDocumentAsync(Document document)
137+
{
138+
var syntaxRoot = await document.GetSyntaxRootAsync();
139+
syntaxRoot = await Rule.ProcessAsync(document, syntaxRoot, CancellationToken.None);
140+
return document.WithSyntaxRoot(syntaxRoot);
141+
}
142+
}
143+
144+
public abstract class GlobalSemanticRuleTestBase : CodeFormattingTestBase
145+
{
146+
internal abstract IGlobalSemanticFormattingRule Rule
147+
{
148+
get;
149+
}
150+
151+
protected override async Task<Document> RewriteDocumentAsync(Document document)
152+
{
153+
var solution = await Rule.ProcessAsync(document, await document.GetSyntaxRootAsync(), CancellationToken.None);
154+
return solution.GetDocument(document.Id);
122155
}
123156
}
124157
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@
105105
</ItemGroup>
106106
<ItemGroup>
107107
<Compile Include="CodeFormattingTestBase.cs" />
108+
<Compile Include="Rules\BracesRuleTests.cs" />
109+
<Compile Include="Rules\CombinationTest.cs" />
108110
<Compile Include="Rules\ExplicitThisRuleTests.cs" />
109111
<Compile Include="Rules\ExplicitVisibilityRuleTests.cs" />
110112
<Compile Include="Rules\HasNewLineBeforeFirstNamespaceFormattingRuleTests.cs" />
111113
<Compile Include="Rules\HasNoIllegalHeadersFormattingRuleTests.cs" />
112114
<Compile Include="Rules\HasNewLineBeforeFirstUsingFormattingRuleTests.cs" />
113-
<Compile Include="Rules\HasNoNewLineAfterOpenBraceFormattingRuleTests.cs" />
114-
<Compile Include="Rules\HasNoNewLineBeforeEndBraceFormattingRuleTests.cs" />
115115
<Compile Include="Rules\PrivateFieldNamingRuleTests.cs" />
116116
<Compile Include="Rules\NonAsciiCharactersAreEscapedInLiteralsRuleTests.cs" />
117117
<Compile Include="Rules\UsesXunitForTestsFormattingRuleTests.cs" />

0 commit comments

Comments
 (0)