Skip to content

Commit 2fb8106

Browse files
Merge pull request #56189 from jasonmalinowski/allow-rename-with-references-in-generated-code
Don't try to rename references in source generated files
2 parents 7955a0a + 82a4adb commit 2fb8106

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/EditorFeatures/Test2/Rename/CSharp/SourceGeneratorTests.vb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,28 @@ public class GeneratedClass
3333
End Using
3434
End Sub
3535

36+
<Theory, CombinatorialData, Trait(Traits.Feature, Traits.Features.Rename)>
37+
Public Sub RenameWithReferenceInGeneratedFile(host As RenameTestHost)
38+
Using result = RenameEngineResult.Create(_outputHelper,
39+
<Workspace>
40+
<Project Language="C#" AssemblyName="ClassLibrary1" CommonReferences="true">
41+
<Document>
42+
public class [|$$RegularClass|]
43+
{
44+
}
45+
</Document>
46+
<DocumentFromSourceGenerator>
47+
public class GeneratedClass
48+
{
49+
public void M(RegularClass c) { }
50+
}
51+
</DocumentFromSourceGenerator>
52+
</Project>
53+
</Workspace>, host:=host, renameTo:="A")
54+
55+
End Using
56+
End Sub
57+
3658
<Theory, CombinatorialData, Trait(Traits.Feature, Traits.Features.Rename)>
3759
<WorkItem(51537, "https://github.com/dotnet/roslyn/issues/51537")>
3860
Public Sub RenameWithCascadeIntoGeneratedFile(host As RenameTestHost)

src/Workspaces/Core/Portable/Rename/RenameLocation.ReferenceProcessing.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,13 @@ void AddRenameLocationIfNotGenerated(Location location, bool isRenamableAccessor
360360

361361
internal static async Task<IEnumerable<RenameLocation>> GetRenamableReferenceLocationsAsync(ISymbol referencedSymbol, ISymbol originalSymbol, ReferenceLocation location, Solution solution, CancellationToken cancellationToken)
362362
{
363+
// We won't try to update references in source generated files; we'll assume the generator will rerun
364+
// and produce an updated document with the new name.
365+
if (location.Document is SourceGeneratedDocument)
366+
{
367+
return SpecializedCollections.EmptyEnumerable<RenameLocation>();
368+
}
369+
363370
var shouldIncludeSymbol = await ShouldIncludeSymbolAsync(referencedSymbol, originalSymbol, solution, true, cancellationToken).ConfigureAwait(false);
364371
if (!shouldIncludeSymbol)
365372
{

0 commit comments

Comments
 (0)