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

Commit 7ba96d7

Browse files
author
Lakshmi Priya Sekar
committed
Add NoNewLineAfterOpenBraceRule and formatting for DisabledTextTrivia
1 parent 4a53025 commit 7ba96d7

File tree

7 files changed

+122
-5
lines changed

7 files changed

+122
-5
lines changed

src/CodeFormatter/CodeFormatter.csproj

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,48 @@
3232
<WarningLevel>4</WarningLevel>
3333
</PropertyGroup>
3434
<ItemGroup>
35-
<Reference Include="Microsoft.CodeAnalysis.Workspaces">
35+
<Reference Include="Microsoft.CodeAnalysis, Version=0.7.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
36+
<SpecificVersion>False</SpecificVersion>
37+
<HintPath>..\packages\Microsoft.CodeAnalysis.Common.0.7.4091001-beta\lib\net45\Microsoft.CodeAnalysis.dll</HintPath>
38+
</Reference>
39+
<Reference Include="Microsoft.CodeAnalysis.CSharp, Version=0.7.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
40+
<SpecificVersion>False</SpecificVersion>
41+
<HintPath>..\packages\Microsoft.CodeAnalysis.CSharp.0.7.4091001-beta\lib\net45\Microsoft.CodeAnalysis.CSharp.dll</HintPath>
42+
</Reference>
43+
<Reference Include="Microsoft.CodeAnalysis.CSharp.Desktop">
44+
<HintPath>..\packages\Microsoft.CodeAnalysis.CSharp.0.7.4091001-beta\lib\net45\Microsoft.CodeAnalysis.CSharp.Desktop.dll</HintPath>
45+
</Reference>
46+
<Reference Include="Microsoft.CodeAnalysis.CSharp.Workspaces">
47+
<HintPath>..\packages\Microsoft.CodeAnalysis.CSharp.Workspaces.0.7.4091001-beta\lib\net45\Microsoft.CodeAnalysis.CSharp.Workspaces.dll</HintPath>
48+
</Reference>
49+
<Reference Include="Microsoft.CodeAnalysis.Desktop, Version=0.7.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
50+
<SpecificVersion>False</SpecificVersion>
51+
<HintPath>..\packages\Microsoft.CodeAnalysis.Common.0.7.4091001-beta\lib\net45\Microsoft.CodeAnalysis.Desktop.dll</HintPath>
52+
</Reference>
53+
<Reference Include="Microsoft.CodeAnalysis.VisualBasic">
54+
<HintPath>..\packages\Microsoft.CodeAnalysis.VisualBasic.0.7.4091001-beta\lib\net45\Microsoft.CodeAnalysis.VisualBasic.dll</HintPath>
55+
</Reference>
56+
<Reference Include="Microsoft.CodeAnalysis.VisualBasic.Desktop">
57+
<HintPath>..\packages\Microsoft.CodeAnalysis.VisualBasic.0.7.4091001-beta\lib\net45\Microsoft.CodeAnalysis.VisualBasic.Desktop.dll</HintPath>
58+
</Reference>
59+
<Reference Include="Microsoft.CodeAnalysis.VisualBasic.Workspaces">
60+
<HintPath>..\packages\Microsoft.CodeAnalysis.VisualBasic.Workspaces.0.7.4091001-beta\lib\net45\Microsoft.CodeAnalysis.VisualBasic.Workspaces.dll</HintPath>
61+
</Reference>
62+
<Reference Include="Microsoft.CodeAnalysis.Workspaces, Version=0.7.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
63+
<SpecificVersion>False</SpecificVersion>
3664
<HintPath>..\packages\Microsoft.CodeAnalysis.Workspaces.Common.0.7.4091001-beta\lib\net45\Microsoft.CodeAnalysis.Workspaces.dll</HintPath>
3765
</Reference>
3866
<Reference Include="System" />
67+
<Reference Include="System.Collections.Immutable, Version=1.1.20.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
68+
<Private>True</Private>
69+
<HintPath>..\packages\Microsoft.Bcl.Immutable.1.1.20-beta\lib\portable-net45+win8\System.Collections.Immutable.dll</HintPath>
70+
</Reference>
3971
<Reference Include="System.ComponentModel.Composition" />
4072
<Reference Include="System.Core" />
73+
<Reference Include="System.Reflection.Metadata, Version=1.0.12.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
74+
<SpecificVersion>False</SpecificVersion>
75+
<HintPath>..\packages\Microsoft.Bcl.Metadata.1.0.12-alpha\lib\portable-net45+win8\System.Reflection.Metadata.dll</HintPath>
76+
</Reference>
4177
<Reference Include="System.Xml.Linq" />
4278
<Reference Include="System.Data.DataSetExtensions" />
4379
<Reference Include="Microsoft.CSharp" />
@@ -50,6 +86,7 @@
5086
</ItemGroup>
5187
<ItemGroup>
5288
<None Include="App.config" />
89+
<None Include="packages.config" />
5390
</ItemGroup>
5491
<ItemGroup>
5592
<ProjectReference Include="..\Microsoft.DotNet.CodeFormatting\Microsoft.DotNet.CodeFormatting.csproj">

