Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for outlining switch expressions #76827

Merged
merged 2 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<SwitchExpressionSyntax>
{
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@

namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Structure;

public class SwitchStatementStructureTests : AbstractCSharpSyntaxNodeStructureTests<SwitchStatementSyntax>
[Trait(Traits.Feature, Traits.Features.Outlining)]
public sealed class SwitchStatementStructureTests : AbstractCSharpSyntaxNodeStructureTests<SwitchStatementSyntax>
{
internal override AbstractSyntaxStructureProvider CreateProvider() => new SwitchStatementStructureProvider();

[Fact, Trait(Traits.Feature, Traits.Features.Outlining)]
[Fact]
public async Task TestSwitchStatement1()
{
var code = """
Expand All @@ -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 = """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ private static ImmutableDictionary<Type, ImmutableArray<AbstractSyntaxStructureP
builder.Add<RegionDirectiveTriviaSyntax, RegionDirectiveStructureProvider>();
builder.Add<SimpleLambdaExpressionSyntax, SimpleLambdaExpressionStructureProvider>();
builder.Add<StructDeclarationSyntax, TypeDeclarationStructureProvider>();
builder.Add<SwitchExpressionSyntax, SwitchExpressionStructureProvider>();
builder.Add<SwitchStatementSyntax, SwitchStatementStructureProvider>();
builder.Add<LiteralExpressionSyntax, StringLiteralExpressionStructureProvider>();
builder.Add<InterpolatedStringExpressionSyntax, InterpolatedStringExpressionStructureProvider>();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<SwitchExpressionSyntax>
{
protected override void CollectBlockSpans(
SyntaxToken previousToken,
SwitchExpressionSyntax node,
ArrayBuilder<BlockSpan> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Microsoft.CodeAnalysis.CSharp.Structure;

internal class SwitchStatementStructureProvider : AbstractSyntaxNodeStructureProvider<SwitchStatementSyntax>
internal sealed class SwitchStatementStructureProvider : AbstractSyntaxNodeStructureProvider<SwitchStatementSyntax>
{
protected override void CollectBlockSpans(
SyntaxToken previousToken,
Expand Down
Loading