internal override bool TryGetSmartIndenter(ITextBuffer textBuffer, out BraceSmartIndenter braceSmartIndenter)
        {
            if (textBuffer == null)
            {
                throw new ArgumentNullException(nameof(textBuffer));
            }

            if (!textBuffer.IsRazorBuffer())
            {
                braceSmartIndenter = null;
                return(false);
            }

            var textBufferInitialized = TryInitializeTextBuffer(textBuffer);

            if (!textBufferInitialized)
            {
                braceSmartIndenter = null;
                return(false);
            }

            if (!textBuffer.Properties.TryGetProperty(typeof(BraceSmartIndenter), out braceSmartIndenter))
            {
                Debug.Fail("Brace smart indenter should have been stored on the text buffer during initialization.");
                return(false);
            }

            return(true);
        }
        public void TextBuffer_OnPostChanged_IndentsInbetweenDirectiveBlockBraces()
        {
            // Arrange
            var change              = Environment.NewLine;
            var initialSnapshot     = new StringTextSnapshot("    @functions {}");
            var afterChangeSnapshot = new StringTextSnapshot("    @functions {" + change + "}");
            var edit = new TestEdit(16, 0, initialSnapshot, afterChangeSnapshot, change);
            var expectedIndentResult = "    @functions {" + change + change + "    }";

            var            caret           = CreateCaretFrom(16 + change.Length, afterChangeSnapshot);
            TestTextBuffer textBuffer      = null;
            var            focusedTextView = CreateFocusedTextView(() => textBuffer, caret);
            var            documentTracker = CreateDocumentTracker(() => textBuffer, focusedTextView);

            textBuffer = CreateTextBuffer(initialSnapshot, documentTracker);
            var codeDocumentProvider    = CreateCodeDocumentProvider(initialSnapshot.Content);
            var editorOperationsFactory = CreateOperationsFactoryService();

            using var braceSmartIndenter = new BraceSmartIndenter(Dispatcher, documentTracker, codeDocumentProvider, editorOperationsFactory);

            // Act
            textBuffer.ApplyEdit(edit);

            // Assert
            Assert.Equal(expectedIndentResult, ((StringTextSnapshot)textBuffer.CurrentSnapshot).Content);
        }
        public void TextBuffer_OnPostChanged_DoesNotIndentJavaScript()
        {
            // Arrange
            var change              = Environment.NewLine;
            var initialSnapshot     = new StringTextSnapshot("    <script>function foo() {}</script>");
            var afterChangeSnapshot = new StringTextSnapshot("    <script>function foo() {" + change + "}</script>");
            var edit = new TestEdit(28, 0, initialSnapshot, afterChangeSnapshot, change);

            var            caret           = CreateCaretFrom(28 + change.Length, afterChangeSnapshot);
            TestTextBuffer textBuffer      = null;
            var            focusedTextView = CreateFocusedTextView(() => textBuffer, caret);
            var            documentTracker = CreateDocumentTracker(() => textBuffer, focusedTextView);

            textBuffer = CreateTextBuffer(initialSnapshot, documentTracker);
            var codeDocumentProvider    = CreateCodeDocumentProvider(initialSnapshot.Content);
            var editorOperationsFactory = CreateOperationsFactoryService();

            using var braceSmartIndenter = new BraceSmartIndenter(Dispatcher, documentTracker, codeDocumentProvider, editorOperationsFactory);

            // Act
            textBuffer.ApplyEdit(edit);

            // Assert
            Assert.Equal(afterChangeSnapshot.Content, ((StringTextSnapshot)textBuffer.CurrentSnapshot).Content);
        }
        public void TextBuffer_OnPostChanged_IndentsInbetweenBraces_OneLevelOfIndentation()
        {
            // Arrange
            var change              = "\r";
            var initialSnapshot     = new StringTextSnapshot("    @{ }");
            var afterChangeSnapshot = new StringTextSnapshot("    @{ " + change + "}");
            var edit = new TestEdit(7, 0, initialSnapshot, afterChangeSnapshot, change);
            var expectedIndentResult = "    @{ " + change + change + "    }";

            var            caret           = CreateCaretFrom(7 + change.Length, afterChangeSnapshot);
            TestTextBuffer textBuffer      = null;
            var            focusedTextView = CreateFocusedTextView(() => textBuffer, caret);
            var            documentTracker = CreateDocumentTracker(() => textBuffer, focusedTextView);

            textBuffer = CreateTextBuffer(initialSnapshot, documentTracker);
            var codeDocumentProvider    = CreateCodeDocumentProvider(initialSnapshot.Content);
            var editorOperationsFactory = CreateOperationsFactoryService();

            using var braceSmartIndenter = new BraceSmartIndenter(JoinableTaskFactory.Context, documentTracker, codeDocumentProvider, editorOperationsFactory);

            // Act
            textBuffer.ApplyEdit(edit);

            // Assert
            Assert.Equal(expectedIndentResult, ((StringTextSnapshot)textBuffer.CurrentSnapshot).Content);
        }
