Skip to content

Commit 83387e5

Browse files
committed
Add test for renaming to an existing file, and fix the bug that the test found
1 parent 649286d commit 83387e5

File tree

5 files changed

+40
-13
lines changed

5 files changed

+40
-13
lines changed

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Refactoring/RenameEndpoint.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,16 @@ public void ApplyCapabilities(VSInternalServerCapabilities serverCapabilities, V
4848

4949
protected override string CustomMessageTarget => CustomMessageNames.RazorRenameEndpointName;
5050

51-
protected override Task<WorkspaceEdit?> TryHandleAsync(RenameParams request, RazorRequestContext requestContext, DocumentPositionInfo positionInfo, CancellationToken cancellationToken)
51+
protected override async Task<WorkspaceEdit?> TryHandleAsync(RenameParams request, RazorRequestContext requestContext, DocumentPositionInfo positionInfo, CancellationToken cancellationToken)
5252
{
5353
var documentContext = requestContext.DocumentContext;
5454
if (documentContext is null)
5555
{
56-
return SpecializedTasks.Null<WorkspaceEdit>();
56+
return null;
5757
}
5858

59-
return _renameService.TryGetRazorRenameEditsAsync(documentContext, positionInfo, request.NewName, _projectManager.GetQueryOperations(), cancellationToken);
59+
var result = await _renameService.TryGetRazorRenameEditsAsync(documentContext, positionInfo, request.NewName, _projectManager.GetQueryOperations(), cancellationToken).ConfigureAwait(false);
60+
return result.Edit;
6061
}
6162

6263
protected override bool IsSupported()

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ namespace Microsoft.CodeAnalysis.Razor.Rename;
1010

1111
internal interface IRenameService
1212
{
13-
Task<WorkspaceEdit?> TryGetRazorRenameEditsAsync(
13+
Task<RenameResult> TryGetRazorRenameEditsAsync(
1414
DocumentContext documentContext,
1515
DocumentPositionInfo positionInfo,
1616
string newName,
1717
ISolutionQueryOperations solutionQueryOperations,
1818
CancellationToken cancellationToken);
1919
}
20+
21+
internal readonly record struct RenameResult(WorkspaceEdit? Edit, bool FallbackToCSharp = true);

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ internal class RenameService(
3131
private readonly IFileSystem _fileSystem = fileSystem;
3232
private readonly LanguageServerFeatureOptions _languageServerFeatureOptions = languageServerFeatureOptions;
3333

34-
public async Task<WorkspaceEdit?> TryGetRazorRenameEditsAsync(
34+
public async Task<RenameResult> TryGetRazorRenameEditsAsync(
3535
DocumentContext documentContext,
3636
DocumentPositionInfo positionInfo,
3737
string newName,
@@ -41,30 +41,32 @@ internal class RenameService(
4141
// We only support renaming of .razor components, not .cshtml tag helpers
4242
if (!documentContext.FileKind.IsComponent())
4343
{
44-
return null;
44+
return new(Edit: null);
4545
}
4646

4747
var codeDocument = await documentContext.GetCodeDocumentAsync(cancellationToken).ConfigureAwait(false);
4848

4949
var originTagHelpers = await GetOriginTagHelpersAsync(documentContext, positionInfo.HostDocumentIndex, cancellationToken).ConfigureAwait(false);
5050
if (originTagHelpers.IsDefaultOrEmpty)
5151
{
52-
return null;
52+
return new(Edit: null);
5353
}
5454

5555
var originComponentDocumentSnapshot = await _componentSearchEngine
5656
.TryLocateComponentAsync(originTagHelpers.First(), solutionQueryOperations, cancellationToken)
5757
.ConfigureAwait(false);
5858
if (originComponentDocumentSnapshot is null)
5959
{
60-
return null;
60+
return new(Edit: null);
6161
}
6262

6363
var originComponentDocumentFilePath = originComponentDocumentSnapshot.FilePath;
6464
var newPath = MakeNewPath(originComponentDocumentFilePath, newName);
6565
if (_fileSystem.FileExists(newPath))
6666
{
67-
return null;
67+
// We found a tag, but the new name would cause a conflict, so we can't proceed with the rename,
68+
// even if C# might have worked.
69+
return new(Edit: null, FallbackToCSharp: false);
6870
}
6971

7072
using var _ = ListPool<SumType<TextDocumentEdit, CreateFile, RenameFile, DeleteFile>>.GetPooledObject(out var documentChanges);
@@ -88,10 +90,10 @@ internal class RenameService(
8890
}
8991
}
9092

91-
return new WorkspaceEdit
93+
return new(new WorkspaceEdit
9294
{
9395
DocumentChanges = documentChanges.ToArray(),
94-
};
96+
});
9597
}
9698

9799
private static ImmutableArray<IDocumentSnapshot> GetAllDocumentSnapshots(string filePath, ISolutionQueryOperations solutionQueryOperations)

src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/Rename/RemoteRenameService.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,21 @@ protected override IRemoteRenameService CreateService(in ServiceArgs args)
5858
.TryGetRazorRenameEditsAsync(context, positionInfo, newName, context.GetSolutionQueryOperations(), cancellationToken)
5959
.ConfigureAwait(false);
6060

61-
if (razorEdit is not null)
61+
if (razorEdit.Edit is { } edit)
6262
{
63-
return Results(razorEdit);
63+
return Results(edit);
6464
}
6565

6666
if (positionInfo.LanguageKind != CodeAnalysis.Razor.Protocol.RazorLanguageKind.CSharp)
6767
{
6868
return CallHtml;
6969
}
7070

71+
if (!razorEdit.FallbackToCSharp)
72+
{
73+
return NoFurtherHandling;
74+
}
75+
7176
var csharpEdit = await ExternalHandlers.Rename
7277
.GetRenameEditAsync(generatedDocument, positionInfo.Position.ToLinePosition(), newName, cancellationToken)
7378
.ConfigureAwait(false);

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,23 @@ public class HasJacketAttribute : Attribute
316316
""")
317317
]);
318318

319+
[Fact]
320+
public Task Component_ExistingFile()
321+
=> VerifyRenamesAsync(
322+
input: $"""
323+
This is a Razor document.
324+
325+
<Comp$$onent />
326+
327+
The end.
328+
""",
329+
additionalFiles: [
330+
(FilePath("Component.razor"), ""),
331+
(FilePath("DifferentName.razor"), "")
332+
],
333+
newName: "DifferentName",
334+
expected: "");
335+
319336
[Theory]
320337
[InlineData("$$Component")]
321338
[InlineData("Com$$ponent")]

0 commit comments

Comments
 (0)