diff --git a/src/EditorFeatures/CSharpTest/Formatting/FormattingEngineTestBase.cs b/src/EditorFeatures/CSharpTest/Formatting/FormattingEngineTestBase.cs index 7f471f8b1a1a94c551a21870e332b23c25238c4b..454551fa7570189a4cba9a55350be2c908fff048 100644 --- a/src/EditorFeatures/CSharpTest/Formatting/FormattingEngineTestBase.cs +++ b/src/EditorFeatures/CSharpTest/Formatting/FormattingEngineTestBase.cs @@ -267,5 +267,18 @@ protected async Task AssertFormatWithBaseIndentAsync(string expected, string mar new List { span }, baseIndentation: baseIndentation); } + + /// + /// Asserts formatting on an arbitrary that is not part of a + /// + /// the to format. + /// uses an for formatting context, since the is not associated with a + protected async Task AssertFormatOnArbitraryNodeAsync(SyntaxNode node, string expected) + { + var result = await Formatter.FormatAsync(node, new AdhocWorkspace()); + var actual = result.GetText().ToString(); + + Assert.Equal(expected, actual); + } } } diff --git a/src/EditorFeatures/CSharpTest/Formatting/FormattingEngineTests.cs b/src/EditorFeatures/CSharpTest/Formatting/FormattingEngineTests.cs index 863c7d12c2865e7003aba5dbfc8ec474e0a05474..899d387856cc188cebee8931f0db1a8b211b150f 100644 --- a/src/EditorFeatures/CSharpTest/Formatting/FormattingEngineTests.cs +++ b/src/EditorFeatures/CSharpTest/Formatting/FormattingEngineTests.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Editor.Commands; using Microsoft.CodeAnalysis.Editor.Implementation.Formatting; using Microsoft.CodeAnalysis.Editor.Options; @@ -534,8 +535,6 @@ static void Main(int a, int b) await AssertFormatAfterTypeCharAsync(code, expected); } - - [WorkItem(449, "https://github.com/dotnet/roslyn/issues/449")] [WorkItem(1077103, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/1077103")] [WpfFact, Trait(Traits.Feature, Traits.Features.Formatting)] @@ -1405,6 +1404,18 @@ public void M() await AssertFormatAfterTypeCharAsync(code, expected); } + [WorkItem(11642, "https://github.com/dotnet/roslyn/issues/11642")] + [WpfFact, Trait(Traits.Feature, Traits.Features.Formatting)] + public async Task FormatArbitraryNodeParenthesizedLambdaExpression() + { + // code equivalent to an expression synthesized like so: + // ParenthesizedExpression(ParenthesizedLambdaExpression(ParameterList(), Block())) + var code = @"(()=>{})"; + var node = SyntaxFactory.ParseExpression(code); + var expected = @"(() => { })"; + await AssertFormatOnArbitraryNodeAsync(node, expected); + } + private static async Task AssertFormatAfterTypeCharAsync(string code, string expected, Dictionary changedOptionSet = null) { using (var workspace = await TestWorkspace.CreateCSharpAsync(code)) diff --git a/src/Workspaces/CSharp/Portable/Formatting/Rules/BaseFormattingRule.cs b/src/Workspaces/CSharp/Portable/Formatting/Rules/BaseFormattingRule.cs index eb7767cea34cfe2756c33897e005b98b0c4a34aa..665b68efd086c80724e25b6b79648251cd7b539a 100644 --- a/src/Workspaces/CSharp/Portable/Formatting/Rules/BaseFormattingRule.cs +++ b/src/Workspaces/CSharp/Portable/Formatting/Rules/BaseFormattingRule.cs @@ -257,7 +257,7 @@ protected void AddBraceSuppressOperations(List list, SyntaxNo private bool SomeParentHasMissingCloseBrace(SyntaxNode node) { - while (node.Kind() != SyntaxKind.CompilationUnit) + while (node != null && node.Kind() != SyntaxKind.CompilationUnit) { var bracePair = node.GetBracePair(); if (bracePair.Item2.IsMissing)