Skip to content

Commit fec62b6

Browse files
committed
Ensure we regenerate C# code when documents move from misc files to regular projects (or vice versa) while open
1 parent dea497e commit fec62b6

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/OpenDocumentGenerator.cs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections.Immutable;
77
using System.Threading;
88
using System.Threading.Tasks;
9+
using Microsoft.CodeAnalysis.Razor.Logging;
910
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
1011
using Microsoft.CodeAnalysis.Razor.Utilities;
1112
using Microsoft.CodeAnalysis.Razor.Workspaces;
@@ -29,14 +30,16 @@ internal partial class OpenDocumentGenerator : IRazorStartupService, IDisposable
2930
private readonly ImmutableArray<IDocumentProcessedListener> _listeners;
3031
private readonly IProjectSnapshotManager _projectManager;
3132
private readonly LanguageServerFeatureOptions _options;
33+
private readonly ILogger _logger;
3234

3335
private readonly AsyncBatchingWorkQueue<IDocumentSnapshot> _workQueue;
3436
private readonly CancellationTokenSource _disposeTokenSource;
3537

3638
public OpenDocumentGenerator(
3739
IEnumerable<IDocumentProcessedListener> listeners,
3840
IProjectSnapshotManager projectManager,
39-
LanguageServerFeatureOptions options)
41+
LanguageServerFeatureOptions options,
42+
ILoggerFactory loggerFactory)
4043
{
4144
_listeners = listeners.ToImmutableArray();
4245
_projectManager = projectManager;
@@ -49,6 +52,7 @@ public OpenDocumentGenerator(
4952
_disposeTokenSource.Token);
5053

5154
_projectManager.Changed += ProjectManager_Changed;
55+
_logger = loggerFactory.GetOrCreateLogger<OpenDocumentGenerator>();
5256
}
5357

5458
public void Dispose()
@@ -88,6 +92,8 @@ private void ProjectManager_Changed(object? sender, ProjectChangeEventArgs args)
8892
return;
8993
}
9094

95+
_logger.LogDebug($"Got a project change of type {args.Kind} for {args.ProjectKey.Id}");
96+
9197
switch (args.Kind)
9298
{
9399
case ProjectChangeKind.ProjectChanged:
@@ -106,25 +112,11 @@ private void ProjectManager_Changed(object? sender, ProjectChangeEventArgs args)
106112
}
107113

108114
case ProjectChangeKind.DocumentAdded:
109-
{
110-
var newProject = args.Newer.AssumeNotNull();
111-
var documentFilePath = args.DocumentFilePath.AssumeNotNull();
112-
113-
if (newProject.TryGetDocument(documentFilePath, out var document))
114-
{
115-
// We don't enqueue the current document because added documents are initially closed.
116-
117-
foreach (var relatedDocument in newProject.GetRelatedDocuments(document))
118-
{
119-
EnqueueIfNecessary(relatedDocument);
120-
}
121-
}
122-
123-
break;
124-
}
125-
126115
case ProjectChangeKind.DocumentChanged:
127116
{
117+
// Most of the time Add will be called on closed files, but when migrating files to/from the misc files
118+
// project they could already be open, but with a different generated C# path
119+
128120
var newProject = args.Newer.AssumeNotNull();
129121
var documentFilePath = args.DocumentFilePath.AssumeNotNull();
130122

@@ -178,6 +170,8 @@ void EnqueueIfNecessary(IDocumentSnapshot document)
178170
return;
179171
}
180172

173+
_logger.LogDebug($"Enqueuing generation of {document.FilePath} in {document.Project.Key.Id}");
174+
181175
_workQueue.AddWork(document);
182176
}
183177
}

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/ProjectSystem/RazorProjectService.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,18 @@ private void MoveDocument(
454454
var currentHostDocument = documentSnapshot.State.HostDocument;
455455

456456
var textLoader = new DocumentSnapshotTextLoader(documentSnapshot);
457-
var newHostDocument = new HostDocument(documentSnapshot.FilePath, documentSnapshot.TargetPath, documentSnapshot.FileKind);
457+
458+
// If we're moving from the misc files project to a real project, then target path will be the full path to the file
459+
// and the next update to the project will update it to be a relative path. To save a bunch of busy work if that is
460+
// the only change necessary, we can proactively do that work here.
461+
var projectDirectory = FilePathNormalizer.GetNormalizedDirectoryName(toProject.FilePath);
462+
var newTargetPath = documentSnapshot.TargetPath;
463+
if (FilePathNormalizer.Normalize(newTargetPath).StartsWith(projectDirectory))
464+
{
465+
newTargetPath = newTargetPath[projectDirectory.Length..];
466+
}
467+
468+
var newHostDocument = new HostDocument(documentSnapshot.FilePath, newTargetPath, documentSnapshot.FileKind);
458469

459470
_logger.LogInformation($"Moving '{documentFilePath}' from the '{fromProject.Key}' project to '{toProject.Key}' project.");
460471

0 commit comments

Comments
 (0)