src/CodeFormatter/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ private static int Main(string[] args)
3232
Console.CancelKeyPress += delegate { cts.Cancel(); };
3333

3434
RunAsnc(solutionPath, ct).Wait(ct);
35+
Console.WriteLine("Completed formatting.");
3536
return 0;
3637
}
3738

src/CodeFormatter/packages.config

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="Microsoft.Bcl.Immutable" version="1.1.20-beta" targetFramework="net45" />
4+
<package id="Microsoft.Bcl.Metadata" version="1.0.12-alpha" targetFramework="net45" />
5+
<package id="Microsoft.CodeAnalysis.Common" version="0.7.4091001-beta" targetFramework="net45" />
6+
<package id="Microsoft.CodeAnalysis.VisualBasic" version="0.7.4091001-beta" targetFramework="net45" />
7+
<package id="Microsoft.CodeAnalysis.VisualBasic.Workspaces" version="0.7.4091001-beta" targetFramework="net45" />
8+
<package id="Microsoft.CodeAnalysis.Workspaces.Common" version="0.7.4091001-beta" targetFramework="net45" />
9+
</packages>

src/Microsoft.DotNet.CodeFormatting/FormattingEngineImplementation.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ public async Task<bool> RunAsync(Workspace workspace, CancellationToken cancella
3737
var shouldBeProcessed = await ShouldBeProcessedAsync(document);
3838
if (!shouldBeProcessed)
3939
continue;
40-
40+
4141
var newDocument = await RewriteDocumentAsync(document, cancellationToken);
4242
hasChanges |= newDocument != document;
4343

4444
await SaveDocumentAsync(newDocument, cancellationToken);
45+
Console.WriteLine("Processing document: " + document.Name);
4546
}
4647

4748
return hasChanges;
@@ -84,11 +85,18 @@ private async Task<Document> RewriteDocumentAsync(Document document, Cancellatio
8485
foreach (var rule in _rules)
8586
newDocument = await rule.ProcessAsync(newDocument, cancellationToken);
8687

87-
if (newDocument == previousDocument)
88+
if (IsEqual(newDocument, previousDocument))
8889
break;
8990
}
9091

