Skip to content
Merged
2 changes: 1 addition & 1 deletion src/EditorFeatures/Core/IInlineRenameSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@ internal interface IInlineRenameSession
/// <remarks>
/// It will only be async when InlineRenameUIOptionsStorage.CommitRenameAsynchronously is set to true.
/// </remarks>
Task CommitAsync(bool previewChanges, IUIThreadOperationContext editorOperationContext = null);
Task CommitAsync(bool previewChanges, IUIThreadOperationContext editorOperationContext);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,18 @@
using System.Linq;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.InlineRename;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.TestHooks;
using Microsoft.VisualStudio.Commanding;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Editor.Commanding;
using Microsoft.VisualStudio.Utilities;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.Editor.Implementation.InlineRename;

internal abstract partial class AbstractRenameCommandHandler(
IThreadingContext threadingContext,
InlineRenameService renameService,
IGlobalOptionService globalOptionService,
IAsynchronousOperationListener listener)
{
public string DisplayName => EditorFeaturesResources.Rename;
Expand Down Expand Up @@ -76,7 +72,7 @@ private void HandlePossibleTypingCommand<TArgs>(TArgs args, Action nextHandler,
}
else if (renameService.ActiveSession.IsInOpenTextBuffer(singleSpan.Start))
{
HandleTypingOutsideEditableSpan(args, operationContext);
CancelRenameSession();
nextHandler();
}
else
Expand All @@ -86,40 +82,9 @@ private void HandlePossibleTypingCommand<TArgs>(TArgs args, Action nextHandler,
}
}

private void HandleTypingOutsideEditableSpan(EditorCommandArgs args, IUIThreadOperationContext operationContext)
=> CommitIfSynchronousOrCancelIfAsynchronous(args, operationContext, placeCaretAtTheEndOfIdentifier: true);

private void CommitIfActive(EditorCommandArgs args, IUIThreadOperationContext operationContext, bool placeCaretAtTheEndOfIdentifier = true)
{
if (renameService.ActiveSession != null)
{
var selection = args.TextView.Selection.VirtualSelectedSpans.First();
Commit(operationContext);
if (placeCaretAtTheEndOfIdentifier)
{
var translatedSelection = selection.TranslateTo(args.TextView.TextBuffer.CurrentSnapshot);
args.TextView.Selection.Select(translatedSelection.Start, translatedSelection.End);
args.TextView.Caret.MoveTo(translatedSelection.End);
}
}
}

/// <summary>
/// Commit() will be called if rename commit is sync. Editor command would be executed after the rename operation complete and it is our legacy behavior.
/// Cancel() will be called if rename is async. It also matches the other editor's behavior (like VS LSP and VSCode).
/// </summary>
private void CommitIfSynchronousOrCancelIfAsynchronous(EditorCommandArgs args, IUIThreadOperationContext operationContext, bool placeCaretAtTheEndOfIdentifier)
{
if (globalOptionService.ShouldCommitAsynchronously())
renameService.ActiveSession?.Cancel();
else
CommitIfActive(args, operationContext, placeCaretAtTheEndOfIdentifier);
}

private void Commit(IUIThreadOperationContext operationContext)
private void CancelRenameSession()
{
RoslynDebug.AssertNotNull(renameService.ActiveSession);
renameService.ActiveSession.Commit(previewChanges: false, operationContext);
renameService.ActiveSession?.Cancel();
}

private bool IsRenameCommitInProgress()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ public CommandState GetCommandState(MoveSelectedLinesUpCommandArgs args)
=> CommandState.Unspecified;

public bool ExecuteCommand(MoveSelectedLinesUpCommandArgs args, CommandExecutionContext context)
=> HandleMoveSelectLinesUpOrDownCommand(args, context);
=> HandleMoveSelectLinesUpOrDownCommand();

public CommandState GetCommandState(MoveSelectedLinesDownCommandArgs args)
=> CommandState.Unspecified;

public bool ExecuteCommand(MoveSelectedLinesDownCommandArgs args, CommandExecutionContext context)
=> HandleMoveSelectLinesUpOrDownCommand(args, context);
=> HandleMoveSelectLinesUpOrDownCommand();

