Remove some things from `IProjectSnapshot` for cohostings benefit (#10831)

Chris found these to be problematic in his branch where he enabled the
source generator, but fortunately they're simply unnecessary in
cohosting.
This commit is contained in:
David Wengier 2024-09-05 10:16:54 +10:00 коммит произвёл GitHub
Родитель 6718dbcb40 42cc39e4a9
Коммит fbf8c8ef4d
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
8 изменённых файлов: 36 добавлений и 125 удалений

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

@ -27,12 +27,6 @@ internal static class IDocumentSnapshotExtensions
var project = documentSnapshot.Project;
// If the document is an import document, then it can't be a component
if (project.IsImportDocument(documentSnapshot))
{
return null;
}
// If we got this far, we can check for tag helpers
var tagHelpers = await project.GetTagHelpersAsync(cancellationToken).ConfigureAwait(false);
foreach (var tagHelper in tagHelpers)

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

@ -39,14 +39,4 @@ internal interface IProjectSnapshot
RazorProjectEngine GetProjectEngine();
IDocumentSnapshot? GetDocument(string filePath);
bool TryGetDocument(string filePath, [NotNullWhen(true)] out IDocumentSnapshot? document);
bool IsImportDocument(IDocumentSnapshot document);
/// <summary>
/// If the provided document is an import document, gets the other documents in the project
/// that include directives specified by the provided document. Otherwise returns an empty
/// list.
/// </summary>
/// <param name="document">The document.</param>
/// <returns>A list of related documents.</returns>
ImmutableArray<IDocumentSnapshot> GetRelatedDocuments(IDocumentSnapshot document);
}

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

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.
using System;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Threading;
@ -50,4 +51,14 @@ internal static class IProjectSnapshotExtensions
return tagHelperTask.Result;
#pragma warning restore VSTHRD002 // Avoid problematic synchronous waits
}
public static ImmutableArray<IDocumentSnapshot> GetRelatedDocuments(this IProjectSnapshot projectSnapshot, IDocumentSnapshot document)
{
if (projectSnapshot is not ProjectSnapshot project)
{
throw new InvalidOperationException("This method can only be called with a ProjectSnapshot.");
}
return project.GetRelatedDocuments(document);
}
}

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

