コード例 #1
0
        public void Update_NoChanges_InvokesPostChangedEventTwice_NoEffectiveChanges()
        {
            // Arrange
            var textBuffer = new TestTextBuffer(new StringTextSnapshot("Hello World"));
            var called     = 0;

            textBuffer.PostChanged += (s, a) =>
            {
                textBuffer.TryGetHostDocumentSyncVersion(out var version);
                Assert.Equal(1, version);

                called += 1;
            };

            var document = new CSharpVirtualDocument(Uri, textBuffer);

            // Act
            document.Update(Array.Empty <ITextChange>(), hostDocumentVersion: 1);

            // Assert
            Assert.Equal(2, called);
            var text = textBuffer.CurrentSnapshot.GetText();

            Assert.Equal("Hello World", text);
        }
コード例 #2
0
        public override bool TryCreateFor(ITextBuffer hostDocumentBuffer, out VirtualDocument virtualDocument)
        {
            if (hostDocumentBuffer is null)
            {
                throw new ArgumentNullException(nameof(hostDocumentBuffer));
            }

            if (!hostDocumentBuffer.ContentType.IsOfType(RazorLSPConstants.RazorLSPContentTypeName))
            {
                // Another content type we don't care about.
                virtualDocument = null;
                return(false);
            }

            var hostDocumentUri = _fileUriProvider.GetOrCreate(hostDocumentBuffer);

            // Index.cshtml => Index.cshtml__virtual.cs
            var virtualCSharpFilePath = hostDocumentUri.GetAbsoluteOrUNCPath() + RazorLSPConstants.VirtualCSharpFileNameSuffix;
            var virtualCSharpUri      = new Uri(virtualCSharpFilePath);

            var csharpBuffer = _textBufferFactory.CreateTextBuffer();

            _fileUriProvider.AddOrUpdate(csharpBuffer, virtualCSharpUri);
            csharpBuffer.Properties.AddProperty(RazorLSPConstants.ContainedLanguageMarker, true);
            csharpBuffer.Properties.AddProperty(LanguageClientConstants.ClientNamePropertyKey, "RazorCSharp");

            // Create a text document to trigger the C# language server initialization.
            _textDocumentFactory.CreateTextDocument(csharpBuffer, virtualCSharpFilePath);

            csharpBuffer.ChangeContentType(CSharpLSPContentType, editTag: null);

            virtualDocument = new CSharpVirtualDocument(virtualCSharpUri, csharpBuffer);
            return(true);
        }
コード例 #3
0
        public void Update_RecalculatesSnapshot()
        {
            // Arrange
            var replace = new TextChange(new TextSpan(123, 4), "replaced text");
            var edit    = new Mock <ITextEdit>();

            edit.Setup(e => e.Replace(replace.Span.Start, replace.Span.Length, replace.NewText));
            var textBuffer         = new Mock <ITextBuffer>();
            var textBufferSnapshot = Mock.Of <ITextSnapshot>();

            textBuffer.Setup(buffer => buffer.CreateEdit(EditOptions.None, null, It.IsAny <IInviolableEditTag>()))
            .Returns(edit.Object);
            textBuffer.Setup(buffer => buffer.CurrentSnapshot)
            .Returns(() => textBufferSnapshot);
            var editedSnapshot = Mock.Of <ITextSnapshot>();

            edit.Setup(e => e.Apply())
            .Callback(() =>
            {
                textBufferSnapshot = editedSnapshot;
            });
            var document = new CSharpVirtualDocument(Uri, textBuffer.Object);

            // Act
            document.Update(new[] { replace }, hostDocumentVersion: 1);

            // Assert
            Assert.Same(editedSnapshot, document.CurrentSnapshot.Snapshot);
        }
コード例 #4
0
        public void Update_AlwaysSetsHostDocumentSyncVersion_AndUpdatesSnapshot()
        {
            // Arrange
            var textBuffer       = new TestTextBuffer(StringTextSnapshot.Empty);
            var document         = new CSharpVirtualDocument(Uri, textBuffer);
            var originalSnapshot = document.CurrentSnapshot;

            // Act
            document.Update(Array.Empty <ITextChange>(), hostDocumentVersion: 1337);

            // Assert
            Assert.NotSame(originalSnapshot, document.CurrentSnapshot);
            Assert.Equal(1337, document.HostDocumentSyncVersion);
        }
コード例 #5
0
        public void Update_Delete()
        {
            // Arrange
            var textBuffer = new TestTextBuffer(new StringTextSnapshot("Hello World"));
            var delete     = new VisualStudioTextChange(6, 5, string.Empty);
            var document   = new CSharpVirtualDocument(Uri, textBuffer);

            // Act
            document.Update(new[] { delete }, hostDocumentVersion: 1);

            // Assert
            var text = textBuffer.CurrentSnapshot.GetText();

            Assert.Equal("Hello ", text);
        }
