예제 #1
0
        public void TryGetFromBuffer_PrefersVisualStudioRazorParserIfRazorEditorParserIsAvailable()
        {
            // Arrange
            var properties           = new PropertyCollection();
            var expectedCodeDocument = TestRazorCodeDocument.Create("Hello World");
            var parser = new VisualStudioRazorParser(expectedCodeDocument);

            properties.AddProperty(typeof(VisualStudioRazorParser), parser);
            var unexpectedCodeDocument = TestRazorCodeDocument.Create("Unexpected");
            var legacyParser           = new RazorEditorParser(unexpectedCodeDocument);

            properties.AddProperty(typeof(RazorEditorParser), legacyParser);
            var textBuffer = new Mock <ITextBuffer>();

            textBuffer.Setup(buffer => buffer.Properties)
            .Returns(properties);
            var provider = new DefaultVisualStudioCodeDocumentProvider();

            // Act
            var result = provider.TryGetFromBuffer(textBuffer.Object, out var codeDocument);

            // Assert
            Assert.True(result);
            Assert.Same(expectedCodeDocument, codeDocument);
        }
        // [Fact] Silent skip to avoid warnings. Skipping until we can control the parser more directly.
        private void BufferChangeStartsFullReparseIfChangeOverlapsMultipleSpans()
        {
            // Arrange
            var original   = new StringTextSnapshot("Foo @bar Baz");
            var testBuffer = new TestTextBuffer(original);

            using (var parser = new VisualStudioRazorParser(Dispatcher, testBuffer, CreateTemplateEngine(), TestLinePragmaFileName, new TestCompletionBroker()))
            {
                parser._idleTimer.Interval = 100;
                var changed       = new StringTextSnapshot("Foo @bap Daz");
                var edit          = new TestEdit(7, 3, original, 3, changed, "p D");
                var parseComplete = new ManualResetEventSlim();
                var parseCount    = 0;
                parser.DocumentStructureChanged += (s, a) =>
                {
                    Interlocked.Increment(ref parseCount);
                    parseComplete.Set();
                };

                // Act - 1
                testBuffer.ApplyEdit(edit);
                DoWithTimeoutIfNotDebugging(parseComplete.Wait); // Wait for the parse to finish

                // Assert - 1
                Assert.Equal(1, parseCount);
                parseComplete.Reset();

                // Act - 2
                testBuffer.ApplyEdit(edit);

                // Assert - 2
                DoWithTimeoutIfNotDebugging(parseComplete.Wait);
                Assert.Equal(2, parseCount);
            }
        }
예제 #3
0
        private static DefaultRazorEditorFactoryService CreateFactoryService(
            VisualStudioDocumentTracker documentTracker = null,
            VisualStudioRazorParser parser   = null,
            BraceSmartIndenter smartIndenter = null)
        {
            documentTracker = documentTracker ?? Mock.Of <VisualStudioDocumentTracker>();
            parser          = parser ?? Mock.Of <VisualStudioRazorParser>();
            smartIndenter   = smartIndenter ?? Mock.Of <BraceSmartIndenter>();

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

            return(factoryService);
        }
        public override bool TryGetParser(ITextBuffer textBuffer, out VisualStudioRazorParser parser)
        {
            if (textBuffer == null)
            {
                throw new ArgumentNullException(nameof(textBuffer));
            }

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

            EnsureTextBufferInitialized(textBuffer);

            if (!textBuffer.Properties.TryGetProperty(typeof(VisualStudioRazorParser), out parser))
            {
                Debug.Fail("Parser should have been stored on the text buffer during initialization.");
                return(false);
            }

            return(true);
        }
            public TestParserManager(VisualStudioRazorParser parser)
            {
                _parserComplete  = new ManualResetEventSlim();
                _reparseComplete = new ManualResetEventSlim();
                _testBuffer      = (TestTextBuffer)parser._textBuffer;
                ParseCount       = 0;

                // Change idle delay to be huge in order to enable us to take control of when idle methods fire.
                parser._idleTimer.Interval = TimeSpan.FromMinutes(2).TotalMilliseconds;
                _parser = parser;
                parser.DocumentStructureChanged += (sender, args) =>
                {
                    Interlocked.Increment(ref ParseCount);
                    _parserComplete.Set();

                    if (args.SourceChange == null)
                    {
                        // Reparse occurred
                        _reparseComplete.Set();
                    }

                    CurrentSyntaxTree = args.CodeDocument.GetSyntaxTree();
                };
            }
        private TestParserManager CreateParserManager(ITextSnapshot originalSnapshot, int idleDelay = 50)
        {
            var parser = new VisualStudioRazorParser(Dispatcher, new TestTextBuffer(originalSnapshot), CreateTemplateEngine(), TestLinePragmaFileName, new TestCompletionBroker());

            return(new TestParserManager(parser));
        }