示例#5
0
        public void RestoreCaretTo_PlacesCursorAtProvidedPosition()
        {
            // Arrange
            var initialSnapshot = new StringTextSnapshot("@{ \n\n}");
            var bufferPosition  = new VirtualSnapshotPoint(initialSnapshot, 4);
            var caret           = new Mock <ITextCaret>(MockBehavior.Strict);

            caret.Setup(c => c.MoveTo(It.IsAny <SnapshotPoint>()))
            .Callback <SnapshotPoint>(point =>
            {
                Assert.Equal(3, point.Position);
                Assert.Same(initialSnapshot, point.Snapshot);
            }).Returns(new CaretPosition(bufferPosition, new Mock <IMappingPoint>(MockBehavior.Strict).Object, PositionAffinity.Predecessor));
            ITextBuffer textBuffer      = null;
            var         textView        = CreateFocusedTextView(() => textBuffer, caret.Object);
            var         documentTracker = CreateDocumentTracker(() => textBuffer, textView);

            textBuffer = CreateTextBuffer(initialSnapshot, documentTracker);

            // Act
            BraceSmartIndenter.RestoreCaretTo(3, textView);

            // Assert
            caret.VerifyAll();
        }
示例#6
0
        private static DefaultRazorEditorFactoryService CreateFactoryService(
            VisualStudioDocumentTracker documentTracker = null,
            VisualStudioRazorParser parser   = null,
            BraceSmartIndenter smartIndenter = null)
        {
            documentTracker ??= Mock.Of <VisualStudioDocumentTracker>(MockBehavior.Strict);
            parser ??= Mock.Of <VisualStudioRazorParser>(MockBehavior.Strict);
            smartIndenter ??= Mock.Of <BraceSmartIndenter>(MockBehavior.Strict);

            var documentTrackerFactory = Mock.Of <VisualStudioDocumentTrackerFactory>(f => f.Create(It.IsAny <ITextBuffer>()) == documentTracker, MockBehavior.Strict);
            var parserFactory          = Mock.Of <VisualStudioRazorParserFactory>(f => f.Create(It.IsAny <VisualStudioDocumentTracker>()) == parser, MockBehavior.Strict);
            var smartIndenterFactory   = Mock.Of <BraceSmartIndenterFactory>(f => f.Create(It.IsAny <VisualStudioDocumentTracker>()) == smartIndenter, MockBehavior.Strict);

            var services = TestServices.Create(new ILanguageService[]
            {
                documentTrackerFactory,
                parserFactory,
                smartIndenterFactory
            });

            Workspace workspace         = TestWorkspace.Create(services);
            var       workspaceAccessor = new Mock <VisualStudioWorkspaceAccessor>(MockBehavior.Strict);

            workspaceAccessor.Setup(p => p.TryGetWorkspace(It.IsAny <ITextBuffer>(), out workspace))
            .Returns(true);

            var factoryService = new DefaultRazorEditorFactoryService(workspaceAccessor.Object);

            return(factoryService);
        }
示例#7
0
        public void AfterClosingBrace_ContentAfterBrace_ReturnsFalse()
        {
            // Arrange
            var fileSnapshot   = new StringTextSnapshot("@functions\n{a\n}");
            var changePosition = 13;
            var line           = fileSnapshot.GetLineFromPosition(changePosition);

            // Act & Assert
            Assert.False(BraceSmartIndenter.BeforeClosingBrace(0, line));
        }
示例#8
0
        public void AfterClosingBrace_BraceBeforePosition_ReturnsTrue(string fileContent)
        {
            // Arrange
            var fileSnapshot   = new StringTextSnapshot(fileContent);
            var changePosition = fileContent.Length - 3 /* \n} */;
            var line           = fileSnapshot.GetLineFromPosition(changePosition);

            // Act & Assert
            Assert.True(BraceSmartIndenter.AfterOpeningBrace(line.Length - 1, line));
        }
示例#9
0
        public void BeforeClosingBrace_ContentPriorToBrace_ReturnsFalse()
        {
            // Arrange
            var fileSnapshot   = new StringTextSnapshot("@functions\n{\na}");
            var changePosition = 12;
            var line           = fileSnapshot.GetLineFromPosition(changePosition + 1 /* \n */);

            // Act & Assert
            Assert.False(BraceSmartIndenter.BeforeClosingBrace(0, line));
        }
示例#10
0
        public void BeforeClosingBrace_BraceAfterPosition_ReturnsTrue(string fileContent)
        {
            // Arrange
            var fileSnapshot   = new StringTextSnapshot(fileContent);
            var changePosition = 12;
            var line           = fileSnapshot.GetLineFromPosition(changePosition + 1 /* \n */);

            // Act & Assert
            Assert.True(BraceSmartIndenter.BeforeClosingBrace(0, line));
        }
示例#11
0
        public void SurroundedByInvalidContent_OnlyPreviousMetacode_ReturnsTrue()
        {
            // Arrange
            var span = ExtractSpan(2, "@{<p>");

            // Act
            var result = BraceSmartIndenter.SurroundedByInvalidContent(span);

            // Assert
            Assert.True(result);
        }
示例#12
0
        public void SurroundedByInvalidContent_MetacodeSurroundings_ReturnsFalse()
        {
            // Arrange
            var span = ExtractSpan(2, "@{}");

            // Act
            var result = BraceSmartIndenter.SurroundedByInvalidContent(span);

            // Assert
            Assert.False(result);
        }
示例#13
0
        public void IsUnlinkedSpan_NullOwner_ReturnsTrue()
        {
            // Arrange
            SyntaxNode owner = null;

            // Act
            var result = BraceSmartIndenter.IsUnlinkedSpan(owner);

            // Assert
            Assert.True(result);
        }
示例#14
0
        public void IsUnlinkedSpan_NullNext_ReturnsTrue()
        {
            // Arrange
            var span = ExtractSpan(3, "@{}");

            // Act
            var result = BraceSmartIndenter.IsUnlinkedSpan(span);

            // Assert
            Assert.True(result);
        }
示例#15
0
        public void ContainsInvalidContent_NonWhitespaceMarker_ReturnsTrue()
        {
            // Arrange
            var span = ExtractSpan(2, "@{ if}");

            // Act
            var result = BraceSmartIndenter.ContainsInvalidContent(span);

            // Assert
            Assert.True(result);
        }
示例#16
0
        public void AtApplicableRazorBlock_NestedIfBlock_ReturnsFalse()
        {
            // Arrange
            var syntaxTree = GetSyntaxTree("@{ if (true) { } }");

            // Act
            var result = BraceSmartIndenter.AtApplicableRazorBlock(14, syntaxTree);

            // Assert
            Assert.False(result);
        }
示例#17
0
        public void ContainsInvalidContent_NewLineSpan_ReturnsFalse()
        {
            // Arrange
            var span = ExtractSpan(2, "@{" + Environment.NewLine + "}");

            // Act
            var result = BraceSmartIndenter.ContainsInvalidContent(span);

            // Assert
            Assert.False(result);
        }
示例#18
0
        public void ContainsInvalidContent_WhitespaceSpan_ReturnsFalse()
        {
            // Arrange
            var span = ExtractSpan(2, "@{ }");

            // Act
            var result = BraceSmartIndenter.ContainsInvalidContent(span);

            // Assert
            Assert.False(result);
        }
示例#19
0
        public void AtApplicableRazorBlock_FunctionsBlock_ReturnsTrue()
        {
            // Arrange
            var syntaxTree = GetSyntaxTree("@functions { }");

            // Act
            var result = BraceSmartIndenter.AtApplicableRazorBlock(13, syntaxTree);

            // Assert
            Assert.True(result);
        }
示例#20
0
        public void AtApplicableRazorBlock_ExplicitCodeBlock_ReturnsTrue()
        {
            // Arrange
            var syntaxTree = GetSyntaxTree("@{ }");

            // Act
            var result = BraceSmartIndenter.AtApplicableRazorBlock(3, syntaxTree);

            // Assert
            Assert.True(result);
        }
示例#21
0
        public void TextBuffer_OnChanged_NoopsIfNoChanges()
        {
            // Arrange
            var editorOperationsFactory = new Mock <IEditorOperationsFactoryService>();
            var changeCollection        = new TestTextChangeCollection();
            var textContentChangeArgs   = new TestTextContentChangedEventArgs(changeCollection);
            var documentTracker         = CreateDocumentTracker(() => Mock.Of <ITextBuffer>(), Mock.Of <ITextView>());
            var braceSmartIndenter      = new BraceSmartIndenter(Dispatcher, documentTracker, editorOperationsFactory.Object);

            // Act & Assert
            braceSmartIndenter.TextBuffer_OnChanged(null, textContentChangeArgs);
        }
