-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Allow modification of source generated documents #77587
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
Changes from all commits
35c369c
ff977e0
b305748
a4741c0
d72827f
c465ec3
fbb41aa
7553622
a41720b
fd92422
3ba9684
c98b091
bbdef4f
1f4564f
5260136
81cb8a9
ed74582
5f0dd64
fcd2d4d
582edc2
07df90a
5a075b0
703d071
95ff002
b5a8879
e4dfe81
8d08511
8bc17a8
837b368
fd086b1
3a68fc2
645611f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -388,13 +388,33 @@ public Document WithSourceCodeKind(SourceCodeKind kind) | |
/// Creates a new instance of this document updated to have the text specified. | ||
/// </summary> | ||
public Document WithText(SourceText text) | ||
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. It feels a bit weird this method isn't just virtual rather than having the base type explicitly deal with the source-generated case, but it's fine I guess. 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 think you commented this before, and I think it's a good idea, but making this virtual would be a public API change that needs to go through that process, right? So a follow up would be a lot easier |
||
=> this.Project.Solution.WithDocumentText(this.Id, text, PreservationMode.PreserveIdentity).GetRequiredDocument(Id); | ||
{ | ||
var solution = this.Project.Solution.WithDocumentText(this.Id, text, PreservationMode.PreserveIdentity); | ||
|
||
if (Id.IsSourceGenerated) | ||
{ | ||
// We just modified the text of the generated document, so it should be available synchronously, and throwing is appropriate if it isn't. | ||
return solution.GetRequiredSourceGeneratedDocumentForAlreadyGeneratedId(Id); | ||
} | ||
davidwengier marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return solution.GetRequiredDocument(Id); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a new instance of this document updated to have a syntax tree rooted by the specified syntax node. | ||
/// </summary> | ||
public Document WithSyntaxRoot(SyntaxNode root) | ||
=> this.Project.Solution.WithDocumentSyntaxRoot(this.Id, root, PreservationMode.PreserveIdentity).GetRequiredDocument(Id); | ||
{ | ||
var solution = this.Project.Solution.WithDocumentSyntaxRoot(this.Id, root, PreservationMode.PreserveIdentity); | ||
|
||
if (Id.IsSourceGenerated) | ||
{ | ||
// We just modified the text of the generated document, so it should be available synchronously, and throwing is appropriate if it isn't. | ||
return solution.GetRequiredSourceGeneratedDocumentForAlreadyGeneratedId(Id); | ||
} | ||
|
||
return solution.GetRequiredDocument(Id); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a new instance of this document updated to have the specified name. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1616,7 +1616,8 @@ public Solution WithDocumentText(IEnumerable<DocumentId?> documentIds, SourceTex | |
internal Document WithFrozenSourceGeneratedDocument( | ||
SourceGeneratedDocumentIdentity documentIdentity, DateTime generationDateTime, SourceText text) | ||
{ | ||
var newCompilationState = CompilationState.WithFrozenSourceGeneratedDocuments([(documentIdentity, generationDateTime, text)]); | ||
// SyntaxNode is null here because it will be computed on demand. Other APIs, like Document.WithSyntaxRoot, specify it. | ||
var newCompilationState = CompilationState.WithFrozenSourceGeneratedDocuments([(documentIdentity, generationDateTime, text, syntaxNode: null)]); | ||
davidwengier marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var newSolution = WithCompilationState(newCompilationState); | ||
|
||
var newDocumentState = newCompilationState.TryGetSourceGeneratedDocumentStateForAlreadyGeneratedId(documentIdentity.DocumentId); | ||
|
@@ -1627,7 +1628,7 @@ internal Document WithFrozenSourceGeneratedDocument( | |
} | ||
|
||
internal Solution WithFrozenSourceGeneratedDocuments(ImmutableArray<(SourceGeneratedDocumentIdentity documentIdentity, DateTime generationDateTime, SourceText text)> documents) | ||
=> WithCompilationState(CompilationState.WithFrozenSourceGeneratedDocuments(documents)); | ||
=> WithCompilationState(CompilationState.WithFrozenSourceGeneratedDocuments(documents.SelectAsArray(d => (d.documentIdentity, d.generationDateTime, d.text, (SyntaxNode?)null)))); | ||
|
||
/// <inheritdoc cref="SolutionCompilationState.UpdateSpecificSourceGeneratorExecutionVersions"/> | ||
internal Solution UpdateSpecificSourceGeneratorExecutionVersions(SourceGeneratorExecutionVersionMap sourceGeneratorExecutionVersionMap) | ||
|
@@ -1740,9 +1741,26 @@ private void CheckContainsDocument(DocumentId documentId) | |
throw new ArgumentNullException(nameof(documentId)); | ||
} | ||
|
||
if (!ContainsDocument(documentId)) | ||
// For source generated documents we expect them to be already generated to use any of the APIs that call this | ||
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. Why was this needed? 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. The first thing the |
||
if (documentId.IsSourceGenerated && ContainsSourceGeneratedDocument(documentId)) | ||
{ | ||
throw new InvalidOperationException(WorkspaceExtensionsResources.The_solution_does_not_contain_the_specified_document); | ||
return; | ||
} | ||
davidwengier marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (ContainsDocument(documentId)) | ||
{ | ||
return; | ||
} | ||
|
||
throw new InvalidOperationException(WorkspaceExtensionsResources.The_solution_does_not_contain_the_specified_document); | ||
|
||
bool ContainsSourceGeneratedDocument(DocumentId documentId) | ||
{ | ||
var project = this.GetProject(documentId.ProjectId); | ||
if (project is null) | ||
return false; | ||
|
||
return project.TryGetSourceGeneratedDocumentForAlreadyGeneratedId(documentId) is not null; | ||
} | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.