From ad2afadd0726f0a485800f0c320e06c5ea619d8d Mon Sep 17 00:00:00 2001 From: Shen Chen Date: Thu, 18 Jul 2024 11:24:07 -0700 Subject: [PATCH] Merge pull request #74413 from Cosifne/dev/shech/FixMissingCollapse Also checking line end when block structure is filtered --- .../InvalidIdentifierStructureTests.cs | 1 + .../Test/Structure/StructureTaggerTests.cs | 34 +++++++++++++++++++ .../Syntax/AbstractBlockStructureProvider.cs | 11 +++--- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/EditorFeatures/CSharpTest/Structure/MetadataAsSource/InvalidIdentifierStructureTests.cs b/src/EditorFeatures/CSharpTest/Structure/MetadataAsSource/InvalidIdentifierStructureTests.cs index 4c7aa49f62348..e68df4f868815 100644 --- a/src/EditorFeatures/CSharpTest/Structure/MetadataAsSource/InvalidIdentifierStructureTests.cs +++ b/src/EditorFeatures/CSharpTest/Structure/MetadataAsSource/InvalidIdentifierStructureTests.cs @@ -72,6 +72,7 @@ public async Task IdentifierThatLooksLikeCode() await VerifyBlockSpansAsync(code, Region("textspan3", "/* now everything is commented (); ...", autoCollapse: true), + Region("textspan2", "hint2", CSharpStructureHelpers.Ellipsis, autoCollapse: false), Region("textspan1", "hint1", CSharpStructureHelpers.Ellipsis, autoCollapse: false)); } } diff --git a/src/EditorFeatures/Test/Structure/StructureTaggerTests.cs b/src/EditorFeatures/Test/Structure/StructureTaggerTests.cs index 42046b5d37d1c..cd058d7c8bff1 100644 --- a/src/EditorFeatures/Test/Structure/StructureTaggerTests.cs +++ b/src/EditorFeatures/Test/Structure/StructureTaggerTests.cs @@ -8,6 +8,7 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.CodeAnalysis.Editor.Implementation.Structure; +using Microsoft.CodeAnalysis.Editor.Shared.Options; using Microsoft.CodeAnalysis.Editor.Shared.Utilities; using Microsoft.CodeAnalysis.Editor.Tagging; using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces; @@ -325,6 +326,39 @@ End Sub hints.Do(v => v.TextView_TestOnly.Close()); } + [WpfFact] + [WorkItem("https://devdiv.visualstudio.com/DevDiv/_workitems/edit/2094051")] + public async Task IfShouldBeCollapsed() + { + var code = @" +Module Program + Sub Main(args As String()) + Dim str = """" + If str.Contains(""foo"") Then + + End If + End Sub +End Module"; + + using var workspace = EditorTestWorkspace.CreateVisualBasic(code, composition: EditorTestCompositions.EditorFeaturesWpf); + var tags = await GetTagsFromWorkspaceAsync(workspace); + Assert.Collection(tags, programTag => + { + Assert.Equal("Module Program", GetHeaderText(programTag)); + Assert.Equal(8, GetCollapsedHintLineCount(programTag)); + }, + mainTag => + { + Assert.Equal("Sub Main(args As String())", GetHeaderText(mainTag)); + Assert.Equal(6, GetCollapsedHintLineCount(mainTag)); + }, + IfTag => + { + Assert.Equal("If str.Contains(\"foo\") Then", GetHeaderText(IfTag)); + Assert.Equal(3, GetCollapsedHintLineCount(IfTag)); + }); + } + #pragma warning disable CS0618 // Type or member is obsolete private static async Task> GetTagsFromWorkspaceAsync(EditorTestWorkspace workspace) { diff --git a/src/Features/Core/Portable/Structure/Syntax/AbstractBlockStructureProvider.cs b/src/Features/Core/Portable/Structure/Syntax/AbstractBlockStructureProvider.cs index 978561bc7b621..90665355eef58 100644 --- a/src/Features/Core/Portable/Structure/Syntax/AbstractBlockStructureProvider.cs +++ b/src/Features/Core/Portable/Structure/Syntax/AbstractBlockStructureProvider.cs @@ -51,16 +51,19 @@ public override void ProvideBlockStructure(in BlockStructureContext context) // We only collapse the "inner" span which has larger start. spans.Sort(static (x, y) => y.TextSpan.Start.CompareTo(x.TextSpan.Start)); - var lastAddedLine = -1; + var lastAddedLineStart = -1; + var lastAddedLineEnd = -1; var text = context.SyntaxTree.GetText(context.CancellationToken); foreach (var span in spans) { - var line = text.Lines.GetLinePosition(span.TextSpan.Start).Line; - if (line == lastAddedLine) + var lineStart = text.Lines.GetLinePosition(span.TextSpan.Start).Line; + var lineEnd = text.Lines.GetLinePosition(span.TextSpan.End).Line; + if (lineStart == lastAddedLineStart && lastAddedLineEnd == lineEnd) continue; - lastAddedLine = line; + lastAddedLineStart = lineStart; + lastAddedLineEnd = lineEnd; context.Spans.Add(span); } }