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

Commit 8eaf59a

Browse files
committed
Merge pull request #66 from jaredpar/basic
Initial support for Visual Basic
2 parents 8479c0f + 9a16638 commit 8eaf59a

35 files changed

+1956
-627
lines changed

src/CodeFormatter/CodeFormatter.csproj

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@
5050
<Reference Include="Microsoft.CodeAnalysis.Desktop">
5151
<HintPath>..\packages\Microsoft.CodeAnalysis.Common.1.0.0-beta1-20141031-01\lib\net45\Microsoft.CodeAnalysis.Desktop.dll</HintPath>
5252
</Reference>
53+
<Reference Include="Microsoft.CodeAnalysis.VisualBasic, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
54+
<SpecificVersion>False</SpecificVersion>
55+
<HintPath>..\packages\Microsoft.CodeAnalysis.VisualBasic.1.0.0-beta1-20141031-01\lib\net45\Microsoft.CodeAnalysis.VisualBasic.dll</HintPath>
56+
</Reference>
57+
<Reference Include="Microsoft.CodeAnalysis.VisualBasic.Desktop">
58+
<HintPath>..\packages\Microsoft.CodeAnalysis.VisualBasic.1.0.0-beta1-20141031-01\lib\net45\Microsoft.CodeAnalysis.VisualBasic.Desktop.dll</HintPath>
59+
</Reference>
60+
<Reference Include="Microsoft.CodeAnalysis.VisualBasic.Workspaces">
61+
<HintPath>..\packages\Microsoft.CodeAnalysis.VisualBasic.Workspaces.1.0.0-beta1-20141031-01\lib\net45\Microsoft.CodeAnalysis.VisualBasic.Workspaces.dll</HintPath>
62+
</Reference>
63+
<Reference Include="Microsoft.CodeAnalysis.VisualBasic.Workspaces.Desktop">
64+
<HintPath>..\packages\Microsoft.CodeAnalysis.VisualBasic.Workspaces.1.0.0-beta1-20141031-01\lib\net45\Microsoft.CodeAnalysis.VisualBasic.Workspaces.Desktop.dll</HintPath>
65+
</Reference>
5366
<Reference Include="Microsoft.CodeAnalysis.Workspaces">
5467
<HintPath>..\packages\Microsoft.CodeAnalysis.Workspaces.Common.1.0.0-beta1-20141031-01\lib\net45\Microsoft.CodeAnalysis.Workspaces.dll</HintPath>
5568
</Reference>
@@ -112,4 +125,4 @@
112125
<Copy SourceFiles="$(ProjectDir)IllegalHeaders.md" DestinationFolder="$(OutDir)" ContinueOnError="true" />
113126
<Copy SourceFiles="$(ProjectDir)CopyrightHeader.md" DestinationFolder="$(OutDir)" ContinueOnError="true" />
114127
</Target>
115-
</Project>
128+
</Project>

src/CodeFormatter/packages.config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
<package id="Microsoft.CodeAnalysis.Common" version="1.0.0-beta1-20141031-01" targetFramework="net45" />
55
<package id="Microsoft.CodeAnalysis.CSharp" version="1.0.0-beta1-20141031-01" targetFramework="net45" />
66
<package id="Microsoft.CodeAnalysis.CSharp.Workspaces" version="1.0.0-beta1-20141031-01" targetFramework="net45" />
7+
<package id="Microsoft.CodeAnalysis.VisualBasic" version="1.0.0-beta1-20141031-01" targetFramework="net45" />
8+
<package id="Microsoft.CodeAnalysis.VisualBasic.Workspaces" version="1.0.0-beta1-20141031-01" targetFramework="net45" />
79
<package id="Microsoft.CodeAnalysis.Workspaces.Common" version="1.0.0-beta1-20141031-01" targetFramework="net45" />
810
<package id="Microsoft.Composition" version="1.0.27" targetFramework="net45" />
911
<package id="System.Collections.Immutable" version="1.1.32-beta" targetFramework="net45" />

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ private void AssertSolutionEqual(Solution expectedSolution, Solution actualSolut
9393

9494
protected abstract Task<Document> RewriteDocumentAsync(Document document);
9595

96-
protected void Verify(string[] sources, string[] expected, bool runFormatter)
96+
protected void Verify(string[] sources, string[] expected, bool runFormatter, string languageName)
9797
{
98-
var inputSolution = CreateSolution(sources);
99-
var expectedSolution = CreateSolution(expected);
98+
var inputSolution = CreateSolution(sources, languageName);
99+
var expectedSolution = CreateSolution(expected, languageName);
100100
var actualSolution = Format(inputSolution, runFormatter).Result;
101101

102102
if (actualSolution == null)
@@ -105,9 +105,9 @@ protected void Verify(string[] sources, string[] expected, bool runFormatter)
105105
AssertSolutionEqual(expectedSolution, actualSolution);
106106
}
107107

108-
protected void Verify(string source, string expected, bool runFormatter = true)
108+
protected void Verify(string source, string expected, bool runFormatter = true, string languageName = LanguageNames.CSharp)
109109
{
110-
Verify(new string[] { source }, new string[] { expected }, runFormatter);
110+
Verify(new string[] { source }, new string[] { expected }, runFormatter, languageName);
111111
}
112112
}
113113

