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

Add IProjectSnapshot.ContainsDocument to test document paths cheaply #10975

Merged

Conversation

DustinCampbell
Copy link
Member

There's a fair amount of code in Razor that uses IProjectSnapshot.GetDocument(...) to test for the presence of a document with a given file path in a project. However, GetDocument has the side effect of creating a document snapshot which ultimately may be thrown away.

This change adds an IProjectSnapshot.ContainsDocument(...) method that tests for the presence of a document by file path and returns true or false without creating a new document snapshot. In addition, I've cleaned up the IProjectSnapshot implementations a bit.

There's a fair amount of code in Razor that uses `IProjectSnapshot.GetDocument(...)` to test for the presence of a document with a given file path in a project. However, `GetDocument` has the side effect of creating a document snapshot which ultimately may be thrown away.

This change adds an `IProjectSnapshot.ContainsDocument(...)` method that tests for the presence of a document by file path and returns true or false without creating a new document snapshot. In addition, I've cleaned up the `IProjectSnapshot` implementations a bit.
@DustinCampbell DustinCampbell requested a review from a team as a code owner October 7, 2024 15:46
@@ -38,13 +34,6 @@ public bool TryCreate(
return false;
}

if (documentSnapshot is null)
Copy link
Member Author

Choose a reason for hiding this comment

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

ℹ️ This condition can never be true because of the call to TryResolveDocument(...) above.

Copy link
Contributor

@davidwengier davidwengier left a comment

Choose a reason for hiding this comment

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

Totally fine with this as is, as it matches the previous code better, but left a couple of nits you could consider.

@@ -37,6 +37,8 @@ internal interface IProjectSnapshot
ProjectWorkspaceState ProjectWorkspaceState { get; }

RazorProjectEngine GetProjectEngine();

bool ContainsDocument(string filePath);
IDocumentSnapshot? GetDocument(string filePath);
Copy link
Contributor

Choose a reason for hiding this comment

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

💭 should we get rid of GetDocument entirely?

Copy link
Member Author

Choose a reason for hiding this comment

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

I thought about that, but it requires a much larger change. This PR doesn't address most of the existing GetDocument(...) call-sites. Also, I think the API is probably still useful. I'd rather make it non-nullable and throw at some point -- sort of like TryGetProject and GetLoadedProject on IProjectSnapshotManager.

@DustinCampbell DustinCampbell merged commit f02835b into dotnet:main Oct 8, 2024
12 checks passed
@DustinCampbell DustinCampbell deleted the projectsnaphot-containsdocument branch October 8, 2024 00:34
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Oct 8, 2024
@phil-allen-msft phil-allen-msft modified the milestones: Next, 17.13 P1 Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants