-
Notifications
You must be signed in to change notification settings - Fork 196
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
Add IProjectSnapshot.ContainsDocument to test document paths cheaply #10975
Conversation
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.
@@ -38,13 +34,6 @@ public bool TryCreate( | |||
return false; | |||
} | |||
|
|||
if (documentSnapshot is null) |
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectSnapshot.cs
Show resolved
Hide resolved
src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/ProjectSnapshot.cs
Outdated
Show resolved
Hide resolved
@@ -37,6 +37,8 @@ internal interface IProjectSnapshot | |||
ProjectWorkspaceState ProjectWorkspaceState { get; } | |||
|
|||
RazorProjectEngine GetProjectEngine(); | |||
|
|||
bool ContainsDocument(string filePath); | |||
IDocumentSnapshot? GetDocument(string filePath); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
.
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 theIProjectSnapshot
implementations a bit.