未验证 提交 b243b05a 编写于 作者: C CyrusNajmabadi 提交者: GitHub

Merge pull request #42216 from CyrusNajmabadi/singleLineBlock

Format single-line block properly when adding a field initializer in it.
......@@ -5,10 +5,10 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Formatting;
using Microsoft.CodeAnalysis.CSharp.InitializeParameter;
using Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.CodeRefactorings;
using Microsoft.CodeAnalysis.Editor.UnitTests.Diagnostics.NamingStyles;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;
......@@ -1517,5 +1517,48 @@ public C(string? s)
public string? S { get; }
}", parameters: new TestParameters(options: options.PropertyNamesArePascalCase));
}
[WorkItem(24526, "https://github.com/dotnet/roslyn/issues/24526")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInitializeParameter)]
public async Task TestSingleLineBlock_BraceOnNextLine()
{
await TestInRegularAndScript1Async(
@"
class C
{
public C([||]string s) { }
}",
@"
class C
{
public C(string s)
{
S = s;
}
public string S { get; }
}");
}
[WorkItem(24526, "https://github.com/dotnet/roslyn/issues/24526")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInitializeParameter)]
public async Task TestSingleLineBlock_BraceOnSameLine()
{
await TestInRegularAndScriptAsync(
@"
class C
{
public C([||]string s) { }
}",
@"
class C
{
public C(string s) {
S = s;
}
public string S { get; }
}", options: this.Option(CSharpFormattingOptions.NewLinesForBracesInMethods, false));
}
}
}
......@@ -747,6 +747,9 @@ protected static (OptionKey, object) SingleOption<T>(PerLanguageOption<CodeStyle
protected IDictionary<OptionKey, object> Option<T>(PerLanguageOption<CodeStyleOption<T>> option, T enabled, NotificationOption notification)
=> OptionsSet(SingleOption(option, enabled, notification));
protected IDictionary<OptionKey, object> Option<T>(Option<T> option, T value)
=> OptionsSet(SingleOption(option, value));
protected IDictionary<OptionKey, object> Option<T>(PerLanguageOption<T> option, T value)
=> OptionsSet(SingleOption(option, value));
......
......@@ -126,8 +126,13 @@ public bool IsOnSingleLine(SyntaxNode node, bool fullSpan)
var index = 0;
foreach (var child in childNodesAndTokens.Reverse())
{
var first = index == 0;
var last = index == childCount - 1;
// Since we're walking the children in reverse, if we're on hte 0th item,
// that's the last child.
var last = index == 0;
// Once we get all the way to the end of the reversed list, we're actually
// on the first.
var first = index == childCount - 1;
// We want the leading trivia if we've asked for it, or if we're not the first
// token being processed. We want the trailing trivia if we've asked for it,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册