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

Commit 7a417d3

Browse files
committed
Fixed a newline case.
1 parent 6348822 commit 7a417d3

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,46 @@ public void UsingWithOnlyBlankAbove()
7474
Verify(text, text);
7575
}
7676

77+
[Fact]
78+
public void StandardCombination()
79+
{
80+
var text = @"// copyright
81+
using System;
82+
using System.Collections;
83+
namespace NS1
84+
{
85+
86+
}";
87+
88+
var expected = @"// copyright
89+
90+
using System;
91+
using System.Collections;
92+
93+
namespace NS1
94+
{
95+
96+
}";
97+
98+
Verify(text, expected);
99+
}
100+
101+
[Fact]
102+
public void StandardCombinationNoWork()
103+
{
104+
var text = @"// copyright
105+
106+
using System;
107+
using System.Collections;
108+
109+
namespace NS1
110+
{
111+
112+
}";
113+
114+
Verify(text, text);
115+
}
116+
77117
[Fact]
78118
public void TestNewLineBeforeFirstUsing01()
79119
{

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,25 @@ private SyntaxNode ProcessNamespace(SyntaxNode syntaxRoot)
4747
return syntaxRoot;
4848
}
4949

50-
return ProcessCore(syntaxRoot, firstNamespace);
50+
var list = firstNamespace.GetLeadingTrivia();
51+
if (list.Count == 0)
52+
{
53+
var newLine = SyntaxUtil.GetBestNewLineTrivia(firstNamespace);
54+
list = list.Add(newLine);
55+
return syntaxRoot.ReplaceNode(firstNamespace, firstNamespace.WithLeadingTrivia(list));
56+
}
57+
else if (list.Count == 1 && list[0].IsKind(SyntaxKind.EndOfLineTrivia))
58+
{
59+
// The namespace node is typically preceeded by a using node. In thate case the trivia will
60+
// be split between the two nodes. If the namespace already has a newline leading trivia then
61+
// there is at least a single blank between the nodes as the using will have a trailing new
62+
// line as well (in case of a single on it will be on the using).
63+
return syntaxRoot;
64+
}
65+
else
66+
{
67+
return ProcessCore(syntaxRoot, firstNamespace);
68+
}
5169
}
5270

5371
private SyntaxNode ProcessCore<TNode>(SyntaxNode syntaxRoot, TNode node) where TNode: SyntaxNode

0 commit comments

Comments
 (0)