Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ private async Task<SynchronizationResult> PublishHtmlDocumentAsync(TextDocument

try
{
var result = new SynchronizationResult(true, requestedVersion.Checksum);
await _htmlDocumentPublisher.PublishAsync(document, result, htmlText, cancellationToken).ConfigureAwait(false);
var synchronized = await _htmlDocumentPublisher.TryPublishAsync(document, requestedVersion.Checksum, htmlText, cancellationToken).ConfigureAwait(false);
var result = new SynchronizationResult(Synchronized: synchronized, requestedVersion.Checksum);

// If we were cancelled, we can't trust that the publish worked.
if (cancellationToken.IsCancellationRequested)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.ExternalAccess.Razor;

namespace Microsoft.VisualStudio.Razor.LanguageClient.Cohost;

internal interface IHtmlDocumentPublisher
{
Task PublishAsync(TextDocument document, SynchronizationResult synchronizationResult, string htmlText, CancellationToken cancellationToken);
Task<bool> TryPublishAsync(TextDocument document, ChecksumWrapper checksum, string htmlText, CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Collections.Immutable;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor;
Expand All @@ -29,22 +28,19 @@ internal sealed class HtmlDocumentPublisher(
private readonly TrackingLSPDocumentManager _documentManager = documentManager as TrackingLSPDocumentManager ?? throw new InvalidOperationException("Expected TrackingLSPDocumentManager");
private readonly ILogger _logger = loggerFactory.GetOrCreateLogger<HtmlDocumentPublisher>();

public async Task PublishAsync(TextDocument document, SynchronizationResult synchronizationResult, string htmlText, CancellationToken cancellationToken)
public async Task<bool> TryPublishAsync(TextDocument document, ChecksumWrapper checksum, string htmlText, CancellationToken cancellationToken)
{
Assumed.True(synchronizationResult.Synchronized);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the only thing that used the Synchronized field of the passed in result, and the passed in result always hardcoded it to true.

Sometimes it's not good to look back at your past code :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I experience this a lot, but I just consider PreviousTodd as a completely different idiot than current Todd.


var uri = document.CreateUri();
if (!_documentManager.TryGetDocument(uri, out var documentSnapshot) ||
!documentSnapshot.TryGetVirtualDocument<HtmlVirtualDocumentSnapshot>(out var htmlDocument))
{
Debug.Fail("Got an LSP text document update before getting informed of the VS buffer. Create on demand?");
_logger.LogError($"Couldn't get Html text for {document.FilePath}. Html document contents will be stale");
return;
_logger.LogDebug($"No {(documentSnapshot is null ? "" : "virtual")} document snapshot for {document.FilePath}.");
return false;
}

if (cancellationToken.IsCancellationRequested)
{
return;
return false;
}

_logger.LogDebug($"The html document for {document.FilePath} is {htmlDocument.Uri}");
Expand All @@ -53,7 +49,7 @@ public async Task PublishAsync(TextDocument document, SynchronizationResult sync

if (cancellationToken.IsCancellationRequested)
{
return;
return false;
}

// We have a string for the Html text here, so its tempting to just overwrite the whole buffer, but that causes issues with
Expand All @@ -63,8 +59,10 @@ public async Task PublishAsync(TextDocument document, SynchronizationResult sync
var newHtmlSourceText = SourceText.From(htmlText, currentHtmlSourceText.Encoding, currentHtmlSourceText.ChecksumAlgorithm);
var textChanges = SourceTextDiffer.GetMinimalTextChanges(currentHtmlSourceText, newHtmlSourceText);
var changes = textChanges.SelectAsArray(c => new VisualStudioTextChange(c.Span.Start, c.Span.Length, c.NewText.AssumeNotNull()));
_documentManager.UpdateVirtualDocument<HtmlVirtualDocument>(uri, changes, documentSnapshot.Version, state: synchronizationResult.Checksum);
_documentManager.UpdateVirtualDocument<HtmlVirtualDocument>(uri, changes, documentSnapshot.Version, state: checksum);

_logger.LogDebug($"Finished Html document generation for {document.FilePath} (into {htmlDocument.Uri})");

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ internal sealed class HtmlDocumentPublisher(
{
private readonly RazorClientServerManagerProvider _razorClientServerManagerProvider = razorClientServerManagerProvider;

public async Task PublishAsync(TextDocument document, SynchronizationResult synchronizationResult, string htmlText, CancellationToken cancellationToken)
public async Task<bool> TryPublishAsync(TextDocument document, ChecksumWrapper checksum, string htmlText, CancellationToken cancellationToken)
{
Assumed.True(synchronizationResult.Synchronized);

var request = new HtmlUpdateParameters(new TextDocumentIdentifier { DocumentUri = document.CreateDocumentUri() }, synchronizationResult.Checksum.ToString(), htmlText);
var request = new HtmlUpdateParameters(new TextDocumentIdentifier { DocumentUri = document.CreateDocumentUri() }, checksum.ToString(), htmlText);

var clientConnection = _razorClientServerManagerProvider.ClientLanguageServerManager.AssumeNotNull();
await clientConnection.SendRequestAsync("razor/updateHtml", request, cancellationToken).ConfigureAwait(false);
return true;
}

private record HtmlUpdateParameters(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ protected override void ConfigureWorkspace(AdhocWorkspace workspace)
Assert.True(workspace.TryApplyChanges(document.Project.Solution));
}

[Fact]
public async Task TrySynchronize_FailsIfPublishFails()
{
var document = Workspace.CurrentSolution.GetAdditionalDocument(_documentId).AssumeNotNull();

var publisher = new TestHtmlDocumentPublisher(publishResult: false);
var remoteServiceInvoker = new RemoteServiceInvoker(document);
var synchronizer = new HtmlDocumentSynchronizer(remoteServiceInvoker, publisher, LoggerFactory);

Assert.False((await synchronizer.TrySynchronizeAsync(document, DisposalToken)).Synchronized);
}

[Fact]
public async Task TrySynchronize_NewDocument_Generates()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@

namespace Microsoft.VisualStudio.Razor.LanguageClient.Cohost;

internal sealed class TestHtmlDocumentPublisher : IHtmlDocumentPublisher
internal sealed class TestHtmlDocumentPublisher(bool publishResult = true) : IHtmlDocumentPublisher
{
private readonly bool _publishResult = publishResult;

public List<(TextDocument Document, string Text, ChecksumWrapper Checksum)> Publishes { get; } = [];

public Task PublishAsync(TextDocument document, SynchronizationResult synchronizationResult, string htmlText, CancellationToken cancellationToken)
public Task<bool> TryPublishAsync(TextDocument document, ChecksumWrapper checksum, string htmlText, CancellationToken cancellationToken)
{
Publishes.Add((document, htmlText, synchronizationResult.Checksum));
return Task.CompletedTask;
Publishes.Add((document, htmlText, checksum));
return Task.FromResult(_publishResult);
}
}