提交 2e272c10 编写于 作者: K Kevin Pilch-Bisson

Merge pull request #5478 from Pilchie/Fix111079-FormattingOfEmbeddedThrow

Wrapping of embedded statements
......@@ -960,25 +960,33 @@ public static SyntaxNode GetParent(this SyntaxNode node)
public static bool IsEmbeddedStatementOwner(this SyntaxNode node)
{
return node is IfStatementSyntax ||
return
node is DoStatementSyntax ||
node is ElseClauseSyntax ||
node is WhileStatementSyntax ||
node is ForStatementSyntax ||
node is FixedStatementSyntax ||
node is ForEachStatementSyntax ||
node is ForStatementSyntax ||
node is IfStatementSyntax ||
node is LabeledStatementSyntax ||
node is LockStatementSyntax ||
node is UsingStatementSyntax ||
node is DoStatementSyntax;
node is WhileStatementSyntax;
}
public static StatementSyntax GetEmbeddedStatement(this SyntaxNode node)
{
return node.TypeSwitch(
(IfStatementSyntax n) => n.Statement,
(DoStatementSyntax n) => n.Statement,
(ElseClauseSyntax n) => n.Statement,
(WhileStatementSyntax n) => n.Statement,
(ForStatementSyntax n) => n.Statement,
(FixedStatementSyntax n) => n.Statement,
(ForEachStatementSyntax n) => n.Statement,
(ForStatementSyntax n) => n.Statement,
(IfStatementSyntax n) => n.Statement,
(LabeledStatementSyntax n) => n.Statement,
(LockStatementSyntax n) => n.Statement,
(UsingStatementSyntax n) => n.Statement,
(DoStatementSyntax n) => n.Statement,
(WhileStatementSyntax n) => n.Statement,
(SyntaxNode n) => null);
}
......
......@@ -7,6 +7,7 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Formatting.Rules;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
......@@ -35,55 +36,59 @@ public override void AddSuppressOperations(List<SuppressOperation> list, SyntaxN
}
}
private void AddSpecificNodesSuppressOperations(List<SuppressOperation> list, SyntaxNode node)
private ValueTuple<SyntaxToken, SyntaxToken> GetSpecificNodeSuppressionTokenRange(SyntaxNode node)
{
var ifStatementNode = node as IfStatementSyntax;
if (ifStatementNode != null)
var embeddedStatement = node.GetEmbeddedStatement();
if (embeddedStatement != null)
{
AddSuppressWrappingIfOnSingleLineOperation(list, ifStatementNode.IfKeyword, ifStatementNode.Statement.GetLastToken(includeZeroWidth: true));
if (ifStatementNode.Else != null)
var firstTokenOfEmbeddedStatement = embeddedStatement.GetFirstToken(includeZeroWidth: true);
if (embeddedStatement.IsKind(SyntaxKind.Block))
{
AddSuppressWrappingIfOnSingleLineOperation(list, ifStatementNode.Else.ElseKeyword, ifStatementNode.Else.Statement.GetLastToken(includeZeroWidth: true));
return ValueTuple.Create(
firstTokenOfEmbeddedStatement.GetPreviousToken(includeZeroWidth: true),
embeddedStatement.GetLastToken(includeZeroWidth: true));
}
else
{
return ValueTuple.Create(
firstTokenOfEmbeddedStatement.GetPreviousToken(includeZeroWidth: true),
firstTokenOfEmbeddedStatement);
}
return;
}
var whileStatementNode = node as DoStatementSyntax;
if (whileStatementNode != null)
{
AddSuppressWrappingIfOnSingleLineOperation(list, whileStatementNode.GetFirstToken(includeZeroWidth: true), whileStatementNode.Statement.GetLastToken(includeZeroWidth: true));
return;
}
var memberDeclNode = node as MemberDeclarationSyntax;
if (memberDeclNode != null)
{
var tokens = memberDeclNode.GetFirstAndLastMemberDeclarationTokensAfterAttributes();
AddSuppressWrappingIfOnSingleLineOperation(list, tokens.Item1, tokens.Item2);
return;
}
var accessorDeclNode = node as AccessorDeclarationSyntax;
if (accessorDeclNode != null)
{
AddSuppressWrappingIfOnSingleLineOperation(list, accessorDeclNode.GetFirstToken(includeZeroWidth: true), accessorDeclNode.GetLastToken(includeZeroWidth: true));
return;
return memberDeclNode.GetFirstAndLastMemberDeclarationTokensAfterAttributes();
}
var switchSection = node as SwitchSectionSyntax;
if (switchSection != null)
{
AddSuppressWrappingIfOnSingleLineOperation(list, switchSection.GetFirstToken(includeZeroWidth: true), switchSection.GetLastToken(includeZeroWidth: true));
return;
return ValueTuple.Create(switchSection.GetFirstToken(includeZeroWidth: true), switchSection.GetLastToken(includeZeroWidth: true));
}
var anonymousMethod = node as AnonymousMethodExpressionSyntax;
if (anonymousMethod != null)
{
AddSuppressWrappingIfOnSingleLineOperation(list, anonymousMethod.DelegateKeyword, anonymousMethod.GetLastToken(includeZeroWidth: true));
return;
return ValueTuple.Create(anonymousMethod.DelegateKeyword, anonymousMethod.GetLastToken(includeZeroWidth: true));
}
return default(ValueTuple<SyntaxToken, SyntaxToken>);
}
private void AddSpecificNodesSuppressOperations(List<SuppressOperation> list, SyntaxNode node)
{
var tokens = GetSpecificNodeSuppressionTokenRange(node);
if (tokens != default(ValueTuple<SyntaxToken, SyntaxToken>))
{
AddSuppressWrappingIfOnSingleLineOperation(list, tokens.Item1, tokens.Item2);
}
var ifStatementNode = node as IfStatementSyntax;
if (ifStatementNode?.Else != null)
{
AddSuppressWrappingIfOnSingleLineOperation(list, ifStatementNode.Else.ElseKeyword, ifStatementNode.Else.Statement.GetFirstToken(includeZeroWidth: true));
}
}
......@@ -112,46 +117,16 @@ private void RemoveSuppressOperationForStatementMethodDeclaration(List<SuppressO
RemoveSuppressOperation(list, firstToken, lastToken);
}
var ifStatementNode = node as IfStatementSyntax;
if (ifStatementNode != null)
{
RemoveSuppressOperation(list, ifStatementNode.IfKeyword, ifStatementNode.Statement.GetLastToken(includeZeroWidth: true));
if (ifStatementNode.Else != null)
{
RemoveSuppressOperation(list, ifStatementNode.Else.ElseKeyword, ifStatementNode.Else.Statement.GetLastToken(includeZeroWidth: true));
}
return;
}
var whileStatementNode = node as DoStatementSyntax;
if (whileStatementNode != null)
{
RemoveSuppressOperation(list, whileStatementNode.GetFirstToken(includeZeroWidth: true), whileStatementNode.Statement.GetLastToken(includeZeroWidth: true));
return;
}
var memberDeclNode = node as MemberDeclarationSyntax;
if (memberDeclNode != null)
var tokens = GetSpecificNodeSuppressionTokenRange(node);
if (tokens != default(ValueTuple<SyntaxToken, SyntaxToken>))
{
var tokens = memberDeclNode.GetFirstAndLastMemberDeclarationTokensAfterAttributes();
RemoveSuppressOperation(list, tokens.Item1, tokens.Item2);
return;
}
var switchSection = node as SwitchSectionSyntax;
if (switchSection != null)
{
RemoveSuppressOperation(list, switchSection.GetFirstToken(includeZeroWidth: true), switchSection.GetLastToken(includeZeroWidth: true));
return;
}
var anonymousMethod = node as AnonymousMethodExpressionSyntax;
if (anonymousMethod != null)
var ifStatementNode = node as IfStatementSyntax;
if (ifStatementNode?.Else != null)
{
RemoveSuppressOperation(list, anonymousMethod.DelegateKeyword, anonymousMethod.GetLastToken(includeZeroWidth: true));
return;
RemoveSuppressOperation(list, ifStatementNode.Else.ElseKeyword, ifStatementNode.Else.Statement.GetFirstToken(includeZeroWidth: true));
}
}
......
......@@ -6519,5 +6519,23 @@ int MethodFoo()
}
}", false, changingOptions);
}
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
[WorkItem(111079, "devdiv.visualstudio.com")]
public void TestThrowInIfOnSingleLine()
{
var code = @"
class C
{
void M()
{
if (true) throw new Exception(
""message"");
}
}
\ No newline at end of file
}
";
AssertFormat(code, code);
}
}
}
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;
using CS = Microsoft.CodeAnalysis.CSharp;
......@@ -10,20 +11,16 @@ namespace Microsoft.CodeAnalysis.UnitTests
{
public partial class FormattingTests : TestBase
{
[Fact]
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
public void TestCSharpFormatting()
{
var text = @"public class C{public int X;}";
var expectedFormattedText = @"public class C { public int X; }";
var tree = CS.SyntaxFactory.ParseSyntaxTree(text);
var formattedRoot = Formatter.Format(tree.GetRoot(), new TestWorkspace());
var actualFormattedText = formattedRoot.ToFullString();
Assert.Equal(expectedFormattedText, actualFormattedText);
AssertFormatCSharp(expectedFormattedText, text);
}
[Fact]
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
public void TestCSharpDefaultRules()
{
var rules = Formatter.GetDefaultFormattingRules(new TestWorkspace(), LanguageNames.CSharp);
......@@ -32,7 +29,7 @@ public void TestCSharpDefaultRules()
Assert.NotEmpty(rules);
}
[Fact]
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
public void TestVisualBasicFormatting()
{
var text = @"
......@@ -46,14 +43,10 @@ End Class
End Class
";
var tree = VB.SyntaxFactory.ParseSyntaxTree(text);
var formattedRoot = Formatter.Format(tree.GetRoot(), new TestWorkspace());
var actualFormattedText = formattedRoot.ToFullString();
Assert.Equal(expectedFormattedText, actualFormattedText);
AssertFormatVB(expectedFormattedText, text);
}
[Fact]
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
public void TestVisualBasicDefaultFormattingRules()
{
var rules = Formatter.GetDefaultFormattingRules(new TestWorkspace(), LanguageNames.VisualBasic);
......@@ -61,5 +54,28 @@ public void TestVisualBasicDefaultFormattingRules()
Assert.NotNull(rules);
Assert.NotEmpty(rules);
}
private void AssertFormatCSharp(string expected, string input)
{
var tree = CS.SyntaxFactory.ParseSyntaxTree(input);
AssertFormat(expected, tree);
}
private void AssertFormatVB(string expected, string input)
{
var tree = VB.SyntaxFactory.ParseSyntaxTree(input);
AssertFormat(expected, tree);
}
private void AssertFormat(string expected, SyntaxTree tree)
{
using (var workspace = new TestWorkspace())
{
var formattedRoot = Formatter.Format(tree.GetRoot(), workspace);
var actualFormattedText = formattedRoot.ToFullString();
Assert.Equal(expected, actualFormattedText);
}
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册