private bool HandleMoveSelectLinesUpOrDownCommand(EditorCommandArgs args, CommandExecutionContext context)
private bool HandleMoveSelectLinesUpOrDownCommand()
{
// When rename commit is in progress, swallow the command so it won't change the workspace
if (IsRenameCommitInProgress())
return true;

CommitIfSynchronousOrCancelIfAsynchronous(args, context.OperationContext, placeCaretAtTheEndOfIdentifier: true);
CancelRenameSession();
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void ExecuteCommand(OpenLineAboveCommandArgs args, Action nextHandler, Co
HandlePossibleTypingCommand(args, nextHandler, context.OperationContext, (activeSession, operationContext, span) =>
{
// Caret would be moved to the new line when editor command is handled, so we don't need to move it.
CommitIfSynchronousOrCancelIfAsynchronous(args, operationContext, placeCaretAtTheEndOfIdentifier: false);
CancelRenameSession();
nextHandler();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void ExecuteCommand(OpenLineBelowCommandArgs args, Action nextHandler, Co
HandlePossibleTypingCommand(args, nextHandler, context.OperationContext, (activeSession, operationContext, span) =>
{
// Caret would be moved to the new line when editor command is handled, so we don't need to move it.
CommitIfSynchronousOrCancelIfAsynchronous(args, operationContext, placeCaretAtTheEndOfIdentifier: false);
CancelRenameSession();
nextHandler();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,32 @@ public CommandState GetCommandState(ReorderParametersCommandArgs args)
=> CommandState.Unspecified;

public bool ExecuteCommand(ReorderParametersCommandArgs args, CommandExecutionContext context)
=> HandleRefactoringCommands(args, context);
=> HandleRefactoringCommands();

public CommandState GetCommandState(RemoveParametersCommandArgs args)
=> CommandState.Unspecified;

public bool ExecuteCommand(RemoveParametersCommandArgs args, CommandExecutionContext context)
=> HandleRefactoringCommands(args, context);
=> HandleRefactoringCommands();

public CommandState GetCommandState(ExtractInterfaceCommandArgs args)
=> CommandState.Unspecified;

public bool ExecuteCommand(ExtractInterfaceCommandArgs args, CommandExecutionContext context)
=> HandleRefactoringCommands(args, context);
=> HandleRefactoringCommands();

public CommandState GetCommandState(EncapsulateFieldCommandArgs args)
=> CommandState.Unspecified;

public bool ExecuteCommand(EncapsulateFieldCommandArgs args, CommandExecutionContext context)
=> HandleRefactoringCommands(args, context);
=> HandleRefactoringCommands();

private bool HandleRefactoringCommands(EditorCommandArgs args, CommandExecutionContext context)
private bool HandleRefactoringCommands()
{
if (IsRenameCommitInProgress())
return true;

CommitIfSynchronousOrCancelIfAsynchronous(args, context.OperationContext, placeCaretAtTheEndOfIdentifier: true);
CancelRenameSession();
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public bool ExecuteCommand(RenameCommandArgs args, CommandExecutionContext conte
}

var token = listener.BeginAsyncOperation(nameof(ExecuteCommand));
_ = ExecuteCommandAsync(args, context.OperationContext).CompletesAsyncOperation(token);
_ = ExecuteCommandAsync(args).CompletesAsyncOperation(token);
return true;
}

private async Task ExecuteCommandAsync(RenameCommandArgs args, IUIThreadOperationContext editorOperationContext)
private async Task ExecuteCommandAsync(RenameCommandArgs args)
{
threadingContext.ThrowIfNotOnUIThread();

Expand Down Expand Up @@ -81,9 +81,8 @@ private async Task ExecuteCommandAsync(RenameCommandArgs args, IUIThreadOperatio
}
else
{
// Otherwise, commit or cancel the existing session and start a new one.
// Set placeCaretAtTheEndOfIdentifier to false because a new rename session will be created based on caret's location.
CommitIfSynchronousOrCancelIfAsynchronous(args, editorOperationContext, placeCaretAtTheEndOfIdentifier: false);
// Otherwise, cancel the existing session and start a new one.
CancelRenameSession();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
// 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.Runtime.Remoting.Contexts;
using Microsoft.CodeAnalysis.ErrorReporting;
using Microsoft.CodeAnalysis.Shared.TestHooks;
using Microsoft.VisualStudio.Commanding;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Editor.Commanding.Commands;
using Microsoft.VisualStudio.TextManager.Interop;
using Microsoft.VisualStudio.Utilities;

namespace Microsoft.CodeAnalysis.Editor.Implementation.InlineRename;
Expand All @@ -27,7 +31,11 @@ public bool ExecuteCommand(ReturnKeyCommandArgs args, CommandExecutionContext co

protected virtual void CommitAndSetFocus(InlineRenameSession activeSession, ITextView textView, IUIThreadOperationContext operationContext)
{
activeSession.InitiateCommit(operationContext);
var token = listener.BeginAsyncOperation(nameof(ExecuteCommand));

// CommitAsync will display UI to the user while this asynchronous work is being done.
activeSession.CommitAsync(previewChanges: false, operationContext)
.ReportNonFatalErrorAsync().CompletesAsyncOperation(token);
SetFocusToTextView(textView);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ public bool ExecuteCommand(SaveCommandArgs args, CommandExecutionContext context
{
// If commit is async, just let editor save the document.
// If call async commit here, it could finish after the save command so the workspace would still be dirty.
if (renameService.ActiveSession != null && !globalOptionService.ShouldCommitAsynchronously())
{
Commit(context.OperationContext);
SetFocusToTextView(args.TextView);
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public CommandState GetCommandState(WordDeleteToEndCommandArgs args)
=> GetCommandState();

public bool ExecuteCommand(WordDeleteToStartCommandArgs args, CommandExecutionContext context)
=> HandleWordDeleteCommand(args, context, args.TextView, deleteToStart: true);
=> HandleWordDeleteCommand(args, args.TextView, deleteToStart: true);

public bool ExecuteCommand(WordDeleteToEndCommandArgs args, CommandExecutionContext context)
=> HandleWordDeleteCommand(args, context, args.TextView, deleteToStart: false);
=> HandleWordDeleteCommand(args, args.TextView, deleteToStart: false);

private bool HandleWordDeleteCommand(EditorCommandArgs args, CommandExecutionContext context, ITextView view, bool deleteToStart)
private bool HandleWordDeleteCommand(EditorCommandArgs args, ITextView view, bool deleteToStart)
{
var subjectBuffer = args.SubjectBuffer;
if (renameService.ActiveSession == null)
Expand Down Expand Up @@ -73,7 +73,7 @@ private bool HandleWordDeleteCommand(EditorCommandArgs args, CommandExecutionCon
}
else
{
CommitIfSynchronousOrCancelIfAsynchronous(args, context.OperationContext, placeCaretAtTheEndOfIdentifier: true);
CancelRenameSession();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ namespace Microsoft.CodeAnalysis.Editor.Implementation.InlineRename;
internal sealed partial class RenameCommandHandler(
IThreadingContext threadingContext,
InlineRenameService renameService,
IGlobalOptionService globalOptionService,
IAsynchronousOperationListenerProvider asynchronousOperationListenerProvider)
: AbstractRenameCommandHandler(threadingContext, renameService, globalOptionService, asynchronousOperationListenerProvider.GetListener(FeatureAttribute.Rename))
: AbstractRenameCommandHandler(threadingContext, renameService, asynchronousOperationListenerProvider.GetListener(FeatureAttribute.Rename))
{
protected override void SetFocusToTextView(ITextView textView)
{
Expand Down
6 changes: 0 additions & 6 deletions src/EditorFeatures/Core/InlineRename/InlineRenameService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,16 @@ namespace Microsoft.CodeAnalysis.Editor.Implementation.InlineRename;
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
internal sealed class InlineRenameService(
IThreadingContext threadingContext,
IUIThreadOperationExecutor uiThreadOperationExecutor,
ITextBufferAssociatedViewService textBufferAssociatedViewService,
ITextBufferFactoryService textBufferFactoryService,
ITextBufferCloneService textBufferCloneService,
IFeatureServiceFactory featureServiceFactory,
IGlobalOptionService globalOptions,
[ImportMany] IEnumerable<IRefactorNotifyService> refactorNotifyServices,
IAsynchronousOperationListenerProvider listenerProvider) : IInlineRenameService
{
private readonly IThreadingContext _threadingContext = threadingContext;
private readonly IUIThreadOperationExecutor _uiThreadOperationExecutor = uiThreadOperationExecutor;
private readonly ITextBufferAssociatedViewService _textBufferAssociatedViewService = textBufferAssociatedViewService;
private readonly IAsynchronousOperationListener _asyncListener = listenerProvider.GetListener(FeatureAttribute.Rename);
private readonly IEnumerable<IRefactorNotifyService> _refactorNotifyServices = refactorNotifyServices;
private readonly ITextBufferFactoryService _textBufferFactoryService = textBufferFactoryService;
private readonly ITextBufferCloneService _textBufferCloneService = textBufferCloneService;
private readonly IFeatureServiceFactory _featureServiceFactory = featureServiceFactory;
Expand Down Expand Up @@ -109,12 +105,10 @@ public async Task<InlineRenameSessionInfo> StartInlineSessionAsync(
renameInfo,
options,
previewChanges,
_uiThreadOperationExecutor,
_textBufferAssociatedViewService,
_textBufferFactoryService,
_textBufferCloneService,
_featureServiceFactory,
_refactorNotifyServices,
_asyncListener);

return new InlineRenameSessionInfo(ActiveSession);
Expand Down
Loading
Loading