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); }
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()); }
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); }