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

Do not show structure guides on raw string literals #75938

Merged
merged 1 commit into from
Nov 15, 2024
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
Expand Up @@ -12,7 +12,7 @@
namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Structure;

[Trait(Traits.Feature, Traits.Features.Outlining)]
public class StringLiteralExpressionStructureTests : AbstractCSharpSyntaxNodeStructureTests<LiteralExpressionSyntax>
public sealed class StringLiteralExpressionStructureTests : AbstractCSharpSyntaxNodeStructureTests<LiteralExpressionSyntax>
{
internal override AbstractSyntaxStructureProvider CreateProvider()
=> new StringLiteralExpressionStructureProvider();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Microsoft.CodeAnalysis.CSharp.Structure;

internal class CSharpBlockStructureProvider : AbstractBlockStructureProvider
internal sealed class CSharpBlockStructureProvider : AbstractBlockStructureProvider
{
private static ImmutableDictionary<Type, ImmutableArray<AbstractSyntaxStructureProvider>> CreateDefaultNodeProviderMap()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,43 @@ protected override void CollectBlockSpans(
BlockStructureOptions options,
CancellationToken cancellationToken)
{
if (node.IsKind(SyntaxKind.StringLiteralExpression) &&
!node.ContainsDiagnostics &&
CouldBeMultiLine())
if (node.IsKind(SyntaxKind.StringLiteralExpression) && !node.ContainsDiagnostics)
{
spans.Add(new BlockSpan(
isCollapsible: true,
textSpan: node.Span,
hintSpan: node.Span,
type: BlockTypes.Expression,
autoCollapse: true,
isDefaultCollapsed: false));
var type = GetStringLiteralType();
if (type != null)
{
spans.Add(new BlockSpan(
type,
isCollapsible: true,
textSpan: node.Span,
hintSpan: node.Span,
autoCollapse: true,
isDefaultCollapsed: false));
}
}

return;

bool CouldBeMultiLine()
string? GetStringLiteralType()
{
// We explicitly pick non-structural here as we don't want 'structure' guides shown for raw string literals.
// We already have a specialized tagger for those showing the user the left side of it. So having a
// structure guide as well is redundant.
if (node.Token.Kind() is SyntaxKind.MultiLineRawStringLiteralToken or SyntaxKind.Utf8MultiLineRawStringLiteralToken)
return true;
return BlockTypes.Nonstructural;

if (node.Token.IsVerbatimStringLiteral())
{
var span = node.Span;
var sourceText = node.SyntaxTree.GetText(cancellationToken);
return sourceText.Lines.GetLineFromPosition(span.Start).LineNumber !=
sourceText.Lines.GetLineFromPosition(span.End).LineNumber;
if (sourceText.Lines.GetLineFromPosition(span.Start).LineNumber !=
sourceText.Lines.GetLineFromPosition(span.End).LineNumber)
{
return BlockTypes.Expression;
}
}

return false;
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Imports Microsoft.CodeAnalysis.Structure
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax

Namespace Microsoft.CodeAnalysis.VisualBasic.Structure
Friend Class VisualBasicBlockStructureProvider
Friend NotInheritable Class VisualBasicBlockStructureProvider
Inherits AbstractBlockStructureProvider

Public Shared Function CreateDefaultNodeStructureProviderMap() As ImmutableDictionary(Of Type, ImmutableArray(Of AbstractSyntaxStructureProvider))
Expand Down
Loading