コード例 #6
0
        public void Update_Replace()
        {
            // Arrange
            var textBuffer = new TestTextBuffer(new StringTextSnapshot("original"));
            var replace    = new VisualStudioTextChange(0, textBuffer.CurrentSnapshot.Length, "replaced text");
            var document   = new CSharpVirtualDocument(Uri, textBuffer);

            // Act
            document.Update(new[] { replace }, hostDocumentVersion: 1);

            // Assert
            var text = textBuffer.CurrentSnapshot.GetText();

            Assert.Equal(replace.NewText, text);
        }
コード例 #7
0
        public void Update_Insert()
        {
            // Arrange
            var insert     = new VisualStudioTextChange(0, 0, "inserted text");
            var textBuffer = new TestTextBuffer(StringTextSnapshot.Empty);
            var document   = new CSharpVirtualDocument(Uri, textBuffer);

            // Act
            document.Update(new[] { insert }, hostDocumentVersion: 1);

            // Assert
            var text = textBuffer.CurrentSnapshot.GetText();

            Assert.Equal(insert.NewText, text);
        }
コード例 #8
0
        public void Update_MultipleEdits()
        {
            // Arrange
            var textBuffer = new TestTextBuffer(new StringTextSnapshot("Hello World"));
            var replace    = new TextChange(new TextSpan(6, 5), "Replaced");
            var delete     = new TextChange(new TextSpan(0, 6), string.Empty);
            var document   = new CSharpVirtualDocument(Uri, textBuffer);

            // Act
            document.Update(new[] { replace, delete }, hostDocumentVersion: 1);

            // Assert
            var text = textBuffer.CurrentSnapshot.GetText();

            Assert.Equal("Replaced", text);
        }
コード例 #9
0
        public void Update_Delete()
        {
            // Arrange
            var delete = new TextChange(new TextSpan(123, 4), string.Empty);
            var edit   = new Mock <ITextEdit>();

            edit.Setup(e => e.Delete(delete.Span.Start, delete.Span.Length)).Verifiable();
            edit.Setup(e => e.Apply()).Verifiable();
            var textBuffer = CreateTextBuffer(edit.Object);
            var document   = new CSharpVirtualDocument(Uri, textBuffer);

            // Act
            document.Update(new[] { delete }, hostDocumentVersion: 1);

            // Assert
            edit.VerifyAll();
        }
コード例 #10
0
        public void Update_Replace()
        {
            // Arrange
            var replace = new TextChange(new TextSpan(123, 4), "replaced text");
            var edit    = new Mock <ITextEdit>();

            edit.Setup(e => e.Replace(replace.Span.Start, replace.Span.Length, replace.NewText)).Verifiable();
            edit.Setup(e => e.Apply()).Verifiable();
            var textBuffer = CreateTextBuffer(edit.Object);
            var document   = new CSharpVirtualDocument(Uri, textBuffer);

            // Act
            document.Update(new[] { replace }, hostDocumentVersion: 1);

            // Assert
            edit.VerifyAll();
        }
コード例 #11
0
        public void Update_Insert()
        {
            // Arrange
            var insert = new TextChange(new TextSpan(123, 0), "inserted text");
            var edit   = new Mock <ITextEdit>();

            edit.Setup(e => e.Insert(insert.Span.Start, insert.NewText)).Verifiable();
            edit.Setup(e => e.Apply()).Verifiable();
            var textBuffer = CreateTextBuffer(edit.Object);
            var document   = new CSharpVirtualDocument(Uri, textBuffer);

            // Act
            document.Update(new[] { insert }, hostDocumentVersion: 1);

            // Assert
            edit.VerifyAll();
        }
コード例 #12
0
        public void Update_MultipleEdits()
        {
            // Arrange
            var replace = new TextChange(new TextSpan(123, 4), "replaced text");
            var delete  = new TextChange(new TextSpan(123, 4), string.Empty);
            var edit    = new Mock <ITextEdit>();

            edit.Setup(e => e.Delete(delete.Span.Start, delete.Span.Length)).Verifiable();
            edit.Setup(e => e.Replace(replace.Span.Start, replace.Span.Length, replace.NewText)).Verifiable();
            var textBuffer = CreateTextBuffer(edit.Object);

            edit.Setup(e => e.Apply())
            .Returns(textBuffer.CurrentSnapshot).Verifiable();
            var document = new CSharpVirtualDocument(Uri, textBuffer);

            // Act
            document.Update(new[] { replace, delete }, hostDocumentVersion: 1);

            // Assert
            edit.VerifyAll();
        }