Skip to content

Commit 97a39ad

Browse files
CopilotEvangelink
andcommitted
Fix same-document editor conflict in multi-file partial class support
Co-authored-by: Evangelink <[email protected]>
1 parent 6f67f7a commit 97a39ad

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/Analyzers/MSTest.Analyzers.CodeFixes/PreferDisposeOverTestCleanupFixer.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,24 @@ private static async Task<Solution> AddDisposeAndBaseClassAsync(
136136
newDisposeMethod = existingDisposeMethod.WithBody(cleanupBody);
137137
}
138138

139-
// Update the dispose document
140-
DocumentEditor disposeEditor = await DocumentEditor.CreateAsync(disposeDocument, cancellationToken).ConfigureAwait(false);
141-
disposeEditor.ReplaceNode(existingDisposeMethod, newDisposeMethod);
142-
solution = solution.WithDocumentSyntaxRoot(disposeDocument.Id, disposeEditor.GetChangedRoot());
143-
144-
// Remove the TestCleanup method from the current document
145-
editor.RemoveNode(testCleanupMethod);
146-
solution = solution.WithDocumentSyntaxRoot(document.Id, editor.GetChangedRoot());
139+
if (disposeDocument.Id == document.Id)
140+
{
141+
// Both methods are in the same document, use single editor
142+
editor.ReplaceNode(existingDisposeMethod, newDisposeMethod);
143+
editor.RemoveNode(testCleanupMethod);
144+
solution = solution.WithDocumentSyntaxRoot(document.Id, editor.GetChangedRoot());
145+
}
146+
else
147+
{
148+
// Methods are in different documents, use separate editors
149+
DocumentEditor disposeEditor = await DocumentEditor.CreateAsync(disposeDocument, cancellationToken).ConfigureAwait(false);
150+
disposeEditor.ReplaceNode(existingDisposeMethod, newDisposeMethod);
151+
solution = solution.WithDocumentSyntaxRoot(disposeDocument.Id, disposeEditor.GetChangedRoot());
152+
153+
// Remove the TestCleanup method from the current document
154+
editor.RemoveNode(testCleanupMethod);
155+
solution = solution.WithDocumentSyntaxRoot(document.Id, editor.GetChangedRoot());
156+
}
147157
}
148158
else
149159
{

0 commit comments

Comments
 (0)