提交 a0b3a016 编写于 作者: B Balaji Soundrarajan

Merge pull request #8417 from basoundr/DoWhileFormatting

Format Do block on Close brace without suppression
......@@ -50,9 +50,10 @@ internal class SmartTokenFormatter : ISmartTokenFormatter
var common = startToken.GetCommonRoot(endToken);
// if there are errors, do not touch lines
// Exception: In the case of try-catch-finally block, a try block without a catch/finally block is considered incomplete
// Exception 1: In the case of try-catch-finally block, a try block without a catch/finally block is considered incomplete
// but we would like to apply line operation in a completed try block even if there is no catch/finally block
if (common.ContainsDiagnostics && !CloseBraceOfTryBlock(endToken))
// Exception 2: Similar behavior for do-while
if (common.ContainsDiagnostics && !CloseBraceOfTryOrDoBlock(endToken))
{
smartTokenformattingRules = (new NoLineChangeFormattingRule()).Concat(_formattingRules);
}
......@@ -60,11 +61,11 @@ internal class SmartTokenFormatter : ISmartTokenFormatter
return Formatter.GetFormattedTextChanges(_root, new TextSpan[] { TextSpan.FromBounds(startToken.SpanStart, endToken.Span.End) }, workspace, _optionSet, smartTokenformattingRules, cancellationToken);
}
private bool CloseBraceOfTryBlock(SyntaxToken endToken)
private bool CloseBraceOfTryOrDoBlock(SyntaxToken endToken)
{
return endToken.IsKind(SyntaxKind.CloseBraceToken) &&
endToken.Parent.IsKind(SyntaxKind.Block) &&
endToken.Parent.IsParentKind(SyntaxKind.TryStatement);
(endToken.Parent.IsParentKind(SyntaxKind.TryStatement) || endToken.Parent.IsParentKind(SyntaxKind.DoStatement));
}
public Task<IList<TextChange>> FormatTokenAsync(Workspace workspace, SyntaxToken token, CancellationToken cancellationToken)
......
......@@ -1028,6 +1028,35 @@ void Method()
await AutoFormatOnCloseBraceAsync(code, expected, SyntaxKind.OpenBraceToken);
}
[WpfFact]
[WorkItem(8413, "https://github.com/dotnet/roslyn/issues/8413")]
[Trait(Traits.Feature, Traits.Features.SmartTokenFormatting)]
public async Task EmbeddedStatementDoBlockAlone()
{
var code = @"using System;
class Class
{
void Method()
{
do {
}$$
}
}";
var expected = @"using System;
class Class
{
void Method()
{
do
{
}
}
}";
await AutoFormatOnCloseBraceAsync(code, expected, SyntaxKind.OpenBraceToken);
}
[WpfFact]
[Trait(Traits.Feature, Traits.Features.SmartTokenFormatting)]
public async Task EmbeddedStatement5()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册