Publish on document change if the config file doesn't exist (#9634)

This commit is contained in:
Andrew Hall 2023-11-29 12:05:42 -08:00 коммит произвёл GitHub
Родитель ea64e3cd3c 6d65ce8643
Коммит dfac5e3cec
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 61 добавлений и 2 удалений

Просмотреть файл

@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
@ -171,6 +172,19 @@ internal class RazorProjectInfoPublisher : IProjectSnapshotChangeTrigger
}
break;
case ProjectChangeKind.DocumentChanged:
// DocumentChanged normally isn't a great trigger for publishing, given that it happens while a user types
// but for a brand new project, its possible this DocumentChanged actually represents a DocumentOpen, and
// it could be the first one, so its important to publish if there is no project configuration file present
if (ProjectWorkspacePublishable(args) &&
_projectConfigurationFilePathStore.TryGet(args.ProjectKey, out var configurationFilePath) &&
!FileExists(configurationFilePath))
{
ImmediatePublish(args.Newer!);
}
break;
case ProjectChangeKind.DocumentRemoved:
case ProjectChangeKind.DocumentAdded:
@ -195,6 +209,10 @@ internal class RazorProjectInfoPublisher : IProjectSnapshotChangeTrigger
case ProjectChangeKind.ProjectRemoved:
RemovePublishingData(args.Older!);
break;
default:
Debug.Fail("A new ProjectChangeKind has been added that the RazorProjectInfoPublisher doesn't know how to deal with");
break;
}
static bool ProjectWorkspacePublishable(ProjectChangeEventArgs args)

Просмотреть файл

@ -158,6 +158,44 @@ public class RazorProjectInfoPublisherTest : LanguageServerTestBase
Assert.True(serializationSuccessful);
}
[Fact]
public async Task ProjectManager_Changed_DocumentOpened_InitializedProject_NoFile_Active_Publishes()
{
// Arrange
var serializationSuccessful = false;
var hostProject = new HostProject(@"C:\path\to\project.csproj", @"C:\path\to\obj", RazorConfiguration.Default, rootNamespace: "TestRootNamespace");
var hostDocument = new HostDocument(@"C:\path\to\file.razor", "file.razor");
_projectSnapshotManager.ProjectAdded(hostProject);
_projectSnapshotManager.ProjectWorkspaceStateChanged(hostProject.Key, ProjectWorkspaceState.Default);
_projectSnapshotManager.DocumentAdded(hostProject.Key, hostDocument, new EmptyTextLoader(hostDocument.FilePath));
var projectSnapshot = _projectSnapshotManager.GetProjects()[0];
var expectedConfigurationFilePath = @"C:\path\to\obj\bin\Debug\project.razor.bin";
_projectConfigurationFilePathStore.Set(projectSnapshot.Key, expectedConfigurationFilePath);
var publisher = new TestRazorProjectInfoPublisher(
_projectConfigurationFilePathStore,
onSerializeToFile: (snapshot, configurationFilePath) =>
{
Assert.Equal(expectedConfigurationFilePath, configurationFilePath);
serializationSuccessful = true;
},
configurationFileExists: false)
{
EnqueueDelay = 10,
_active = true
};
publisher.Initialize(_projectSnapshotManager);
// Act
await RunOnDispatcherThreadAsync(() =>
{
_projectSnapshotManager.DocumentOpened(hostProject.Key, hostDocument.FilePath, SourceText.From(string.Empty));
});
// Assert
Assert.Empty(publisher.DeferredPublishTasks);
Assert.True(serializationSuccessful);
}
[Theory]
[InlineData(ProjectChangeKind.DocumentAdded)]
[InlineData(ProjectChangeKind.DocumentRemoved)]
@ -586,6 +624,7 @@ public class RazorProjectInfoPublisherTest : LanguageServerTestBase
private readonly bool _shouldSerialize;
private readonly bool _useRealShouldSerialize;
private readonly bool _configurationFileExists;
static TestRazorProjectInfoPublisher()
{
@ -598,17 +637,19 @@ public class RazorProjectInfoPublisherTest : LanguageServerTestBase
ProjectConfigurationFilePathStore projectStatePublishFilePathStore,
Action<IProjectSnapshot, string> onSerializeToFile = null,
bool shouldSerialize = true,
bool useRealShouldSerialize = false)
bool useRealShouldSerialize = false,
bool configurationFileExists = true)
: base(s_lspEditorFeatureDetector.Object, projectStatePublishFilePathStore, TestRazorLogger.Instance)
{
_onSerializeToFile = onSerializeToFile ?? ((_1, _2) => throw new XunitException("SerializeToFile should not have been called."));
_shouldSerialize = shouldSerialize;
_useRealShouldSerialize = useRealShouldSerialize;
_configurationFileExists = configurationFileExists;
}
protected override bool FileExists(string file)
{
return true;
return _configurationFileExists;
}
protected override void SerializeToFile(IProjectSnapshot projectSnapshot, string configurationFilePath) => _onSerializeToFile?.Invoke(projectSnapshot, configurationFilePath);