Repository: dotnet/roslyn
File: src/EditorFeatures/TestUtilities/GoToAdjacentMember/AbstractGoToAdjacentMemberTests.cs (C#)

Code before:
    protected async Task AssertNavigatedAsync(string code, bool next, SourceCodeKind? sourceCodeKind = null)
    {
        var kinds = sourceCodeKind != null
            ? SpecializedCollections.SingletonEnumerable(sourceCodeKind.Value)
            : [SourceCodeKind.Regular, SourceCodeKind.Script];

        foreach (var kind in kinds)
        {
            using var workspace = EditorTestWorkspace.Create(
                LanguageName,
                compilationOptions: null,
                parseOptions: DefaultParseOptions.WithKind(kind),
                content: code);

            var hostDocument = workspace.DocumentWithCursor;
            var document = workspace.CurrentSolution.GetDocument(hostDocument.Id);
            var parsedDocument = await ParsedDocument.CreateAsync(document, CancellationToken.None);
            Assert.Empty(parsedDocument.SyntaxTree.GetDiagnostics());

            var textView = hostDocument.GetTextView();
            var subjectBuffer = hostDocument.GetTextBuffer();

            if (hostDocument.AnnotatedSpans.TryGetValue("selection", out var annotatedSelection) && annotatedSelection.Any())
            {
                var firstSpan = annotatedSelection.First();
                var lastSpan = annotatedSelection.Last();
                var cursorPosition = hostDocument.CursorPosition ?? firstSpan.Start;

                textView.Selection.Mode = annotatedSelection.Length > 1
                    ? TextSelectionMode.Box
                    : TextSelectionMode.Stream;

                SnapshotPoint boxSelectionStart, boxSelectionEnd;
                bool isReversed;

                if (cursorPosition == firstSpan.Start || cursorPosition == lastSpan.End)
                {
                    boxSelectionStart = new SnapshotPoint(subjectBuffer.CurrentSnapshot, firstSpan.Start);
                    boxSelectionEnd = new SnapshotPoint(subjectBuffer.CurrentSnapshot, lastSpan.End);
                    isReversed = cursorPosition == firstSpan.Start;
                }
                else
                {
                    boxSelectionStart = new SnapshotPoint(subjectBuffer.CurrentSnapshot, firstSpan.End);
                    boxSelectionEnd = new SnapshotPoint(subjectBuffer.CurrentSnapshot, lastSpan.Start);
                    isReversed = cursorPosition == firstSpan.End;
                }

                textView.Selection.Select(new SnapshotSpan(boxSelectionStart, boxSelectionEnd), isReversed);
            }
            else

Review comment (performance):
We'd just always have one, right? So can we just do annotatedSelection.SingleOrDefault() to force it? Then we can just remove all of the complexity around box selection.

Code after:
    protected async Task AssertNavigatedAsync(string code, bool next, SourceCodeKind? sourceCodeKind = null)
    {
        var kinds = sourceCodeKind != null
            ? SpecializedCollections.SingletonEnumerable(sourceCodeKind.Value)
            : [SourceCodeKind.Regular, SourceCodeKind.Script];

        foreach (var kind in kinds)
        {
            using var workspace = EditorTestWorkspace.Create(
                LanguageName,
                compilationOptions: null,
                parseOptions: DefaultParseOptions.WithKind(kind),
                content: code);

            var hostDocument = workspace.DocumentWithCursor;
            var document = workspace.CurrentSolution.GetDocument(hostDocument.Id);
            var parsedDocument = await ParsedDocument.CreateAsync(document, CancellationToken.None);
            Assert.Empty(parsedDocument.SyntaxTree.GetDiagnostics());

            var textView = hostDocument.GetTextView();
            var subjectBuffer = hostDocument.GetTextBuffer();

            if (hostDocument.AnnotatedSpans.TryGetValue("selection", out var annotatedSelection) && annotatedSelection.Any())
            {
                var span = annotatedSelection.SingleOrDefault();
                var cursorPosition = hostDocument.CursorPosition ?? span.Start;

                textView.Selection.Mode = TextSelectionMode.Stream;

                var snapshotSpan = new SnapshotSpan(subjectBuffer.CurrentSnapshot, span.Start, span.Length);
                var isReversed = cursorPosition == span.Start;

                textView.Selection.Select(snapshotSpan, isReversed);
            }
            else
            {
                textView.Caret.MoveTo(new SnapshotPoint(subjectBuffer.CurrentSnapshot, hostDocument.CursorPosition.Value));
            }

            var handler = workspace.ExportProvider.GetCommandHandler<GoToAdjacentMemberCommandHandler>(PredefinedCommandHandlerNames.GoToAdjacentMember, ContentTypeNames.RoslynContentType);

            EditorCommandArgs args = next
                ? new GoToNextMemberCommandArgs(textView, subjectBuffer)
                : new GoToPreviousMemberCommandArgs(textView, subjectBuffer);

            var executed = next
                ? handler.ExecuteCommand((GoToNextMemberCommandArgs)args, TestCommandExecutionContext.Create())
                : handler.ExecuteCommand((GoToPreviousMemberCommandArgs)args, TestCommandExecutionContext.Create());

            Assert.True(executed, "Command handler should have executed.");