-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Add metadata documents to the MAS workspace upfront #78886
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
Open
dibarbet
wants to merge
2
commits into
dotnet:main
Choose a base branch
from
dibarbet:metadata_virtual
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
343 changes: 176 additions & 167 deletions
343
src/Features/Core/Portable/MetadataAsSource/DecompilationMetadataAsSourceFileProvider.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,11 +44,6 @@ internal sealed class PdbSourceDocumentMetadataAsSourceFileProvider( | |
private readonly IImplementationAssemblyLookupService _implementationAssemblyLookupService = implementationAssemblyLookupService; | ||
private readonly IPdbSourceDocumentLogger? _logger = logger; | ||
|
||
/// <summary> | ||
/// Lock to guard access to workspace updates when opening / closing documents. | ||
/// </summary> | ||
private readonly object _gate = new(); | ||
|
||
/// <summary> | ||
/// Accessed only in <see cref="GetGeneratedFileAsync"/> and <see cref="CleanupGeneratedFiles"/>, both of which | ||
/// are called under a lock in <see cref="MetadataAsSourceFileService"/>. So this is safe as a plain | ||
|
@@ -70,11 +65,6 @@ internal sealed class PdbSourceDocumentMetadataAsSourceFileProvider( | |
/// </summary> | ||
private readonly ConcurrentDictionary<string, SourceDocumentInfo> _fileToDocumentInfoMap = new(StringComparer.OrdinalIgnoreCase); | ||
|
||
/// <summary> | ||
/// Only accessed and mutated in serial calls either from the UI thread or LSP queue. | ||
/// </summary> | ||
private readonly HashSet<DocumentId> _openedDocumentIds = []; | ||
|
||
public async Task<MetadataAsSourceFile?> GetGeneratedFileAsync( | ||
MetadataAsSourceWorkspace metadataWorkspace, | ||
Workspace sourceWorkspace, | ||
|
@@ -262,24 +252,22 @@ internal sealed class PdbSourceDocumentMetadataAsSourceFileProvider( | |
|
||
var symbolId = SymbolKey.Create(symbol, cancellationToken); | ||
|
||
// Get a view of the solution with the document added, but do not actually update the workspace. | ||
// TryAddDocumentToWorkspace is responsible for actually updating the solution with the new document(s). | ||
// We just need a view with the document added so we can find the right location in the generated source. | ||
var pendingSolution = metadataWorkspace.CurrentSolution; | ||
var navigateProject = metadataWorkspace.CurrentSolution.GetRequiredProject(projectId); | ||
var documentInfos = CreateDocumentInfos(sourceFileInfos, encoding, projectId, sourceWorkspace, sourceProject); | ||
if (documentInfos.Length > 0) | ||
{ | ||
foreach (var documentInfo in documentInfos) | ||
{ | ||
// The document might have already been added by a previous go to definition call. | ||
if (!pendingSolution.ContainsDocument(documentInfo.Id)) | ||
if (!metadataWorkspace.CurrentSolution.ContainsDocument(documentInfo.Id)) | ||
{ | ||
pendingSolution = pendingSolution.AddDocument(documentInfo); | ||
metadataWorkspace.OnDocumentAdded(documentInfo); | ||
} | ||
} | ||
} | ||
|
||
var navigateProject = pendingSolution.GetRequiredProject(projectId); | ||
// Get a new view of the project with the documents added. | ||
navigateProject = metadataWorkspace.CurrentSolution.GetRequiredProject(projectId); | ||
} | ||
|
||
// If MetadataAsSourceHelpers.GetLocationInGeneratedSourceAsync can't find the actual document to navigate to, it will fall back | ||
// to the document passed in, which we just use the first document for. | ||
|
@@ -383,50 +371,6 @@ public bool ShouldCollapseOnOpen(MetadataAsSourceWorkspace workspace, string fil | |
return _fileToDocumentInfoMap.TryGetValue(filePath, out _) && blockStructureOptions.CollapseMetadataImplementationsWhenFirstOpened; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm beginning to wonder why _fileToDocumentInfoMap needs to exist at all -- couldn't that now just be centrally held? |
||
} | ||
|
||
public bool TryAddDocumentToWorkspace(MetadataAsSourceWorkspace workspace, string filePath, SourceTextContainer sourceTextContainer, [NotNullWhen(true)] out DocumentId? documentId) | ||
{ | ||
lock (_gate) | ||
{ | ||
if (_fileToDocumentInfoMap.TryGetValue(filePath, out var info)) | ||
{ | ||
Contract.ThrowIfTrue(_openedDocumentIds.Contains(info.DocumentId)); | ||
|
||
workspace.OnDocumentAdded(info.DocumentInfo); | ||
workspace.OnDocumentOpened(info.DocumentId, sourceTextContainer); | ||
documentId = info.DocumentId; | ||
_openedDocumentIds.Add(documentId); | ||
return true; | ||
} | ||
|
||
documentId = null; | ||
return false; | ||
} | ||
} | ||
|
||
public bool TryRemoveDocumentFromWorkspace(MetadataAsSourceWorkspace workspace, string filePath) | ||
{ | ||
lock (_gate) | ||
{ | ||
if (_fileToDocumentInfoMap.TryGetValue(filePath, out var info)) | ||
{ | ||
// In LSP, while calls to TryAddDocumentToWorkspace and TryRemoveDocumentFromWorkspace are handled | ||
// serially, it is possible that TryRemoveDocumentFromWorkspace called without TryAddDocumentToWorkspace first. | ||
// This can happen if the document is immediately closed after opening - only feature requests that force us | ||
// to materialize a solution will trigger TryAddDocumentToWorkspace, if none are made it is never called. | ||
// However TryRemoveDocumentFromWorkspace is always called on close. | ||
if (_openedDocumentIds.Contains(info.DocumentId)) | ||
{ | ||
workspace.OnDocumentClosed(info.DocumentId, new WorkspaceFileTextLoader(workspace.Services.SolutionServices, filePath, info.Encoding)); | ||
workspace.OnDocumentRemoved(info.DocumentId); | ||
_openedDocumentIds.Remove(info.DocumentId); | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
public Project? MapDocument(Document document) | ||
{ | ||
if (document.FilePath is not null && | ||
|
@@ -462,7 +406,6 @@ public void CleanupGeneratedFiles(MetadataAsSourceWorkspace workspace) | |
|
||
// The MetadataAsSourceFileService will clean up the entire temp folder so no need to do anything here | ||
_fileToDocumentInfoMap.Clear(); | ||
_openedDocumentIds.Clear(); | ||
_sourceLinkEnabledProjects.Clear(); | ||
_implementationAssemblyLookupService.Clear(); | ||
} | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 this still be returning false? I'd assume we don't say we removed something if we didn't add it in the first place? It seems like this is missing a different return for the "if not open" case.
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 is deleted in the followup (open doesn't always make sense here)
#79023