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

Remove some things from IProjectSnapshot for cohostings benefit #10831

Merged
merged 3 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
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 @@ -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))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is mentioned in the commit message, but if you're looking at the PR as a whole let me explain here: This was the only call to IsImportDocument, and it was impossible for it to ever return true because 10 lines above this there is a check for FileKind. Import documents have their own file kind, so would never get this far.

{
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -50,4 +51,14 @@ public static ImmutableArray<TagHelperDescriptor> GetTagHelpersSynchronously(thi
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,11 @@ public bool TryGetDocument(string filePath, [NotNullWhen(true)] out IDocumentSna
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
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;
Expand All @@ -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;

Expand All @@ -56,18 +54,6 @@ public RemoteProjectSnapshot(Project project, DocumentSnapshotFactory documentSn
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.");
Expand Down Expand Up @@ -154,34 +140,6 @@ public bool TryGetDocument(string filePath, [NotNullWhen(true)] out IDocumentSna
/// <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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,26 +72,6 @@ public bool TryGetDocument(string filePath, [NotNullWhen(true)] out IDocumentSna
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,43 +61,6 @@ public void ProjectSnapshot_CachesDocumentSnapshots()
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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,26 @@ public class Component : Microsoft.AspNetCore.Components.ComponentBase
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()
{
Expand Down
Loading