Skip to content

Commit 85eb7ef

Browse files
committed
Rename additional files when renaming Razor files
1 parent 83387e5 commit 85eb7ef

File tree

2 files changed

+200
-4
lines changed

2 files changed

+200
-4
lines changed

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Rename/RenameService.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ public async Task<RenameResult> TryGetRazorRenameEditsAsync(
7070
}
7171

7272
using var _ = ListPool<SumType<TextDocumentEdit, CreateFile, RenameFile, DeleteFile>>.GetPooledObject(out var documentChanges);
73-
var fileRename = GetFileRenameForComponent(originComponentDocumentSnapshot, newPath);
73+
var fileRename = GetRenameFileEdit(originComponentDocumentFilePath, newPath);
7474
documentChanges.Add(fileRename);
7575
AddEditsForCodeDocument(documentChanges, originTagHelpers, newName, new(documentContext.Uri), codeDocument);
76+
AddAdditionalFileRenames(documentChanges, originComponentDocumentFilePath, newPath);
7677

7778
var documentSnapshots = GetAllDocumentSnapshots(documentContext.FilePath, solutionQueryOperations);
7879

@@ -130,11 +131,26 @@ private static ImmutableArray<IDocumentSnapshot> GetAllDocumentSnapshots(string
130131
return documentSnapshots.ToImmutableAndClear();
131132
}
132133

133-
private RenameFile GetFileRenameForComponent(IDocumentSnapshot documentSnapshot, string newPath)
134+
private void AddAdditionalFileRenames(List<SumType<TextDocumentEdit, CreateFile, RenameFile, DeleteFile>> documentChanges, string oldFilePath, string newFilePath)
135+
{
136+
TryAdd(".cs");
137+
TryAdd(".css");
138+
139+
void TryAdd(string extension)
140+
{
141+
var changedPath = oldFilePath + extension;
142+
if (_fileSystem.FileExists(changedPath))
143+
{
144+
documentChanges.Add(GetRenameFileEdit(changedPath, newFilePath + extension));
145+
}
146+
}
147+
}
148+
149+
private RenameFile GetRenameFileEdit(string oldFilePath, string newFilePath)
134150
=> new RenameFile
135151
{
136-
OldDocumentUri = new(LspFactory.CreateFilePathUri(documentSnapshot.FilePath, _languageServerFeatureOptions)),
137-
NewDocumentUri = new(LspFactory.CreateFilePathUri(newPath, _languageServerFeatureOptions)),
152+
OldDocumentUri = new(LspFactory.CreateFilePathUri(oldFilePath, _languageServerFeatureOptions)),
153+
NewDocumentUri = new(LspFactory.CreateFilePathUri(newFilePath, _languageServerFeatureOptions)),
138154
};
139155

140156
private static string MakeNewPath(string originalPath, string newName)

