diff --git a/src/EditorFeatures/CSharpTest/Structure/InterpolatedStringExpressionStructureTests.cs b/src/EditorFeatures/CSharpTest/Structure/InterpolatedStringExpressionStructureTests.cs new file mode 100644 index 0000000000000000000000000000000000000000..da4eca97506d346a49794cb9547db15acbb359a1 --- /dev/null +++ b/src/EditorFeatures/CSharpTest/Structure/InterpolatedStringExpressionStructureTests.cs @@ -0,0 +1,49 @@ +// 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 System.Threading.Tasks; +using Microsoft.CodeAnalysis.CSharp.Structure; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.Structure; +using Microsoft.CodeAnalysis.Test.Utilities; +using Xunit; + +namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Structure +{ + public class InterpolatedStringExpressionStructureTests : AbstractCSharpSyntaxNodeStructureTests + { + internal override AbstractSyntaxStructureProvider CreateProvider() + => new InterpolatedStringExpressionStructureProvider(); + + [Fact, Trait(Traits.Feature, Traits.Features.Outlining)] + public async Task TestMultiLineStringLiteral() + { + await VerifyBlockSpansAsync( +@" +class C +{ + void M() + { + var v = +{|hint:{|textspan:$$$@"" +{123} +""|}|}; + } +}", + Region("textspan", "hint", CSharpStructureHelpers.Ellipsis, autoCollapse: true)); + } + + [Fact, Trait(Traits.Feature, Traits.Features.Outlining)] + public async Task TestMissingOnIncompleteStringLiteral() + { + await VerifyNoBlockSpansAsync( +@" +class C +{ + void M() + { + var v = $$$""; + } +}"); + } + } +} diff --git a/src/Features/CSharp/Portable/Structure/Providers/InterpolatedStringExpressionStructureProvider.cs b/src/Features/CSharp/Portable/Structure/Providers/InterpolatedStringExpressionStructureProvider.cs new file mode 100644 index 0000000000000000000000000000000000000000..943d9c33670d3f9c4dcfecb94d6f512413383c3c --- /dev/null +++ b/src/Features/CSharp/Portable/Structure/Providers/InterpolatedStringExpressionStructureProvider.cs @@ -0,0 +1,30 @@ +// 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 System.Threading; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.CodeAnalysis.Options; +using Microsoft.CodeAnalysis.PooledObjects; +using Microsoft.CodeAnalysis.Structure; + +namespace Microsoft.CodeAnalysis.CSharp.Structure +{ + internal sealed class InterpolatedStringExpressionStructureProvider : AbstractSyntaxNodeStructureProvider + { + protected override void CollectBlockSpans(InterpolatedStringExpressionSyntax node, ArrayBuilder spans, OptionSet options, CancellationToken cancellationToken) + { + if (node.StringStartToken.IsMissing || + node.StringEndToken.IsMissing) + { + return; + } + + spans.Add(new BlockSpan( + isCollapsible: true, + textSpan: node.Span, + hintSpan: node.Span, + type: BlockTypes.Expression, + autoCollapse: true, + isDefaultCollapsed: false)); + } + } +}