diff --git a/src/EditorFeatures/CSharpTest/Structure/SwitchExpressionStructureTests.cs b/src/EditorFeatures/CSharpTest/Structure/SwitchExpressionStructureTests.cs new file mode 100644 index 0000000000000..29643c549439b --- /dev/null +++ b/src/EditorFeatures/CSharpTest/Structure/SwitchExpressionStructureTests.cs @@ -0,0 +1,38 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Threading.Tasks; +using Microsoft.CodeAnalysis.CSharp.Structure; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.Structure; +using Microsoft.CodeAnalysis.Test.Utilities; +using Roslyn.Test.Utilities; +using Xunit; + +namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Structure; + +[Trait(Traits.Feature, Traits.Features.Outlining)] +public sealed class SwitchExpressionStructureTests : AbstractCSharpSyntaxNodeStructureTests +{ + internal override AbstractSyntaxStructureProvider CreateProvider() => new SwitchExpressionStructureProvider(); + + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/69357")] + public async Task TestSwitchExpression1() + { + var code = """ + class C + { + void M(int i) + { + var v = {|hint:$$i switch{|textspan: + { + }|}|} + } + } + """; + + await VerifyBlockSpansAsync(code, + Region("textspan", "hint", CSharpStructureHelpers.Ellipsis, autoCollapse: false)); + } +} diff --git a/src/EditorFeatures/CSharpTest/Structure/SwitchStatementStructureTests.cs b/src/EditorFeatures/CSharpTest/Structure/SwitchStatementStructureTests.cs index 38a80fd42f475..d759d315575fb 100644 --- a/src/EditorFeatures/CSharpTest/Structure/SwitchStatementStructureTests.cs +++ b/src/EditorFeatures/CSharpTest/Structure/SwitchStatementStructureTests.cs @@ -11,11 +11,12 @@ namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Structure; -public class SwitchStatementStructureTests : AbstractCSharpSyntaxNodeStructureTests +[Trait(Traits.Feature, Traits.Features.Outlining)] +public sealed class SwitchStatementStructureTests : AbstractCSharpSyntaxNodeStructureTests { internal override AbstractSyntaxStructureProvider CreateProvider() => new SwitchStatementStructureProvider(); - [Fact, Trait(Traits.Feature, Traits.Features.Outlining)] + [Fact] public async Task TestSwitchStatement1() { var code = """ @@ -34,7 +35,7 @@ await VerifyBlockSpansAsync(code, Region("textspan", "hint", CSharpStructureHelpers.Ellipsis, autoCollapse: false)); } - [Fact, Trait(Traits.Feature, Traits.Features.Outlining)] + [Fact] public async Task TestSwitchStatement2() { var code = """ diff --git a/src/Features/CSharp/Portable/Structure/CSharpBlockStructureProvider.cs b/src/Features/CSharp/Portable/Structure/CSharpBlockStructureProvider.cs index e918319709363..d0a7dcc985d75 100644 --- a/src/Features/CSharp/Portable/Structure/CSharpBlockStructureProvider.cs +++ b/src/Features/CSharp/Portable/Structure/CSharpBlockStructureProvider.cs @@ -47,6 +47,7 @@ private static ImmutableDictionary(); builder.Add(); builder.Add(); + builder.Add(); builder.Add(); builder.Add(); builder.Add(); diff --git a/src/Features/CSharp/Portable/Structure/Providers/SwitchExpressionStructureProvider.cs b/src/Features/CSharp/Portable/Structure/Providers/SwitchExpressionStructureProvider.cs new file mode 100644 index 0000000000000..2dd2781f05d67 --- /dev/null +++ b/src/Features/CSharp/Portable/Structure/Providers/SwitchExpressionStructureProvider.cs @@ -0,0 +1,28 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Threading; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.PooledObjects; +using Microsoft.CodeAnalysis.Structure; +using Microsoft.CodeAnalysis.Text; + +namespace Microsoft.CodeAnalysis.CSharp.Structure; + +internal sealed class SwitchExpressionStructureProvider : AbstractSyntaxNodeStructureProvider +{ + protected override void CollectBlockSpans( + SyntaxToken previousToken, + SwitchExpressionSyntax node, + ArrayBuilder spans, + BlockStructureOptions options, + CancellationToken cancellationToken) + { + spans.Add(new BlockSpan( + isCollapsible: true, + textSpan: TextSpan.FromBounds(node.SwitchKeyword.Span.End, node.CloseBraceToken.Span.End), + hintSpan: node.Span, + type: BlockTypes.Conditional)); + } +} diff --git a/src/Features/CSharp/Portable/Structure/Providers/SwitchStatementStructureProvider.cs b/src/Features/CSharp/Portable/Structure/Providers/SwitchStatementStructureProvider.cs index 68c6aafa8493d..2ab65e47b472a 100644 --- a/src/Features/CSharp/Portable/Structure/Providers/SwitchStatementStructureProvider.cs +++ b/src/Features/CSharp/Portable/Structure/Providers/SwitchStatementStructureProvider.cs @@ -10,7 +10,7 @@ namespace Microsoft.CodeAnalysis.CSharp.Structure; -internal class SwitchStatementStructureProvider : AbstractSyntaxNodeStructureProvider +internal sealed class SwitchStatementStructureProvider : AbstractSyntaxNodeStructureProvider { protected override void CollectBlockSpans( SyntaxToken previousToken,