src/Razor/test/Microsoft.VisualStudioCode.RazorExtension.Test/Endpoints/Shared/CohostRenameEndpointTest.cs

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using Microsoft.AspNetCore.Razor.Test.Common;
99
using Microsoft.CodeAnalysis;
1010
using Microsoft.CodeAnalysis.ExternalAccess.Razor;
11+
using Microsoft.CodeAnalysis.Razor.Workspaces;
12+
using Microsoft.CodeAnalysis.Remote.Razor;
1113
using Microsoft.CodeAnalysis.Testing;
1214
using Microsoft.CodeAnalysis.Text;
1315
using Xunit;
@@ -847,6 +849,184 @@ The end.
847849
""")
848850
]);
849851

852+
[Fact]
853+
public Task Component_OwnFile_WithCss()
854+
=> VerifyRenamesAsync(
855+
input: $"""
856+
This is a Razor document.
857+
858+
<Fi$$le1 />
859+
<File1></File1>
860+
<File1>
861+
</File1>
862+
863+
The end.
864+
""",
865+
additionalFiles: [
866+
(FilePath("File1.razor.css"), "")
867+
],
868+
newName: "ABetterName",
869+
expected: """
870+
This is a Razor document.
871+
872+
<ABetterName />
873+
<ABetterName></ABetterName>
874+
<ABetterName>
875+
</ABetterName>
876+
877+
The end.
878+
""",
879+
newFileUri: FileUri("ABetterName.razor"),
880+
additionalExpectedFiles: [
881+
(FileUri("ABetterName.razor.css"), "")]);
882+
883+
[Fact]
884+
public Task Component_OwnFile_WithCodeBehind()
885+
=> VerifyRenamesAsync(
886+
input: $"""
887+
This is a Razor document.
888+
889+
<Fi$$le1 />
890+
<File1></File1>
891+
<File1>
892+
</File1>
893+
894+
The end.
895+
""",
896+
additionalFiles: [
897+
(FilePath("File1.razor.cs"), """
898+
namespace SomeProject;
899+
900+
// This class name should change, but we don't support that yet
901+
public partial class File1
902+
{
903+
}
904+
""")
905+
],
906+
newName: "ABetterName",
907+
expected: """
908+
This is a Razor document.
909+
910+
<ABetterName />
911+
<ABetterName></ABetterName>
912+
<ABetterName>
913+
</ABetterName>
914+
915+
The end.
916+
""",
917+
newFileUri: FileUri("ABetterName.razor"),
918+
additionalExpectedFiles: [
919+
(FileUri("ABetterName.razor.cs"), """
920+
namespace SomeProject;
921+
922+
// This class name should change, but we don't support that yet
923+
public partial class File1
924+
{
925+
}
926+
""")]);
927+
928+
[Fact]
929+
public Task Component_WithOtherFile_WithCss()
930+
=> VerifyRenamesAsync(
931+
input: $"""
932+
This is a Razor document.
933+
934+
<Comp$$onent />
935+
<Component></Component>
936+
<Component>
937+
</Component>
938+
939+
The end.
940+
""",
941+
additionalFiles: [
942+
(FilePath("Component.razor.css"), ""),
943+
(FilePath("Component.razor"), """
944+
<Component />
945+
<Component></Component>
946+
<Component>
947+
</Component>
948+
""")
949+
],
950+
newName: "DifferentName",
951+
expected: """
952+
This is a Razor document.
953+
954+
<DifferentName />
955+
<DifferentName></DifferentName>
956+
<DifferentName>
957+
</DifferentName>
958+
959+
The end.
960+
""",
961+
additionalExpectedFiles: [
962+
(FileUri("DifferentName.razor.css"), ""),
963+
(FileUri("DifferentName.razor"), """
964+
<DifferentName />
965+
<DifferentName></DifferentName>
966+
<DifferentName>
967+
</DifferentName>
968+
"""),
969+
]);
970+
971+
[Fact]
972+
public Task Component_WithOtherFile_WithCodeBehindAndCss()
973+
=> VerifyRenamesAsync(
974+
input: $"""
975+
This is a Razor document.
976+
977+
<Comp$$onent />
978+
<Component></Component>
979+
<Component>
980+
</Component>
981+
982+
The end.
983+
""",
984+
additionalFiles: [
985+
(FilePath("Component.razor.css"), ""),
986+
(FilePath("Component.razor.cs"), """
987+
namespace SomeProject;
988+
989+
// This class name should change, but we don't support that yet
990+
public partial class Component
991+
{
992+
}
993+
"""),
994+
(FilePath("Component.razor"), """
995+
<Component />
996+
<Component></Component>
997+
<Component>
998+
</Component>
999+
""")
1000+
],
1001+
newName: "DifferentName",
1002+
expected: """
1003+
This is a Razor document.
1004+
1005+
<DifferentName />
1006+
<DifferentName></DifferentName>
1007+
<DifferentName>
1008+
</DifferentName>
1009+
1010+
The end.
1011+
""",
1012+
additionalExpectedFiles: [
1013+
(FileUri("DifferentName.razor.css"), ""),
1014+
(FileUri("DifferentName.razor.cs"), """
1015+
namespace SomeProject;
1016+
1017+
// This class name should change, but we don't support that yet
1018+
public partial class Component
1019+
{
1020+
}
1021+
"""),
1022+
(FileUri("DifferentName.razor"), """
1023+
<DifferentName />
1024+
<DifferentName></DifferentName>
1025+
<DifferentName>
1026+
</DifferentName>
1027+
"""),
1028+
]);
1029+
8501030
private async Task VerifyRenamesAsync(
8511031
string input,
8521032
string newName,

0 commit comments

Comments
 (0)