示例#22
0
        public void AtValidContentKind_ReturnsFalseAtMarkup()
        {
            // Arrange
            var syntaxTree     = RazorSyntaxTree.Parse(TestRazorSourceDocument.Create("<p></p>"));
            var changePosition = 2;

            // Act
            var result = BraceSmartIndenter.AtValidContentKind(changePosition, syntaxTree);

            // Assert
            Assert.False(result);
        }
示例#23
0
        public void AtApplicableRazorBlock_AtMarkup_ReturnsFalse()
        {
            // Arrange
            var syntaxTree     = RazorSyntaxTree.Parse(TestRazorSourceDocument.Create("<p></p>"));
            var changePosition = 2;

            // Act
            var result = BraceSmartIndenter.AtApplicableRazorBlock(changePosition, syntaxTree);

            // Assert
            Assert.False(result);
        }
示例#24
0
        public void AtValidContentKind_ReturnsTrueAtCode()
        {
            // Arrange
            var syntaxTree     = RazorSyntaxTree.Parse(TestRazorSourceDocument.Create("@{}"));
            var changePosition = 2;

            // Act
            var result = BraceSmartIndenter.AtValidContentKind(changePosition, syntaxTree);

            // Assert
            Assert.True(result);
        }
示例#25
0
        public void AtApplicableRazorBlock_AtExplicitCodeBlocksCode_ReturnsTrue()
        {
            // Arrange
            var syntaxTree     = RazorSyntaxTree.Parse(TestRazorSourceDocument.Create("@{}"));
            var changePosition = 2;

            // Act
            var result = BraceSmartIndenter.AtApplicableRazorBlock(changePosition, syntaxTree);

            // Assert
            Assert.True(result);
        }
示例#26
0
        public void AtApplicableRazorBlock_WhenNoOwner_ReturnsFalse()
        {
            // Arrange
            var syntaxTree     = RazorSyntaxTree.Parse(TestRazorSourceDocument.Create("@DateTime.Now"));
            var changePosition = 14; // 1 after the end of the content

            // Act
            var result = BraceSmartIndenter.AtApplicableRazorBlock(changePosition, syntaxTree);

            // Assert
            Assert.False(result);
        }
示例#27
0
        public override BraceSmartIndenter Create(VisualStudioDocumentTracker documentTracker)
        {
            if (documentTracker is null)
            {
                throw new ArgumentNullException(nameof(documentTracker));
            }

            _joinableTaskContext.AssertUIThread();

            var braceSmartIndenter = new BraceSmartIndenter(_joinableTaskContext, documentTracker, _codeDocumentProvider, _editorOperationsFactory);

            return(braceSmartIndenter);
        }
        public override BraceSmartIndenter Create(VisualStudioDocumentTracker documentTracker)
        {
            if (documentTracker == null)
            {
                throw new ArgumentNullException(nameof(documentTracker));
            }

            _dispatcher.AssertForegroundThread();

            var braceSmartIndenter = new BraceSmartIndenter(_dispatcher, documentTracker, _editorOperationsFactory);

            return(braceSmartIndenter);
        }
示例#29
0
        public void TextBuffer_OnChanged_NoopsIfChangesThatResultInNoChange()
        {
            // Arrange
            var initialSnapshot         = new StringTextSnapshot("Hello World");
            var textBuffer              = new TestTextBuffer(initialSnapshot);
            var edit                    = new TestEdit(0, 0, initialSnapshot, 0, initialSnapshot, string.Empty);
            var editorOperationsFactory = new Mock <IEditorOperationsFactoryService>();
            var documentTracker         = CreateDocumentTracker(() => textBuffer, Mock.Of <ITextView>());
            var braceSmartIndenter      = new BraceSmartIndenter(Dispatcher, documentTracker, editorOperationsFactory.Object);

            // Act & Assert
            textBuffer.ApplyEdits(edit, edit);
        }
示例#30
0
        public void AtApplicableRazorBlock_AtMetacode_ReturnsTrue()
        {
            // Arrange
            var parseOptions   = RazorParserOptions.Create(options => options.Directives.Add(FunctionsDirective.Directive));
            var syntaxTree     = RazorSyntaxTree.Parse(TestRazorSourceDocument.Create("@functions {}"), parseOptions);
            var changePosition = 12;

            // Act
            var result = BraceSmartIndenter.AtApplicableRazorBlock(changePosition, syntaxTree);

            // Assert
            Assert.True(result);
        }