Skip to content

Commit

Permalink
Merge pull request #74413 from Cosifne/dev/shech/FixMissingCollapse
Browse files Browse the repository at this point in the history
Also checking line end when block structure is filtered
  • Loading branch information
Cosifne authored and arkalyanms committed Jul 22, 2024
1 parent d1deaa3 commit b2e7c06
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
34 changes: 34 additions & 0 deletions src/EditorFeatures/Test/Structure/StructureTaggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.Structure;
Expand Down Expand Up @@ -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<List<IContainerStructureTag>> GetTagsFromWorkspaceAsync(EditorTestWorkspace workspace)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,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);
}
}
Expand Down

0 comments on commit b2e7c06

Please sign in to comment.