-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement textDocument/prepareRename for better rename experience
- Loading branch information
Showing
3 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/Features/LanguageServer/Protocol/Handler/Rename/PrepareRenameHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Composition; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.Host.Mef; | ||
using Microsoft.CodeAnalysis.Rename; | ||
using Microsoft.VisualStudio.LanguageServer.Protocol; | ||
using Roslyn.Utilities; | ||
|
||
namespace Microsoft.CodeAnalysis.LanguageServer.Handler; | ||
|
||
[ExportCSharpVisualBasicStatelessLspService(typeof(PrepareRenameHandler)), Shared] | ||
[Method(Methods.TextDocumentPrepareRenameName)] | ||
internal class PrepareRenameHandler : ILspServiceDocumentRequestHandler<PrepareRenameParams, DefaultBehaviorPrepareRename?> | ||
{ | ||
[ImportingConstructor] | ||
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)] | ||
public PrepareRenameHandler() | ||
{ | ||
} | ||
|
||
public bool MutatesSolutionState => false; | ||
public bool RequiresLSPSolution => true; | ||
|
||
public TextDocumentIdentifier GetTextDocumentIdentifier(PrepareRenameParams request) => request.TextDocument; | ||
|
||
public async Task<DefaultBehaviorPrepareRename?> HandleRequestAsync(PrepareRenameParams request, RequestContext context, CancellationToken cancellationToken) | ||
{ | ||
var document = context.Document; | ||
Contract.ThrowIfNull(document); | ||
|
||
var position = await document.GetPositionFromLinePositionAsync(ProtocolConversions.PositionToLinePosition(request.Position), cancellationToken).ConfigureAwait(false); | ||
|
||
var symbolicRenameInfo = await SymbolicRenameInfo.GetRenameInfoAsync( | ||
document, position, cancellationToken).ConfigureAwait(false); | ||
if (symbolicRenameInfo.IsError) | ||
return null; | ||
|
||
return new DefaultBehaviorPrepareRename | ||
{ | ||
DefaultBehavior = true, | ||
}; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/Features/LanguageServer/ProtocolUnitTests/Rename/PrepareRenameTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Roslyn.Test.Utilities; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
using LSP = Microsoft.VisualStudio.LanguageServer.Protocol; | ||
|
||
namespace Microsoft.CodeAnalysis.LanguageServer.UnitTests.Rename | ||
{ | ||
public class PrepareRenameTests : AbstractLanguageServerProtocolTests | ||
{ | ||
public PrepareRenameTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper) | ||
{ | ||
} | ||
|
||
[Theory, CombinatorialData] | ||
public async Task TestPrepareRenameValidLocationAsync(bool mutatingLspWorkspace) | ||
{ | ||
var markup = | ||
@"class A | ||
{ | ||
void {|caret:|}M() | ||
{ | ||
} | ||
}"; | ||
await using var testLspServer = await CreateTestLspServerAsync(markup, mutatingLspWorkspace); | ||
var renameLocation = testLspServer.GetLocations("caret").First(); | ||
|
||
var results = await RunPrepareRenameAsync(testLspServer, CreatePrepareRenameParams(renameLocation)); | ||
Assert.True(results?.DefaultBehavior); | ||
} | ||
|
||
[Theory, CombinatorialData] | ||
public async Task TestPrepareRenameInvalidLocationAsync(bool mutatingLspWorkspace) | ||
{ | ||
var markup = | ||
@"class A | ||
{ | ||
// Cannot rename {|caret:|}inside a comment. | ||
void M() | ||
{ | ||
} | ||
}"; | ||
await using var testLspServer = await CreateTestLspServerAsync(markup, mutatingLspWorkspace); | ||
var renameLocation = testLspServer.GetLocations("caret").First(); | ||
|
||
var results = await RunPrepareRenameAsync(testLspServer, CreatePrepareRenameParams(renameLocation)); | ||
Assert.Null(results); | ||
} | ||
|
||
private static LSP.PrepareRenameParams CreatePrepareRenameParams(LSP.Location location) | ||
=> new LSP.PrepareRenameParams() | ||
{ | ||
Position = location.Range.Start, | ||
TextDocument = CreateTextDocumentIdentifier(location.Uri) | ||
}; | ||
|
||
private static async Task<LSP.DefaultBehaviorPrepareRename?> RunPrepareRenameAsync(TestLspServer testLspServer, LSP.PrepareRenameParams prepareRenameParams) | ||
{ | ||
return await testLspServer.ExecuteRequestAsync<LSP.PrepareRenameParams, LSP.DefaultBehaviorPrepareRename?>(LSP.Methods.TextDocumentPrepareRenameName, prepareRenameParams, CancellationToken.None); | ||
} | ||
} | ||
} |