@ -79,16 +79,11 @@ internal class ProjectSnapshot : IProjectSnapshot
return document is not null;
}
public bool IsImportDocument(IDocumentSnapshot document)
{
if (document is null)
{
throw new ArgumentNullException(nameof(document));
}
return document.TargetPath is { } targetPath && State.ImportsToRelatedDocuments.ContainsKey(targetPath);
}
/// <summary>
/// If the provided document is an import document, gets the other documents in the project
/// that include directives specified by the provided document. Otherwise returns an empty
/// list.
/// </summary>
public ImmutableArray<IDocumentSnapshot> GetRelatedDocuments(IDocumentSnapshot document)
{
if (document is null)

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

@ -11,7 +11,6 @@ using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.PooledObjects;
using Microsoft.AspNetCore.Razor.ProjectEngineHost;
using Microsoft.AspNetCore.Razor.ProjectSystem;
using Microsoft.AspNetCore.Razor.Telemetry;
@ -32,7 +31,6 @@ internal class RemoteProjectSnapshot : IProjectSnapshot
private readonly ITelemetryReporter _telemetryReporter;
private readonly Lazy<RazorConfiguration> _lazyConfiguration;
private readonly Lazy<RazorProjectEngine> _lazyProjectEngine;
private readonly Lazy<ImmutableDictionary<string, ImmutableArray<string>>> _importsToRelatedDocumentsLazy;
private ImmutableArray<TagHelperDescriptor> _tagHelpers;
@ -56,18 +54,6 @@ internal class RemoteProjectSnapshot : IProjectSnapshot
builder.SetSupportLocalizedComponentNames();
});
});
_importsToRelatedDocumentsLazy = new Lazy<ImmutableDictionary<string, ImmutableArray<string>>>(() =>
{
var importsToRelatedDocuments = ImmutableDictionary.Create<string, ImmutableArray<string>>(FilePathNormalizingComparer.Instance);
foreach (var documentFilePath in DocumentFilePaths)
{
var importTargetPaths = ProjectState.GetImportDocumentTargetPaths(documentFilePath, FileKinds.GetFileKindFromFilePath(documentFilePath), _lazyProjectEngine.Value);
importsToRelatedDocuments = ProjectState.AddToImportsToRelatedDocuments(importsToRelatedDocuments, documentFilePath, importTargetPaths);
}
return importsToRelatedDocuments;
});
}
public RazorConfiguration Configuration => throw new InvalidOperationException("Should not be called for cohosted projects.");
@ -154,34 +140,6 @@ internal class RemoteProjectSnapshot : IProjectSnapshot
/// <returns></returns>
internal RazorProjectEngine GetProjectEngine_CohostOnly() => _lazyProjectEngine.Value;
public ImmutableArray<IDocumentSnapshot> GetRelatedDocuments(IDocumentSnapshot document)
{
var targetPath = document.TargetPath.AssumeNotNull();
if (!_importsToRelatedDocumentsLazy.Value.TryGetValue(targetPath, out var relatedDocuments))
{
return [];
}
using var builder = new PooledArrayBuilder<IDocumentSnapshot>(relatedDocuments.Length);
foreach (var relatedDocumentFilePath in relatedDocuments)
{
if (TryGetDocument(relatedDocumentFilePath, out var relatedDocument))
{
builder.Add(relatedDocument);
}
}
return builder.DrainToImmutable();
}
public bool IsImportDocument(IDocumentSnapshot document)
{
return document.TargetPath is { } targetPath &&
_importsToRelatedDocumentsLazy.Value.ContainsKey(targetPath);
}
private RazorConfiguration CreateRazorConfiguration()
{
// See RazorSourceGenerator.RazorProviders.cs

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

@ -72,26 +72,6 @@ internal class EphemeralProjectSnapshot : IProjectSnapshot
return false;
}
public bool IsImportDocument(IDocumentSnapshot document)
{
if (document is null)
{
throw new ArgumentNullException(nameof(document));
}
return false;
}
public ImmutableArray<IDocumentSnapshot> GetRelatedDocuments(IDocumentSnapshot document)
{
if (document is null)
{
throw new ArgumentNullException(nameof(document));
}
return ImmutableArray<IDocumentSnapshot>.Empty;
}
public RazorProjectEngine GetProjectEngine()
{
return _projectEngine.Value;

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

@ -61,43 +61,6 @@ public class DefaultProjectSnapshotTest : WorkspaceTestBase
d => Assert.Same(d.Value, snapshot.GetDocument(d.Key)));
}
[Fact]
public void IsImportDocument_NonImportDocument_ReturnsFalse()
{
// Arrange
var state = ProjectState.Create(ProjectEngineFactoryProvider, _hostProject, _projectWorkspaceState)
.WithAddedHostDocument(_documents[0], DocumentState.EmptyLoader);
var snapshot = new ProjectSnapshot(state);
var document = snapshot.GetDocument(_documents[0].FilePath);
Assert.NotNull(document);
// Act
var result = snapshot.IsImportDocument(document);
// Assert
Assert.False(result);
}
[Fact]
public void IsImportDocument_ImportDocument_ReturnsTrue()
{
// Arrange
var state = ProjectState.Create(ProjectEngineFactoryProvider, _hostProject, _projectWorkspaceState)
.WithAddedHostDocument(_documents[0], DocumentState.EmptyLoader)
.WithAddedHostDocument(TestProjectData.SomeProjectImportFile, DocumentState.EmptyLoader);
var snapshot = new ProjectSnapshot(state);
var document = snapshot.GetDocument(TestProjectData.SomeProjectImportFile.FilePath);
Assert.NotNull(document);
// Act
var result = snapshot.IsImportDocument(document);
// Assert
Assert.True(result);
}
[Fact]
public void GetRelatedDocuments_NonImportDocument_ReturnsEmpty()
{

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

@ -97,6 +97,26 @@ public class CohostUriPresentationEndpointTest(ITestOutputHelper testOutputHelpe
expected: "<Component />");
}
[Fact]
public async Task ImportsFile()
{
await VerifyUriPresentationAsync(
input: """
This is a Razor document.
<div>
[||]
</div>
The end.
""",
additionalFiles: [
(File("_Imports.razor"), "")
],
uris: [FileUri("_Imports.razor")],
expected: null);
}
[Fact]
public async Task Html_IntoCSharp_NoTag()
{