Skip to content

Commit dfac5e3

Browse files
author
Andrew Hall
authored
Publish on document change if the config file doesn't exist (#9634)
2 parents ea64e3c + 6d65ce8 commit dfac5e3

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

src/Razor/src/Microsoft.VisualStudio.LanguageServerClient.Razor/RazorProjectInfoPublisher.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.ComponentModel.Composition;
7+
using System.Diagnostics;
78
using System.IO;
89
using System.Linq;
910
using System.Threading.Tasks;
@@ -171,6 +172,19 @@ internal void ProjectSnapshotManager_Changed(object sender, ProjectChangeEventAr
171172
}
172173

173174
break;
175+
case ProjectChangeKind.DocumentChanged:
176+
// DocumentChanged normally isn't a great trigger for publishing, given that it happens while a user types
177+
// but for a brand new project, its possible this DocumentChanged actually represents a DocumentOpen, and
178+
// it could be the first one, so its important to publish if there is no project configuration file present
179+
if (ProjectWorkspacePublishable(args) &&
180+
_projectConfigurationFilePathStore.TryGet(args.ProjectKey, out var configurationFilePath) &&
181+
!FileExists(configurationFilePath))
182+
{
183+
ImmediatePublish(args.Newer!);
184+
}
185+
186+
break;
187+
174188
case ProjectChangeKind.DocumentRemoved:
175189
case ProjectChangeKind.DocumentAdded:
176190

@@ -195,6 +209,10 @@ internal void ProjectSnapshotManager_Changed(object sender, ProjectChangeEventAr
195209
case ProjectChangeKind.ProjectRemoved:
196210
RemovePublishingData(args.Older!);
197211
break;
212+
213+
default:
214+
Debug.Fail("A new ProjectChangeKind has been added that the RazorProjectInfoPublisher doesn't know how to deal with");
215+
break;
198216
}
199217

200218
static bool ProjectWorkspacePublishable(ProjectChangeEventArgs args)

src/Razor/test/Microsoft.VisualStudio.LanguageServerClient.Razor.Test/RazorProjectInfoPublisherTest.cs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,44 @@ await RunOnDispatcherThreadAsync(() =>
158158
Assert.True(serializationSuccessful);
159159
}
160160

161+
[Fact]
162+
public async Task ProjectManager_Changed_DocumentOpened_InitializedProject_NoFile_Active_Publishes()
163+
{
164+
// Arrange
165+
var serializationSuccessful = false;
166+
var hostProject = new HostProject(@"C:\path\to\project.csproj", @"C:\path\to\obj", RazorConfiguration.Default, rootNamespace: "TestRootNamespace");
167+
var hostDocument = new HostDocument(@"C:\path\to\file.razor", "file.razor");
168+
_projectSnapshotManager.ProjectAdded(hostProject);
169+
_projectSnapshotManager.ProjectWorkspaceStateChanged(hostProject.Key, ProjectWorkspaceState.Default);
170+
_projectSnapshotManager.DocumentAdded(hostProject.Key, hostDocument, new EmptyTextLoader(hostDocument.FilePath));
171+
var projectSnapshot = _projectSnapshotManager.GetProjects()[0];
172+
var expectedConfigurationFilePath = @"C:\path\to\obj\bin\Debug\project.razor.bin";
173+
_projectConfigurationFilePathStore.Set(projectSnapshot.Key, expectedConfigurationFilePath);
174+
var publisher = new TestRazorProjectInfoPublisher(
175+
_projectConfigurationFilePathStore,
176+
onSerializeToFile: (snapshot, configurationFilePath) =>
177+
{
178+
Assert.Equal(expectedConfigurationFilePath, configurationFilePath);
179+
serializationSuccessful = true;
180+
},
181+
configurationFileExists: false)
182+
{
183+
EnqueueDelay = 10,
184+
_active = true
185+
};
186+
publisher.Initialize(_projectSnapshotManager);
187+
188+
// Act
189+
await RunOnDispatcherThreadAsync(() =>
190+
{
191+
_projectSnapshotManager.DocumentOpened(hostProject.Key, hostDocument.FilePath, SourceText.From(string.Empty));
192+
});
193+
194+
// Assert
195+
Assert.Empty(publisher.DeferredPublishTasks);
196+
Assert.True(serializationSuccessful);
197+
}
198+
161199
[Theory]
162200
[InlineData(ProjectChangeKind.DocumentAdded)]
163201
[InlineData(ProjectChangeKind.DocumentRemoved)]
@@ -586,6 +624,7 @@ private class TestRazorProjectInfoPublisher : RazorProjectInfoPublisher
586624

587625
private readonly bool _shouldSerialize;
588626
private readonly bool _useRealShouldSerialize;
627+
private readonly bool _configurationFileExists;
589628

590629
static TestRazorProjectInfoPublisher()
591630
{
@@ -598,17 +637,19 @@ public TestRazorProjectInfoPublisher(
598637
ProjectConfigurationFilePathStore projectStatePublishFilePathStore,
599638
Action<IProjectSnapshot, string> onSerializeToFile = null,
600639
bool shouldSerialize = true,
601-
bool useRealShouldSerialize = false)
640+
bool useRealShouldSerialize = false,
641+
bool configurationFileExists = true)
602642
: base(s_lspEditorFeatureDetector.Object, projectStatePublishFilePathStore, TestRazorLogger.Instance)
603643
{
604644
_onSerializeToFile = onSerializeToFile ?? ((_1, _2) => throw new XunitException("SerializeToFile should not have been called."));
605645
_shouldSerialize = shouldSerialize;
606646
_useRealShouldSerialize = useRealShouldSerialize;
647+
_configurationFileExists = configurationFileExists;
607648
}
608649

609650
protected override bool FileExists(string file)
610651
{
611-
return true;
652+
return _configurationFileExists;
612653
}
613654

614655
protected override void SerializeToFile(IProjectSnapshot projectSnapshot, string configurationFilePath) => _onSerializeToFile?.Invoke(projectSnapshot, configurationFilePath);

0 commit comments

Comments
 (0)