Example #1
0
        /// <summary>
        /// verifyUndo is needed because of https://github.com/dotnet/roslyn/issues/28033
        /// Most tests will continue to verifyUndo, but select tests will skip it due to
        /// this known test infrastructure issure. This bug does not represent a product
        /// failure.
        /// </summary>
        private static void TestWorker(
            string inputMarkup,
            string expectedOutputMarkup,
            Action callback,
            bool verifyUndo         = true,
            IndentStyle indentStyle = IndentStyle.Smart,
            bool useTabs            = false)
        {
            using var workspace = TestWorkspace.CreateCSharp(inputMarkup);
            workspace.TryApplyChanges(workspace.CurrentSolution.WithOptions(workspace.Options
                                                                            .WithChangedOption(SmartIndent, LanguageNames.CSharp, indentStyle)
                                                                            .WithChangedOption(UseTabs, LanguageNames.CSharp, useTabs)));

            if (useTabs && expectedOutputMarkup != null)
            {
                Assert.Contains("\t", expectedOutputMarkup);
            }

            var document = workspace.Documents.Single();
            var view     = document.GetTextView();

            var originalSnapshot   = view.TextBuffer.CurrentSnapshot;
            var originalSelections = document.SelectedSpans;

            var snapshotSpans = new List <SnapshotSpan>();

            foreach (var selection in originalSelections)
            {
                snapshotSpans.Add(selection.ToSnapshotSpan(originalSnapshot));
            }
            view.SetMultiSelection(snapshotSpans);

            var undoHistoryRegistry = workspace.GetService <ITextUndoHistoryRegistry>();
            var commandHandler      = workspace.ExportProvider.GetCommandHandler <SplitStringLiteralCommandHandler>(nameof(SplitStringLiteralCommandHandler));

            if (!commandHandler.ExecuteCommand(new ReturnKeyCommandArgs(view, view.TextBuffer), TestCommandExecutionContext.Create()))
            {
                callback();
            }

            if (expectedOutputMarkup != null)
            {
                MarkupTestFile.GetSpans(expectedOutputMarkup,
                                        out var expectedOutput, out ImmutableArray <TextSpan> expectedSpans);

                Assert.Equal(expectedOutput, view.TextBuffer.CurrentSnapshot.AsText().ToString());
                Assert.Equal(expectedSpans.First().Start, view.Caret.Position.BufferPosition.Position);

                if (verifyUndo)
                {
                    // Ensure that after undo we go back to where we were to begin with.
                    var history = undoHistoryRegistry.GetHistory(document.GetTextBuffer());
                    history.Undo(count: originalSelections.Count);

                    var currentSnapshot = document.GetTextBuffer().CurrentSnapshot;
                    Assert.Equal(originalSnapshot.GetText(), currentSnapshot.GetText());
                    Assert.Equal(originalSelections.First().Start, view.Caret.Position.BufferPosition.Position);
                }
            }
        }
        private async Task AssertIndentNotUsingSmartTokenFormatterButUsingIndenterAsync(
            string code,
            int indentationLine,
            int?expectedIndentation,
            bool useTabs,
            IndentStyle indentStyle)
        {
            // create tree service
            using var workspace = TestWorkspace.CreateCSharp(code);
            workspace.TryApplyChanges(workspace.CurrentSolution.WithOptions(workspace.Options
                                                                            .WithChangedOption(SmartIndent, LanguageNames.CSharp, indentStyle)
                                                                            .WithChangedOption(UseTabs, LanguageNames.CSharp, useTabs)));
            var hostdoc  = workspace.Documents.First();
            var buffer   = hostdoc.GetTextBuffer();
            var snapshot = buffer.CurrentSnapshot;

            var line = snapshot.GetLineFromLineNumber(indentationLine);

            var document = workspace.CurrentSolution.GetDocument(hostdoc.Id);

            var root = (await document.GetSyntaxRootAsync()) as CompilationUnitSyntax;

            var optionService = workspace.Services.GetRequiredService <IOptionService>();

            Assert.False(
                CSharpIndentationService.ShouldUseSmartTokenFormatterInsteadOfIndenter(
                    Formatter.GetDefaultFormattingRules(workspace, root.Language),
                    root, line.AsTextLine(), optionService, await document.GetOptionsAsync(), out _));

            TestIndentation(workspace, indentationLine, expectedIndentation);
        }
 private async Task AssertIndentNotUsingSmartTokenFormatterButUsingIndenterAsync(
     string code,
     int indentationLine,
     int?expectedIndentation,
     IndentStyle indentStyle = IndentStyle.Smart)
 {
     await AssertIndentNotUsingSmartTokenFormatterButUsingIndenterAsync(code, indentationLine, expectedIndentation, useTabs : false, indentStyle).ConfigureAwait(false);
     await AssertIndentNotUsingSmartTokenFormatterButUsingIndenterAsync(code.Replace("    ", "\t"), indentationLine, expectedIndentation, useTabs : true, indentStyle).ConfigureAwait(false);
 }
Example #4
0
 /// <summary>
 /// verifyUndo is needed because of https://github.com/dotnet/roslyn/issues/28033
 /// Most tests will continue to verifyUndo, but select tests will skip it due to
 /// this known test infrastructure issure. This bug does not represent a product
 /// failure.
 /// </summary>
 private static void TestHandled(
     string inputMarkup, string expectedOutputMarkup,
     bool verifyUndo = true, IndentStyle indentStyle = IndentStyle.Smart,
     bool useTabs    = false)
 {
     TestWorker(
         inputMarkup, expectedOutputMarkup,
         callback: () =>
     {
         Assert.True(false, "Should not reach here.");
     },
         verifyUndo, indentStyle, useTabs);
 }