Esempio n. 1
0
        internal static void TestIndentation(int point, int?expectedIndentation, ITextView textView, TestHostDocument subjectDocument)
        {
            var textUndoHistory         = new Mock <ITextUndoHistoryRegistry>();
            var editorOperationsFactory = new Mock <IEditorOperationsFactoryService>();
            var editorOperations        = new Mock <IEditorOperations>();

            editorOperationsFactory.Setup(x => x.GetEditorOperations(textView)).Returns(editorOperations.Object);

            var snapshot = subjectDocument.TextBuffer.CurrentSnapshot;
            var indentationLineFromBuffer = snapshot.GetLineFromPosition(point);

            var commandHandler = new SmartTokenFormatterCommandHandler(textUndoHistory.Object, editorOperationsFactory.Object);

            commandHandler.ExecuteCommand(new ReturnKeyCommandArgs(textView, subjectDocument.TextBuffer), () => { });
            var newSnapshot = subjectDocument.TextBuffer.CurrentSnapshot;

            int?actualIndentation;

            if (newSnapshot.Version.VersionNumber > snapshot.Version.VersionNumber)
            {
                actualIndentation = newSnapshot.GetLineFromLineNumber(indentationLineFromBuffer.LineNumber).GetFirstNonWhitespaceOffset();
            }
            else
            {
                var provider = new SmartIndent(textView);
                actualIndentation = provider.GetDesiredIndentation(indentationLineFromBuffer);
            }

            Assert.Equal(expectedIndentation, actualIndentation.Value);
        }
Esempio n. 2
0
        protected void TestIndentation(
            int point, int?expectedIndentation, ITextView textView, TestHostDocument subjectDocument)
        {
            var textUndoHistory         = new Mock <ITextUndoHistoryRegistry>();
            var editorOperationsFactory = new Mock <IEditorOperationsFactoryService>();
            var editorOperations        = new Mock <IEditorOperations>();

            editorOperationsFactory.Setup(x => x.GetEditorOperations(textView)).Returns(editorOperations.Object);

            var snapshot = subjectDocument.GetTextBuffer().CurrentSnapshot;
            var indentationLineFromBuffer = snapshot.GetLineFromPosition(point);

            var provider          = new SmartIndent(textView);
            var actualIndentation = provider.GetDesiredIndentation(indentationLineFromBuffer);

            Assert.Equal(expectedIndentation, actualIndentation.Value);
        }
Esempio n. 3
0
        protected void TestIndentation(
            TestWorkspace workspace, int indentationLine,
            int?expectedIndentation, int?expectedBlankLineIndentation)
        {
            var snapshot    = workspace.Documents.First().TextBuffer.CurrentSnapshot;
            var bufferGraph = new Mock <IBufferGraph>(MockBehavior.Strict);

            bufferGraph.Setup(x => x.MapUpToSnapshot(It.IsAny <SnapshotPoint>(),
                                                     It.IsAny <PointTrackingMode>(),
                                                     It.IsAny <PositionAffinity>(),
                                                     It.IsAny <ITextSnapshot>()))
            .Returns <SnapshotPoint, PointTrackingMode, PositionAffinity, ITextSnapshot>((p, m, a, s) =>
            {
                if (workspace.Services.GetService <IHostDependentFormattingRuleFactoryService>() is TestFormattingRuleFactoryServiceFactory.Factory factory && factory.BaseIndentation != 0 && factory.TextSpan.Contains(p.Position))
                {
                    var line            = p.GetContainingLine();
                    var projectedOffset = line.GetFirstNonWhitespaceOffset().Value - factory.BaseIndentation;
                    return(new SnapshotPoint(p.Snapshot, p.Position - projectedOffset));
                }

                return(p);
            });

            var projectionBuffer = new Mock <ITextBuffer>(MockBehavior.Strict);

            projectionBuffer.Setup(x => x.ContentType.DisplayName).Returns("None");

            var textView = new Mock <ITextView>(MockBehavior.Strict);

            textView.Setup(x => x.Options).Returns(TestEditorOptions.Instance);
            textView.Setup(x => x.BufferGraph).Returns(bufferGraph.Object);
            textView.SetupGet(x => x.TextSnapshot.TextBuffer).Returns(projectionBuffer.Object);

            var provider = new SmartIndent(textView.Object);

            var indentationLineFromBuffer = snapshot.GetLineFromLineNumber(indentationLine);
            var actualIndentation         = provider.GetDesiredIndentation(indentationLineFromBuffer);

            Assert.Equal(expectedIndentation, actualIndentation);

            TestBlankLineIndentationService(
                workspace, textView.Object, indentationLine,
                expectedBlankLineIndentation ?? expectedIndentation.Value);
        }