Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't register cohost types with the Razor project system #9830

Merged
merged 1 commit into from
Jan 18, 2024
Merged
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 @@ -6,7 +6,6 @@
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor;
using Microsoft.AspNetCore.Razor.LanguageServer.ProjectSystem;
using Microsoft.CodeAnalysis.Razor.Logging;
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
Expand All @@ -21,15 +20,14 @@ namespace Microsoft.VisualStudio.LanguageServerClient.Razor.Cohost;

[Shared]
[Export(typeof(OpenDocumentGenerator))]
[Export(typeof(IProjectSnapshotChangeTrigger))]
[method: ImportingConstructor]
internal sealed class OpenDocumentGenerator(
LanguageServerFeatureOptions languageServerFeatureOptions,
LSPDocumentManager documentManager,
CSharpVirtualDocumentAddListener csharpVirtualDocumentAddListener,
ISnapshotResolver snapshotResolver,
JoinableTaskContext joinableTaskContext,
IRazorLoggerFactory loggerFactory) : IProjectSnapshotChangeTrigger
IRazorLoggerFactory loggerFactory)
{
private readonly LanguageServerFeatureOptions _languageServerFeatureOptions = languageServerFeatureOptions ?? throw new ArgumentNullException(nameof(languageServerFeatureOptions));
private readonly TrackingLSPDocumentManager _documentManager = documentManager as TrackingLSPDocumentManager ?? throw new ArgumentNullException(nameof(documentManager));
Expand All @@ -38,19 +36,6 @@ internal sealed class OpenDocumentGenerator(
private readonly JoinableTaskFactory _joinableTaskFactory = joinableTaskContext.Factory;
private readonly ILogger _logger = loggerFactory.CreateLogger<OpenDocumentGenerator>();

private ProjectSnapshotManager? _projectManager;

public void Initialize(ProjectSnapshotManagerBase projectManager)
{
if (!_languageServerFeatureOptions.UseRazorCohostServer)
{
return;
}

_projectManager = projectManager;
_projectManager.Changed += ProjectManager_Changed;
}

public async Task DocumentOpenedOrChangedAsync(string textDocumentPath, int version, CancellationToken cancellationToken)
{
// We are purposefully trigger code generation here directly, rather than using the project manager events that the above call
Expand Down Expand Up @@ -179,27 +164,4 @@ [new VisualStudioTextChange(0, csharpVirtualDocumentSnapshot.Snapshot.Length, ge
_ = documentSnapshot.TryGetVirtualDocument<HtmlVirtualDocumentSnapshot>(out var htmlVirtualDocumentSnapshot);
return htmlVirtualDocumentSnapshot;
}

private void ProjectManager_Changed(object sender, ProjectChangeEventArgs e)
{
// We only respond to ProjectChanged events, as opens and changes are handled by LSP endpoints, which call into this class too
if (e.Kind == ProjectChangeKind.ProjectChanged)
{
var newProject = e.Newer.AssumeNotNull();

foreach (var documentFilePath in newProject.DocumentFilePaths)
{
if (_projectManager!.IsDocumentOpen(documentFilePath) &&
newProject.GetDocument(documentFilePath) is { } document &&
_documentManager.TryGetDocument(new Uri(document.FilePath), out var documentSnapshot))
{
// This is not ideal, but we need to re-use the existing snapshot version because our system uses the version
// of the text buffer, but a project change doesn't change the text buffer.
// See https://github.com/dotnet/razor/issues/9197 for more info and some issues this causes
// This should all be moot eventually in Cohosting eventually anyway (ie, this whole file should be deleted)
_ = UpdateGeneratedDocumentsAsync(document, documentSnapshot.Version, CancellationToken.None);
}
}
}
}
}
Loading