@@ -121,7 +121,7 @@ internal abstract ISyntaxFormattingRule Rule
121121
protected override async Task<Document> RewriteDocumentAsync(Document document)
122122
{
123123
var syntaxRoot = await document.GetSyntaxRootAsync();
124-
syntaxRoot = Rule.Process(syntaxRoot);
124+
syntaxRoot = Rule.Process(syntaxRoot, document.Project.Language);
125125
return document.WithSyntaxRoot(syntaxRoot);
126126
}
127127
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
<Compile Include="CodeFormattingTestBase.cs" />
108108
<Compile Include="Rules\BracesRuleTests.cs" />
109109
<Compile Include="Rules\CombinationTest.cs" />
110+
<Compile Include="Rules\CopyrightHeaderRuleTests.cs" />
110111
<Compile Include="Rules\ExplicitThisRuleTests.cs" />
111112
<Compile Include="Rules\ExplicitVisibilityRuleTests.cs" />
112113
<Compile Include="Rules\HasNewLineBeforeFirstNamespaceFormattingRuleTests.cs" />

src/Microsoft.DotNet.CodeFormatting.Tests/Rules/CombinationTest.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ static CombinationTest()
2424

2525
public CombinationTest()
2626
{
27-
s_formattingEngine.CopyrightHeader = ImmutableArray.Create("", "// header");
27+
s_formattingEngine.CopyrightHeader = ImmutableArray.Create("// header");
2828
s_formattingEngine.FormatLogger = new EmptyFormatLogger();
2929
s_formattingEngine.PreprocessorConfigurations = ImmutableArray<string[]>.Empty;
3030
}
@@ -50,8 +50,7 @@ void M() {
5050
}
5151
}";
5252

53-
var expected = @"
54-
// header
53+
var expected = @"// header
5554
5655
internal class C
5756
{
@@ -79,8 +78,7 @@ void M() {
7978
}
8079
}";
8180

82-
var expected = @"
83-
// header
81+
var expected = @"// header
8482
8583
internal class C
8684
{
@@ -106,8 +104,7 @@ void M() { }
106104
#endif
107105
}";
108106

109-
var expected = @"
110-
// header
107+
var expected = @"// header
111108
112109
internal class C
113110
{
@@ -131,8 +128,7 @@ internal void M() {
131128
#endif
132129
}";
133130

134-
var expected = @"
135-
// header
131+
var expected = @"// header
136132
137133
internal class C
138134
{
@@ -163,8 +159,7 @@ void M() {
163159
#endif
164160
}";
165161

166-
var expected = @"
167-
// header
162+
var expected = @"// header
168163
169164
internal class C
170165
{
@@ -202,8 +197,7 @@ void M() {
202197
#endif
203198
}";
204199

205-
var expected = @"
206-
// header
200+
var expected = @"// header
207201
208202
internal class C
209203
{
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
using Microsoft.CodeAnalysis;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Collections.Immutable;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using Xunit;
9+
10+
namespace Microsoft.DotNet.CodeFormatting.Tests
11+
{
12+
public sealed class CopyrightHeaderRuleTests : SyntaxRuleTestBase
13+
{
14+
private readonly Options _options = new Options();
15+
16+
internal override ISyntaxFormattingRule Rule
17+
{
18+
get { return new Rules.CopyrightHeaderRule(_options); }
19+
}
20+
21+
[Fact]
22+
public void CSharpSimple()
23+
{
24+
_options.CopyrightHeader = ImmutableArray.Create("test");
25+
var source = @"
26+
class C
27+
{
28+
}";
29+
30+
var expected = @"// test
31+
32+
class C
33+
{
34+
}";
35+
Verify(source, expected);
36+
37+
}
38+
39+
[Fact]
40+
public void CSharpPreserveExisting()
41+
{
42+
_options.CopyrightHeader = ImmutableArray.Create("test");
43+
var source = @"// test
44+
45+
class C
46+
{
47+
}";
48+
49+
var expected = @"// test
50+
51+
class C
52+
{
53+
}";
54+
Verify(source, expected);
55+
56+
}
57+
58+
[Fact]
59+
public void CSharpDontDoubleComment()
60+
{
61+
_options.CopyrightHeader = ImmutableArray.Create("// test");
62+
var source = @"
63+
class C
64+
{
65+
}";
66+
67+
var expected = @"// test
68+
69+
class C
70+
{
71+
}";
72+
Verify(source, expected);
73+
}
74+
75+
[Fact]
76+
public void CSharpRemoveOlder()
77+
{
78+
_options.CopyrightHeader = ImmutableArray.Create("test");
79+
var source = @"// copyright
80+
81+
class C
82+
{
83+
}";
84+
85+
var expected = @"// test
86+
87+
class C
88+
{
89+
}";
90+
Verify(source, expected);
91+
92+
}
93+
94+
[Fact]
95+
public void VisualBasicSimple()
96+
{
97+
_options.CopyrightHeader = ImmutableArray.Create("test");
98+
var source = @"
99+
Public Class C
100+
End Class";
101+
102+
var expected = @"' test
103+
104+
Public Class C
105+
End Class";
106+
107+
Verify(source, expected, languageName: LanguageNames.VisualBasic);
108+
}
109+
110+
[Fact]
111+
public void VisualBasicNormalizeComment()
112+
{
113+
_options.CopyrightHeader = ImmutableArray.Create("// test");
114+
var source = @"
115+
Public Class C
116+
End Class";
117+
118+
var expected = @"' test
119+
120+
Public Class C
121+
End Class";
122+
123+
Verify(source, expected, languageName: LanguageNames.VisualBasic);
124+
}
125+
126+
[Fact]
127+
public void VisualBasicPreserveExisting()
128+
{
129+
_options.CopyrightHeader = ImmutableArray.Create("// test");
130+
var source = @"' test
131+
132+
Public Class C
133+
End Class";
134+
135+
var expected = @"' test
136+
137+
Public Class C
138+
End Class";
139+
140+
Verify(source, expected, languageName: LanguageNames.VisualBasic);
141+
}
142+
}
143+
}

0 commit comments

Comments
 (0)