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

Commit d045b03

Browse files
committed
Begin support for VB.Net
This commit sets the stage for adding VB.Net support into the code formatter by allowing rules to partition their support based on the language in question. This is the start of getting issue #55 implemented.
1 parent a82be6c commit d045b03

16 files changed

+66
-20
lines changed

src/Microsoft.DotNet.CodeFormatting/FormattingEngineImplementation.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,16 @@ private Task<SyntaxNode> GetSyntaxRootAndFilter(Document document, CancellationT
150150
return document.GetSyntaxRootAsync(cancellationToken);
151151
}
152152

153+
private Task<SyntaxNode> GetSyntaxRootAndFilter(IFormattingRule formattingRule, Document document, CancellationToken cancellationToken)
154+
{
155+
if (!formattingRule.SupportsLanguage(document.Project.Language))
156+
{
157+
return Task.FromResult<SyntaxNode>(null);
158+
}
159+
160+
return GetSyntaxRootAndFilter(formattingRule, document, cancellationToken);
161+
}
162+
153163
private void StartDocument()
154164
{
155165
_watch.Restart();
@@ -182,7 +192,7 @@ private async Task<Solution> RunSyntaxPass(Solution originalSolution, IReadOnlyL
182192
}
183193

184194
StartDocument();
185-
var newRoot = RunSyntaxPass(syntaxRoot);
195+
var newRoot = RunSyntaxPass(syntaxRoot, document.Project.Language);
186196
EndDocument(document);
187197

188198
if (newRoot != syntaxRoot)
@@ -194,11 +204,14 @@ private async Task<Solution> RunSyntaxPass(Solution originalSolution, IReadOnlyL
194204
return currentSolution;
195205
}
196206

197-
private SyntaxNode RunSyntaxPass(SyntaxNode root)
207+
private SyntaxNode RunSyntaxPass(SyntaxNode root, string langaugeName)
198208
{
199209
foreach (var rule in _syntaxRules)
200210
{
201-
root = rule.Process(root);
211+
if (rule.SupportsLanguage(langaugeName))
212+
{
213+
root = rule.Process(root);
214+
}
202215
}
203216

204217
return root;
@@ -222,7 +235,7 @@ private async Task<Solution> RunLocalSemanticPass(Solution originalSolution, IRe
222235
foreach (var documentId in documentIds)
223236
{
224237
var document = originalSolution.GetDocument(documentId);
225-
var syntaxRoot = await GetSyntaxRootAndFilter(document, cancellationToken);
238+
var syntaxRoot = await GetSyntaxRootAndFilter(localSemanticRule, document, cancellationToken);
226239
if (syntaxRoot == null)
227240
{
228241
continue;
@@ -258,7 +271,7 @@ private async Task<Solution> RunGlobalSemanticPass(Solution solution, IReadOnlyL
258271
foreach (var documentId in documentIds)
259272
{
260273
var document = solution.GetDocument(documentId);
261-
var syntaxRoot = await GetSyntaxRootAndFilter(document, cancellationToken);
274+
var syntaxRoot = await GetSyntaxRootAndFilter(globalSemanticRule, document, cancellationToken);
262275
if (syntaxRoot == null)
263276
{
264277
continue;

src/Microsoft.DotNet.CodeFormatting/IFormattingRule.cs

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

1010
namespace Microsoft.DotNet.CodeFormatting
1111
{
12+
/// <summary>
13+
/// Base formatting rule which helps establish which language the rule applies to.
14+
/// </summary>
15+
internal interface IFormattingRule
16+
{
17+
bool SupportsLanguage(string languageName);
18+
}
19+
1220
/// <summary>
1321
/// Rules which need no semantic information and operate on parse trees only.
1422
/// </summary>
15-
internal interface ISyntaxFormattingRule
23+
internal interface ISyntaxFormattingRule : IFormattingRule
1624
{
1725
SyntaxNode Process(SyntaxNode syntaxRoot);
1826
}
@@ -22,15 +30,15 @@ internal interface ISyntaxFormattingRule
2230
/// used for rules that need to see a <see cref="Document"/> and <see cref="SyntaxNode"/> which
2331
/// are in sync with each other,
2432
/// </summary>
25-
internal interface ILocalSemanticFormattingRule
33+
internal interface ILocalSemanticFormattingRule : IFormattingRule
2634
{
2735
Task<SyntaxNode> ProcessAsync(Document document, SyntaxNode syntaxRoot, CancellationToken cancellationToken);
2836
}
2937

3038
/// <summary>
3139
/// Rules which can affect more than the local document
3240
/// </summary>
33-
internal interface IGlobalSemanticFormattingRule
41+
internal interface IGlobalSemanticFormattingRule : IFormattingRule
3442
{
3543
Task<Solution> ProcessAsync(Document document, SyntaxNode syntaxRoot, CancellationToken cancellationToken);
3644
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
<Compile Include="Properties\AssemblyInfo.cs" />
8888
<Compile Include="IOrderMetadata.cs" />
8989
<Compile Include="Rules\BraceNewLineRule.cs" />
90+
<Compile Include="Rules\CSharpOnlyFormattingRule.cs" />
9091
<Compile Include="Rules\ExplicitVisibilityRule.cs" />
9192
<Compile Include="Rules\HasCopyrightHeaderFormattingRule.cs" />
9293
<Compile Include="Rules\HasNewLineBeforeFirstNamespaceFormattingRule.cs" />

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace Microsoft.DotNet.CodeFormatting.Rules
1515
{
1616
[SyntaxRuleOrder(SyntaxRuleOrder.BraceNewLineRule)]
17-
internal sealed class BraceNewLineRule : ISyntaxFormattingRule
17+
internal sealed class BraceNewLineRule : CSharpOnlyFormattingRule, ISyntaxFormattingRule
1818
{
1919
private enum NewLineKind
2020
{
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Microsoft.CodeAnalysis;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
11+
namespace Microsoft.DotNet.CodeFormatting.Rules
12+
{
13+
internal class CSharpOnlyFormattingRule : IFormattingRule
14+
{
15+
protected CSharpOnlyFormattingRule()
16+
{
17+
}
18+
19+
public bool SupportsLanguage(string languageName)
20+
{
21+
return languageName == LanguageNames.CSharp;
22+
}
23+
}
24+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Microsoft.DotNet.CodeFormatting.Rules
1313
{
1414
[LocalSemanticRuleOrder(LocalSemanticRuleOrder.RemoveExplicitThisRule)]
15-
internal sealed class ExplicitThisRule : ILocalSemanticFormattingRule
15+
internal sealed class ExplicitThisRule : CSharpOnlyFormattingRule, ILocalSemanticFormattingRule
1616
{
1717
private sealed class ExplicitThisRewriter : CSharpSyntaxRewriter
1818
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
namespace Microsoft.DotNet.CodeFormatting.Rules
1616
{
1717
[LocalSemanticRuleOrder(LocalSemanticRuleOrder.ExplicitVisibilityRule)]
18-
internal sealed class ExplicitVisibilityRule : ILocalSemanticFormattingRule
18+
internal sealed class ExplicitVisibilityRule : CSharpOnlyFormattingRule, ILocalSemanticFormattingRule
1919
{
2020
private sealed class VisibilityRewriter : CSharpSyntaxRewriter
2121
{

src/Microsoft.DotNet.CodeFormatting/Rules/FormatDocumentFormattingRule.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
[LocalSemanticRuleOrder(LocalSemanticRuleOrder.IsFormattedFormattingRule)]
19-
internal sealed class FormatDocumentFormattingRule : ILocalSemanticFormattingRule
19+
internal sealed class FormatDocumentFormattingRule : CSharpOnlyFormattingRule, ILocalSemanticFormattingRule
2020
{
2121
private readonly Options _options;
2222

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace Microsoft.DotNet.CodeFormatting.Rules
1515
{
1616
[SyntaxRuleOrder(SyntaxRuleOrder.HasCopyrightHeaderFormattingRule)]
17-
internal sealed class HasCopyrightHeaderFormattingRule : ISyntaxFormattingRule
17+
internal sealed class HasCopyrightHeaderFormattingRule : CSharpOnlyFormattingRule, ISyntaxFormattingRule
1818
{
1919
private readonly Options _options;
2020

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
namespace Microsoft.DotNet.CodeFormatting.Rules
1616
{
1717
[SyntaxRuleOrder(SyntaxRuleOrder.HasNewLineBeforeFirstNamespaceFormattingRule)]
18-
internal sealed class HasNewLineBeforeFirstNamespaceFormattingRule : ISyntaxFormattingRule
18+
internal sealed class HasNewLineBeforeFirstNamespaceFormattingRule : CSharpOnlyFormattingRule, ISyntaxFormattingRule
1919
{
2020
public SyntaxNode Process(SyntaxNode syntaxRoot)
2121
{

0 commit comments

Comments
 (0)