Skip to content

Commit 50c2db6

Browse files
Fix out of bounds when creating structure guides for types without bodies (#79543)
2 parents 841a82a + 9288f35 commit 50c2db6

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/EditorFeatures/CSharpTest/Structure/TypeDeclarationStructureTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.CodeAnalysis.CSharp.Syntax;
88
using Microsoft.CodeAnalysis.Structure;
99
using Microsoft.CodeAnalysis.Test.Utilities;
10+
using Roslyn.Test.Utilities;
1011
using Xunit;
1112

1213
namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Structure;
@@ -231,4 +232,21 @@ public Task TestStructWithNestedComments()
231232
""",
232233
Region("textspan1", "hint1", CSharpStructureHelpers.Ellipsis, autoCollapse: false),
233234
Region("span2", "// Goo ...", autoCollapse: true));
235+
236+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/62506")]
237+
public Task TestTypeDeclarationNoBraces1()
238+
=> VerifyBlockSpansAsync("""
239+
{|comment:// comment|}
240+
$$struct S
241+
""",
242+
Region("comment", "// comment ...", autoCollapse: true));
243+
244+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/62506")]
245+
public Task TestTypeDeclarationNoBraces2()
246+
=> VerifyBlockSpansAsync("""
247+
{|comment:// comment|}
248+
{|hint1:$$struct S{|textspan1:;|}|}
249+
""",
250+
Region("comment", "// comment ...", autoCollapse: true),
251+
Region("textspan1", "hint1", CSharpStructureHelpers.Ellipsis, autoCollapse: false));
234252
}

src/Features/CSharp/Portable/Structure/CSharpStructureHelpers.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,12 @@ static SyntaxToken GetEndToken(SyntaxNode node)
286286
static SyntaxToken GetHintTextEndToken(SyntaxNode node)
287287
=> node switch
288288
{
289-
EnumDeclarationSyntax enumDeclaration => enumDeclaration.OpenBraceToken.GetPreviousToken(),
290-
TypeDeclarationSyntax typeDeclaration => typeDeclaration.OpenBraceToken.GetPreviousToken(),
289+
EnumDeclarationSyntax enumDeclaration
290+
=> enumDeclaration.OpenBraceToken.GetPreviousToken(),
291+
TypeDeclarationSyntax typeDeclaration
292+
=> typeDeclaration.OpenBraceToken != default
293+
? typeDeclaration.OpenBraceToken.GetPreviousToken()
294+
: typeDeclaration.SemicolonToken.GetPreviousToken(),
291295
_ => node.GetLastToken()
292296
};
293297
}

0 commit comments

Comments
 (0)