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

Commit 3dcea06

Browse files
author
Lakshmi Priya Sekar
committed
Fix bugs
1 parent 4c25b8e commit 3dcea06

7 files changed

+132
-2
lines changed

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="Rules\HasNoNewLineAfterOpenBraceFormattingRuleTests.cs" />
108108
<Compile Include="Rules\HasNoNewLineBeforeEndBraceFormattingRuleTests.cs" />
109109
<Compile Include="Rules\HasPrivateAccessorOnFieldNamesFormattingRuleTests.cs" />
110+
<Compile Include="Rules\HasUnderScoreInPrivateFieldNamesFormattingRuleTests.cs" />
110111
</ItemGroup>
111112
<ItemGroup>
112113
<Folder Include="Properties\" />

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ class O
3434
3535
}
3636
class M {
37+
}
38+
class R
39+
{
40+
// some comment
41+
3742
}";
3843
var expected = @"
3944
using System;
@@ -57,6 +62,10 @@ class O
5762
}
5863
class M
5964
{
65+
}
66+
class R
67+
{
68+
// some comment
6069
}";
6170
Verify(text, expected);
6271
}
@@ -84,6 +93,44 @@ class L
8493
Verify(text, expected);
8594
}
8695

96+
[Fact]
97+
public void TestNoNewLineBeforeEndBrace03()
98+
{
99+
var text = @"
100+
class S
101+
{
102+
103+
104+
}
105+
106+
107+
";
108+
var expected = @"
109+
class S
110+
{
111+
}
112+
113+
114+
";
115+
Verify(text, expected);
116+
}
117+
118+
[Fact]
119+
public void TestNoNewLineBeforeEndBrace04()
120+
{
121+
var text = @"
122+
class S
123+
{
124+
125+
126+
}";
127+
var expected = @"
128+
class S
129+
{
130+
}";
131+
Verify(text, expected);
132+
}
133+
87134
internal override IFormattingRule GetFormattingRule()
88135
{
89136
return new Rules.HasNoNewLineBeforeEndBraceFormattingRule();
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 Xunit;
5+
6+
namespace Microsoft.DotNet.CodeFormatting.Tests
7+
{
8+
public class HasUnderScoreInPrivateFieldNamesFormattingRuleTests : CodeFormattingTestBase
9+
{
10+
[Fact]
11+
public void TestUnderScoreInPrivateFields()
12+
{
13+
var text = @"
14+
using System;
15+
class T
16+
{
17+
private static int x;
18+
private static int s_y;
19+
// some trivia
20+
private static int m_z;
21+
// some trivia
22+
private int k = 1, m_s = 2;
23+
// some trivia
24+
[ThreadStatic] static int r;
25+
[ThreadStaticAttribute] static int b_r;
26+
}";
27+
var expected = @"
28+
using System;
29+
class T
30+
{
31+
private static int s_x;
32+
private static int s_y;
33+
// some trivia
34+
private static int s_z;
35+
// some trivia
36+
private int _k = 1, _s = 2;
37+
// some trivia
38+
[ThreadStatic]
39+
static int t_r;
40+
[ThreadStaticAttribute]
41+
static int t_r;
42+
}";
43+
Verify(text, expected);
44+
}
45+
46+
internal override IFormattingRule GetFormattingRule()
47+
{
48+
return new Rules.HasUnderScoreInPrivateFieldNamesFormattingRule();
49+
}
50+
}
51+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
<Compile Include="Rules\HasNoNewLineBeforeEndBraceFormattingRule.cs" />
9696
<Compile Include="Rules\HasNoCustomCopyrightHeaderFormattingRule.cs" />
9797
<Compile Include="Rules\HasPrivateAccessorOnFieldNamesFormattingRule.cs" />
98+
<Compile Include="Rules\HasRightEncodingFormattingRule.cs" />
9899
<Compile Include="Rules\HasUnderScoreInPrivateFieldNamesFormattingRule.cs" />
99100
<Compile Include="Rules\HasUsingsOutsideOfNamespaceFormattingRule.cs" />
100101
<Compile Include="Rules\IsFormattedFormattingRule.cs" />

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ private static IEnumerable<SyntaxTrivia> RemoveNewLinesFromBottom(IEnumerable<Sy
8787

8888
var newTrivia = trivia.Take(elementsToRemoveAtEnd + 1);
8989

90+
if (newTrivia.Any() && newTrivia.Last().CSharpKind().ToString().ToLower().Contains("comment"))
91+
addNewLine = true;
92+
9093
if (addWhitespace)
9194
{
9295
if (newTrivia.Last().IsDirective)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 System.Text;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
8+
using Microsoft.CodeAnalysis;
9+
using Microsoft.CodeAnalysis.Text;
10+
11+
namespace Microsoft.DotNet.CodeFormatting.Rules
12+
{
13+
[RuleOrder(int.MaxValue)]
14+
internal sealed class HasRightEncodingFormattingRule : IFormattingRule
15+
{
16+
Encoding _desiredEncoding = new UTF8Encoding(false);
17+
public async Task<Document> ProcessAsync(Document document, CancellationToken cancellationToken)
18+
{
19+
var text = await document.GetTextAsync();
20+
var newText = SourceText.From(text.ToString(), _desiredEncoding);
21+
return document.WithText(newText);
22+
}
23+
}
24+
}
25+

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections.Generic;
77
using System.ComponentModel.Composition;
88
using System.Linq;
9+
using System.Text.RegularExpressions;
910
using System.Threading;
1011
using System.Threading.Tasks;
1112

@@ -87,11 +88,12 @@ private async Task<Solution> RenameFields(Solution solution, DocumentId document
8788
private static string GetNewSymbolName(ISymbol symbol)
8889
{
8990
var symbolName = symbol.Name.TrimStart('_');
90-
if (symbolName.StartsWith("m_", StringComparison.OrdinalIgnoreCase)) symbolName = symbolName.Remove(0, 2);
91+
Regex regex = new Regex("^(.)_");
92+
if (regex.IsMatch(symbolName)) symbolName = symbolName.Remove(0, 2);
9193
if (symbol.IsStatic)
9294
{
9395
// Check for ThreadStatic private fields.
94-
if (symbol.GetAttributes().Any(a => a.AttributeClass.Name.Equals("ThreadStatic")))
96+
if (symbol.GetAttributes().Any(a => a.AttributeClass.Name.Equals("ThreadStaticAttribute")))
9597
{
9698
if (!symbolName.StartsWith("t_", StringComparison.OrdinalIgnoreCase))
9799
return "t_" + symbolName;

0 commit comments

Comments
 (0)