9192
return newDocument;
9293
}
94+
95+
private bool IsEqual(Document newDocument, Document previousDocument)
96+
{
97+
if (newDocument.GetTextAsync().Result.ToString() == previousDocument.GetTextAsync().Result.ToString())
98+
return true;
99+
return false;
100+
}
93101
}
94102
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
<Reference Include="Microsoft.CodeAnalysis.Desktop">
4848
<HintPath>..\packages\Microsoft.CodeAnalysis.Common.0.7.4091001-beta\lib\net45\Microsoft.CodeAnalysis.Desktop.dll</HintPath>
4949
</Reference>
50+
<Reference Include="Microsoft.CodeAnalysis.VisualBasic">
51+
<HintPath>..\packages\Microsoft.CodeAnalysis.VisualBasic.0.7.4091001-beta\lib\net45\Microsoft.CodeAnalysis.VisualBasic.dll</HintPath>
52+
</Reference>
5053
<Reference Include="Microsoft.CodeAnalysis.Workspaces">
5154
<SpecificVersion>False</SpecificVersion>
5255
<HintPath>..\packages\Microsoft.CodeAnalysis.Workspaces.Common.0.7.4091001-beta\lib\net45\Microsoft.CodeAnalysis.Workspaces.dll</HintPath>
@@ -76,6 +79,7 @@
7679
<Compile Include="Filters\IgnoreDesignerGeneratedCodeFilter.cs" />
7780
<Compile Include="Properties\AssemblyInfo.cs" />
7881
<Compile Include="Rules\HasCopyrightHeaderFormattingRule.cs" />
82+
<Compile Include="Rules\HasNoNewLineAfterOpenBraceFormattingRule.cs" />
7983
<Compile Include="Rules\HasNoXmlBasedCopyrightHeaderFormattingRule.cs" />
8084
<Compile Include="Rules\HasUsingsOutsideOfNamespaceFormattingRule.cs" />
8185
<Compile Include="Rules\IsFormattedFormattingRule.cs" />
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under MIT. See LICENSE in the project root for license information.
3+
using System;
4+
using System.Collections.Generic;
5+
using System.ComponentModel.Composition;
6+
using System.Linq;
7+
using System.Threading;
8+
using System.Threading.Tasks;
9+
10+
using Microsoft.CodeAnalysis;
11+
using Microsoft.CodeAnalysis.CSharp;
12+
13+
namespace Microsoft.DotNet.CodeFormatting.Rules
14+
{
15+
[Export(typeof(IFormattingRule))]
16+
internal sealed class HasNoNewLineAfterOpenBraceFormattingRule : IFormattingRule
17+
{
18+
public async Task<Document> ProcessAsync(Document document, CancellationToken cancellationToken)
19+
{
20+
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken) as CSharpSyntaxNode;
21+
if (syntaxRoot == null)
22+
return document;
23+
24+
var openBraceTokens = syntaxRoot.DescendantTokens().Where((token) => token.CSharpKind() == SyntaxKind.OpenBraceToken);
25+
Func<SyntaxToken, SyntaxToken, SyntaxToken> replacementForTokens = (token, dummy) =>
26+
{
27+
var triviaItem = token.LeadingTrivia.First();
28+
int elementsToRemove = 1;
29+
while (triviaItem == token.LeadingTrivia.ElementAt(elementsToRemove))
30+
elementsToRemove++;
31+
var newToken = token.WithLeadingTrivia(token.LeadingTrivia.Skip(elementsToRemove));
32+
return newToken;
33+
};
34+
35+
var tokensToReplace = openBraceTokens.Where((token) =>
36+
{
37+
var nextToken = token.GetNextToken();
38+
return (nextToken.HasLeadingTrivia && nextToken.LeadingTrivia.First().CSharpKind() == SyntaxKind.EndOfLineTrivia);
39+
}).Select((token) => token.GetNextToken());
40+
41+
return document.WithSyntaxRoot(syntaxRoot.ReplaceTokens(tokensToReplace, replacementForTokens));
42+
}
43+
}
44+
}

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,29 @@
77

88
using Microsoft.CodeAnalysis;
99
using Microsoft.CodeAnalysis.Formatting;
10+
using Microsoft.CodeAnalysis.CSharp;
11+
using System.Linq;
1012

1113
namespace Microsoft.DotNet.CodeFormatting.Rules
1214
{
1315
[Export(typeof(IFormattingRule))]
1416
internal sealed class IsFormattedFormattingRule : IFormattingRule
1517
{
16-
public Task<Document> ProcessAsync(Document document, CancellationToken cancellationToken)
18+
public async Task<Document> ProcessAsync(Document document, CancellationToken cancellationToken)
1719
{
18-
return Formatter.FormatAsync(document, cancellationToken: cancellationToken);
20+
// Roslyn formatter doesn't format code in #if false as it's considered as DisabledTextTrivia. Will be removed after the bug is fixed.
21+
// Doing that manually here
22+
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken) as CSharpSyntaxNode;
23+
var oldTrivia = syntaxRoot.DescendantTrivia().Where(trivia => trivia.CSharpKind() == SyntaxKind.DisabledTextTrivia);
24+
Func<SyntaxTrivia, SyntaxTrivia, SyntaxTrivia> replacementTrivia = (trivia, dummy) =>
25+
{
26+
var compilation = SyntaxFactory.ParseCompilationUnit(trivia.ToString());
27+
var formattedTrivia = Formatter.Format(compilation.SyntaxTree.GetRoot(), document.Project.Solution.Workspace).GetText().ToString();
28+
return SyntaxFactory.DisabledText(formattedTrivia);
29+
};
30+
31+
var newDocument = document.WithSyntaxRoot(syntaxRoot.ReplaceTrivia(oldTrivia, replacementTrivia));
32+
return await Formatter.FormatAsync(newDocument, cancellationToken: cancellationToken);
1933
}
2034
}
2135
}

0 commit comments

Comments
 (0)