public void GetMinimalTextChanges_ReturnsExpectedResults()
        {
            // Arrange
            var oldText = SourceText.From(@"
<div>
  Hello!
</div>
".Replace(Environment.NewLine, "\r\n", StringComparison.Ordinal));
            var newText = SourceText.From(@"
<div>
  Hola!
</div>".Replace(Environment.NewLine, "\r\n", StringComparison.Ordinal));

            // Act 1
            var characterChanges = SourceTextDiffer.GetMinimalTextChanges(oldText, newText, lineDiffOnly: false);

            // Assert 1
            Assert.Collection(characterChanges,
                              change => Assert.Equal(new TextChange(TextSpan.FromBounds(12, 13), "o"), change),
                              change => Assert.Equal(new TextChange(TextSpan.FromBounds(14, 16), "a"), change),
                              change => Assert.Equal(new TextChange(TextSpan.FromBounds(25, 27), string.Empty), change));

            // Act 2
            var lineChanges = SourceTextDiffer.GetMinimalTextChanges(oldText, newText, lineDiffOnly: true);

            // Assert 2
            var change = Assert.Single(lineChanges);

            Assert.Equal(new TextChange(TextSpan.FromBounds(9, 27), "  Hola!\r\n</div>"), change);
        }
Ejemplo n.º 2
0
        public static IReadOnlyList <TextChange> GetMinimalTextChanges(SourceText oldText, SourceText newText, bool lineDiffOnly = true)
        {
            if (oldText is null)
            {
                throw new ArgumentNullException(nameof(oldText));
            }

            if (newText is null)
            {
                throw new ArgumentNullException(nameof(newText));
            }

            if (oldText.ContentEquals(newText))
            {
                return(Array.Empty <TextChange>());
            }
            else if (oldText.Length == 0 || newText.Length == 0)
            {
                return(newText.GetTextChanges(oldText));
            }

            var differ = new SourceTextDiffer(oldText, newText, lineDiffOnly);
            var edits  = differ.ComputeDiff();

            var changes = differ.ProcessChanges(edits);

            Debug.Assert(oldText.WithChanges(changes).ContentEquals(newText), "Incorrect minimal changes");

            return(changes);
        }
Ejemplo n.º 3
0
        public override void PublishCSharp(string filePath, SourceText sourceText, int hostDocumentVersion)
        {
            if (filePath is null)
            {
                throw new ArgumentNullException(nameof(filePath));
            }

            if (sourceText is null)
            {
                throw new ArgumentNullException(nameof(sourceText));
            }

            _foregroundDispatcher.AssertForegroundThread();

            if (!_publishedCSharpData.TryGetValue(filePath, out var previouslyPublishedData))
            {
                previouslyPublishedData = PublishData.Default;
            }

            var textChanges = SourceTextDiffer.GetMinimalTextChanges(previouslyPublishedData.SourceText, sourceText);

            if (textChanges.Count == 0 && hostDocumentVersion == previouslyPublishedData.HostDocumentVersion)
            {
                // Source texts match along with host document versions. We've already published something that looks like this. No-op.
                return;
            }

            if (_logger.IsEnabled(LogLevel.Trace))
            {
                var previousDocumentLength = previouslyPublishedData.SourceText.Length;
                var currentDocumentLength  = sourceText.Length;
                var documentLengthDelta    = sourceText.Length - previousDocumentLength;
                _logger.LogTrace(
                    "Updating C# buffer of {0} to correspond with host document version {1}. {2} -> {3} = Change delta of {4} via {5} text changes.",
                    filePath,
                    hostDocumentVersion,
                    previousDocumentLength,
                    currentDocumentLength,
                    documentLengthDelta,
                    textChanges.Count);
            }

            _publishedCSharpData[filePath] = new PublishData(sourceText, hostDocumentVersion);

            var request = new UpdateBufferRequest()
            {
                HostDocumentFilePath = filePath,
                Changes             = textChanges,
                HostDocumentVersion = hostDocumentVersion,
            };

            var result = _server.SendRequest(LanguageServerConstants.RazorUpdateCSharpBufferEndpoint, request);

            // This is the call that actually makes the request, any SendRequest without a .Returning* after it will do nothing.
            _ = result.ReturningVoid(CancellationToken.None);
        }
        public override void PublishCSharp(string filePath, SourceText sourceText, int hostDocumentVersion)
        {
            if (filePath is null)
            {
                throw new ArgumentNullException(nameof(filePath));
            }

            if (sourceText is null)
            {
                throw new ArgumentNullException(nameof(sourceText));
            }

            _foregroundDispatcher.AssertForegroundThread();

            if (!_publishedCSharpData.TryGetValue(filePath, out var previouslyPublishedData))
            {
                previouslyPublishedData = PublishData.Default;
            }

            var textChanges = SourceTextDiffer.GetMinimalTextChanges(previouslyPublishedData.SourceText, sourceText);

            if (textChanges.Count == 0 && hostDocumentVersion == previouslyPublishedData.HostDocumentVersion)
            {
                // Source texts match along with host document versions. We've already published something that looks like this. No-op.
                return;
            }

            _publishedCSharpData[filePath] = new PublishData(sourceText, hostDocumentVersion);

            var request = new UpdateBufferRequest()
            {
                HostDocumentFilePath = filePath,
                Changes             = textChanges,
                HostDocumentVersion = hostDocumentVersion,
            };

            var result = _server.Value.Client.SendRequest(LanguageServerConstants.RazorUpdateCSharpBufferEndpoint, request);

            // This is the call that actually makes the request, any SendRequest without a .Returning* after it will do nothing.
            result.ReturningVoid(CancellationToken.None);
        }
        public void GetMinimalTextChanges_ReturnsAccurateResults(string oldStr, string newStr)
        {
            // Arrange
            var oldText = SourceText.From(oldStr);
            var newText = SourceText.From(newStr);

            // Act 1
            var characterChanges = SourceTextDiffer.GetMinimalTextChanges(oldText, newText, lineDiffOnly: false);

            // Assert 1
            var changedText = oldText.WithChanges(characterChanges);

            Assert.Equal(newStr, changedText.ToString());

            // Act 2
            var lineChanges = SourceTextDiffer.GetMinimalTextChanges(oldText, newText, lineDiffOnly: false);

            // Assert 2
            changedText = oldText.WithChanges(lineChanges);
            Assert.Equal(newStr, changedText.ToString());
        }
Ejemplo n.º 6
0
        public override void PublishHtml(string filePath, SourceText sourceText, long hostDocumentVersion)
        {
            if (filePath is null)
            {
                throw new ArgumentNullException(nameof(filePath));
            }

            if (sourceText is null)
            {
                throw new ArgumentNullException(nameof(sourceText));
            }

            _foregroundDispatcher.AssertForegroundThread();

            if (!_publishedHtmlData.TryGetValue(filePath, out var previouslyPublishedData))
            {
                previouslyPublishedData = PublishData.Default;
            }

            var textChanges = SourceTextDiffer.GetMinimalTextChanges(previouslyPublishedData.SourceText, sourceText);

            if (textChanges.Count == 0 && hostDocumentVersion == previouslyPublishedData.HostDocumentVersion)
            {
                // Source texts match along with host document versions. We've already published something that looks like this. No-op.
                return;
            }

            _publishedHtmlData[filePath] = new PublishData(sourceText, hostDocumentVersion);

            var request = new UpdateBufferRequest()
            {
                HostDocumentFilePath = filePath,
                Changes             = textChanges,
                HostDocumentVersion = hostDocumentVersion,
            };

            _server.Value.Client.SendRequest(LanguageServerConstants.RazorUpdateHtmlBufferEndpoint, request);
        }