Skip to content

Commit 2fa323c

Browse files
Merge pull request #72695 from juan-carlos-diaz/issue-72632
2 parents 2c3e10f + ffa1aac commit 2fa323c

File tree

6 files changed

+55
-3
lines changed

6 files changed

+55
-3
lines changed

src/EditorFeatures/CSharpTest/CodeActions/MoveType/MoveTypeTests.MoveToNewFile.cs

+28
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,34 @@ class Class2 { }
535535
await TestMoveTypeToNewFileAsync(code, codeAfterMove, expectedDocumentName, destinationDocumentText, index: 1);
536536
}
537537

538+
[WpfFact, WorkItem("https://github.com/dotnet/roslyn/issues/72632")]
539+
public async Task MoveNestedTypeToNewFile_Simple_DottedName_WithPrimaryConstructor()
540+
{
541+
var code =
542+
@"internal class Outer()
543+
{
544+
private class Inner[||]
545+
{
546+
}
547+
}";
548+
549+
var codeAfterMove =
550+
@"internal partial class Outer()
551+
{
552+
}";
553+
554+
var expectedDocumentName = "Outer.Inner.cs";
555+
556+
var destinationDocumentText =
557+
@"internal partial class Outer
558+
{
559+
private class Inner
560+
{
561+
}
562+
}";
563+
await TestMoveTypeToNewFileAsync(code, codeAfterMove, expectedDocumentName, destinationDocumentText, index: 1);
564+
}
565+
538566
[WpfFact]
539567
public async Task MoveNestedTypeToNewFile_ParentHasOtherMembers()
540568
{

src/Features/Core/Portable/CodeRefactorings/MoveType/AbstractMoveTypeService.MoveTypeEditor.cs

+9-3
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ private async Task<Document> AddNewDocumentWithSingleTypeDeclarationAsync(Docume
125125
// attributes from the containing partial types. We don't want to create
126126
// duplicate attributes on things.
127127
AddPartialModifiersToTypeChain(
128-
documentEditor, removeAttributesAndComments: true, removeTypeInheritance: true);
128+
documentEditor, removeAttributesAndComments: true, removeTypeInheritance: true, removePrimaryConstructor: true);
129129

130130
// remove things that are not being moved, from the forked document.
131131
var membersToRemove = GetMembersToRemove(root);
@@ -193,7 +193,7 @@ private async Task<Solution> RemoveTypeFromSourceDocumentAsync(Document sourceDo
193193
// However, keep all the attributes on these types as theses are the
194194
// original attributes and we don't want to mess with them.
195195
AddPartialModifiersToTypeChain(documentEditor,
196-
removeAttributesAndComments: false, removeTypeInheritance: false);
196+
removeAttributesAndComments: false, removeTypeInheritance: false, removePrimaryConstructor: false);
197197
documentEditor.RemoveNode(State.TypeNode, SyntaxRemoveOptions.KeepUnbalancedDirectives);
198198

199199
var updatedDocument = documentEditor.GetChangedDocument();
@@ -258,7 +258,8 @@ TMemberDeclarationSyntax or
258258
private void AddPartialModifiersToTypeChain(
259259
DocumentEditor documentEditor,
260260
bool removeAttributesAndComments,
261-
bool removeTypeInheritance)
261+
bool removeTypeInheritance,
262+
bool removePrimaryConstructor)
262263
{
263264
var semanticFacts = State.SemanticDocument.Document.GetRequiredLanguageService<ISemanticFactsService>();
264265
var typeChain = State.TypeNode.Ancestors().OfType<TTypeDeclarationSyntax>();
@@ -283,6 +284,11 @@ private void AddPartialModifiersToTypeChain(
283284
{
284285
documentEditor.RemoveAllTypeInheritance(node);
285286
}
287+
288+
if (removePrimaryConstructor)
289+
{
290+
documentEditor.RemovePrimaryConstructor(node);
291+
}
286292
}
287293

288294
documentEditor.ReplaceNode(State.TypeNode,

src/Workspaces/CSharp/Portable/CodeGeneration/CSharpSyntaxGenerator.cs

+3
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,9 @@ private static SyntaxNode WithAttributeLists(SyntaxNode declaration, SyntaxList<
12001200
_ => declaration,
12011201
};
12021202

1203+
internal override SyntaxNode? GetPrimaryConstructorParameterList(SyntaxNode declaration)
1204+
=> declaration is TypeDeclarationSyntax { ParameterList: { } parameterList } ? parameterList : null;
1205+
12031206
internal override ImmutableArray<SyntaxNode> GetTypeInheritance(SyntaxNode declaration)
12041207
=> declaration is BaseTypeDeclarationSyntax baseType && baseType.BaseList != null
12051208
? [baseType.BaseList]

src/Workspaces/Core/Portable/Editing/SyntaxEditorExtensions.cs

+3
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,7 @@ public static void AddInterfaceType(this SyntaxEditor editor, SyntaxNode declara
7575

7676
public static void AddBaseType(this SyntaxEditor editor, SyntaxNode declaration, SyntaxNode baseType)
7777
=> editor.ReplaceNode(declaration, (d, g) => g.AddBaseType(d, baseType));
78+
79+
internal static void RemovePrimaryConstructor(this SyntaxEditor editor, SyntaxNode declaration)
80+
=> editor.ReplaceNode(declaration, (d, g) => g.RemovePrimaryConstructor(d));
7881
}

src/Workspaces/Core/Portable/Editing/SyntaxGenerator.cs

+8
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,14 @@ public SyntaxNode RemoveAllAttributes(SyntaxNode declaration)
10711071
/// </summary>
10721072
internal abstract SyntaxNode RemoveAllComments(SyntaxNode node);
10731073

1074+
internal SyntaxNode RemovePrimaryConstructor(SyntaxNode declaration)
1075+
{
1076+
var node = GetPrimaryConstructorParameterList(declaration);
1077+
return RemoveNodes(declaration, node is not null ? [node] : []);
1078+
}
1079+
1080+
internal abstract SyntaxNode? GetPrimaryConstructorParameterList(SyntaxNode declaration);
1081+
10741082
internal SyntaxNode RemoveLeadingAndTrailingComments(SyntaxNode node)
10751083
{
10761084
return node.WithLeadingTrivia(RemoveCommentLines(node.GetLeadingTrivia()))

src/Workspaces/VisualBasic/Portable/CodeGeneration/VisualBasicSyntaxGenerator.vb

+4
Original file line numberDiff line numberDiff line change
@@ -1675,6 +1675,10 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeGeneration
16751675
Return attr.WithTarget(Nothing)
16761676
End Function
16771677

1678+
Friend Overrides Function GetPrimaryConstructorParameterList(declaration As SyntaxNode) As SyntaxNode
1679+
Return Nothing
1680+
End Function
1681+
16781682
Friend Overrides Function GetTypeInheritance(declaration As SyntaxNode) As ImmutableArray(Of SyntaxNode)
16791683
Dim typeDecl = TryCast(declaration, TypeBlockSyntax)
16801684
If typeDecl Is Nothing Then

0 commit comments

Comments
 (0)