Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use primary constructor in rename command handler #75336

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,14 @@ namespace Microsoft.CodeAnalysis.Editor.Implementation.InlineRename
[Order(Before = PredefinedCommandHandlerNames.ChangeSignature)]
[Order(Before = PredefinedCommandHandlerNames.ExtractInterface)]
[Order(Before = PredefinedCommandHandlerNames.EncapsulateField)]
internal partial class RenameCommandHandler : AbstractRenameCommandHandler
[method: ImportingConstructor]
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
internal partial class RenameCommandHandler(
IThreadingContext threadingContext,
InlineRenameService renameService,
IAsynchronousOperationListenerProvider asynchronousOperationListenerProvider)
: AbstractRenameCommandHandler(threadingContext, renameService, asynchronousOperationListenerProvider.GetListener(FeatureAttribute.Rename))
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public RenameCommandHandler(
IThreadingContext threadingContext,
InlineRenameService renameService,
IAsynchronousOperationListenerProvider asynchronousOperationListenerProvider)
: base(threadingContext, renameService, asynchronousOperationListenerProvider)
{
}

protected override bool AdornmentShouldReceiveKeyboardNavigation(ITextView textView)
=> GetAdornment(textView) switch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,11 @@

namespace Microsoft.CodeAnalysis.Editor.Implementation.InlineRename;

internal abstract partial class AbstractRenameCommandHandler
internal abstract partial class AbstractRenameCommandHandler(
IThreadingContext threadingContext,
InlineRenameService renameService,
IAsynchronousOperationListener listener)
{
private readonly IThreadingContext _threadingContext;
private readonly InlineRenameService _renameService;
private readonly IAsynchronousOperationListener _listener;

protected AbstractRenameCommandHandler(
IThreadingContext threadingContext,
InlineRenameService renameService,
IAsynchronousOperationListenerProvider asynchronousOperationListenerProvider)
{
_threadingContext = threadingContext;
_renameService = renameService;
_listener = asynchronousOperationListenerProvider.GetListener(FeatureAttribute.Rename);
}

public string DisplayName => EditorFeaturesResources.Rename;

protected abstract bool AdornmentShouldReceiveKeyboardNavigation(ITextView textView);
Expand All @@ -46,7 +35,7 @@ protected AbstractRenameCommandHandler(

private CommandState GetCommandState(Func<CommandState> nextHandler)
{
if (_renameService.ActiveSession != null)
if (renameService.ActiveSession != null)
{
return CommandState.Available;
}
Expand All @@ -55,12 +44,12 @@ private CommandState GetCommandState(Func<CommandState> nextHandler)
}

private CommandState GetCommandState()
=> _renameService.ActiveSession != null ? CommandState.Available : CommandState.Unspecified;
=> renameService.ActiveSession != null ? CommandState.Available : CommandState.Unspecified;

private void HandlePossibleTypingCommand<TArgs>(TArgs args, Action nextHandler, IUIThreadOperationContext operationContext, Action<InlineRenameSession, IUIThreadOperationContext, SnapshotSpan> actionIfInsideActiveSpan)
where TArgs : EditorCommandArgs
{
if (_renameService.ActiveSession == null)
if (renameService.ActiveSession == null)
{
nextHandler();
return;
Expand All @@ -77,17 +66,17 @@ private void HandlePossibleTypingCommand<TArgs>(TArgs args, Action nextHandler,
}

var singleSpan = selectedSpans.Single();
if (_renameService.ActiveSession.TryGetContainingEditableSpan(singleSpan.Start, out var containingSpan) &&
if (renameService.ActiveSession.TryGetContainingEditableSpan(singleSpan.Start, out var containingSpan) &&
containingSpan.Contains(singleSpan))
{
actionIfInsideActiveSpan(_renameService.ActiveSession, operationContext, containingSpan);
actionIfInsideActiveSpan(renameService.ActiveSession, operationContext, containingSpan);
}
else if (_renameService.ActiveSession.IsInOpenTextBuffer(singleSpan.Start))
else if (renameService.ActiveSession.IsInOpenTextBuffer(singleSpan.Start))
{
// It's in a read-only area that is open, so let's commit the rename
// and then let the character go through

CommitIfActiveAndCallNextHandler(args, nextHandler, operationContext);
CommitIfActive(args, operationContext);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No real change, just inline a call.

nextHandler();
}
else
{
Expand All @@ -98,7 +87,7 @@ private void HandlePossibleTypingCommand<TArgs>(TArgs args, Action nextHandler,

private void CommitIfActive(EditorCommandArgs args, IUIThreadOperationContext operationContext)
{
if (_renameService.ActiveSession != null)
if (renameService.ActiveSession != null)
{
var selection = args.TextView.Selection.VirtualSelectedSpans.First();

Expand All @@ -110,15 +99,9 @@ private void CommitIfActive(EditorCommandArgs args, IUIThreadOperationContext op
}
}

private void CommitIfActiveAndCallNextHandler(EditorCommandArgs args, Action nextHandler, IUIThreadOperationContext operationContext)
{
CommitIfActive(args, operationContext);
nextHandler();
}

private void Commit(IUIThreadOperationContext operationContext)
{
RoslynDebug.AssertNotNull(_renameService.ActiveSession);
_renameService.ActiveSession.Commit(previewChanges: false, operationContext);
RoslynDebug.AssertNotNull(renameService.ActiveSession);
renameService.ActiveSession.Commit(previewChanges: false, operationContext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public CommandState GetCommandState(EscapeKeyCommandArgs args)

public bool ExecuteCommand(EscapeKeyCommandArgs args, CommandExecutionContext context)
{
if (_renameService.ActiveSession != null)
if (renameService.ActiveSession != null)
{
_renameService.ActiveSession.Cancel();
renameService.ActiveSession.Cancel();
SetFocusToTextView(args.TextView);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ public bool ExecuteCommand(LineEndExtendCommandArgs args, CommandExecutionContex

private bool HandleLineStartOrLineEndCommand(ITextBuffer subjectBuffer, ITextView view, bool lineStart, bool extendSelection)
{
if (_renameService.ActiveSession == null)
if (renameService.ActiveSession == null)
{
return false;
}

var caretPoint = view.GetCaretPoint(subjectBuffer);
if (caretPoint.HasValue)
{
if (_renameService.ActiveSession.TryGetContainingEditableSpan(caretPoint.Value, out var span))
if (renameService.ActiveSession.TryGetContainingEditableSpan(caretPoint.Value, out var span))
{
var newPoint = lineStart ? span.Start : span.End;
if (newPoint == caretPoint.Value && (view.Selection.IsEmpty || extendSelection))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ public bool ExecuteCommand(RenameCommandArgs args, CommandExecutionContext conte
return false;
}

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

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

if (!args.SubjectBuffer.TryGetWorkspace(out var workspace))
{
Expand All @@ -65,11 +65,11 @@ private async Task ExecuteCommandAsync(RenameCommandArgs args, IUIThreadOperatio
}

// If there is already an active session, commit it first
if (_renameService.ActiveSession != null)
if (renameService.ActiveSession != null)
{
// Is the caret within any of the rename fields in this buffer?
// If so, focus the dashboard
if (_renameService.ActiveSession.TryGetContainingEditableSpan(caretPoint.Value, out _))
if (renameService.ActiveSession.TryGetContainingEditableSpan(caretPoint.Value, out _))
{
SetFocusToAdornment(args.TextView);
return;
Expand Down Expand Up @@ -111,7 +111,7 @@ private async Task ExecuteCommandAsync(RenameCommandArgs args, IUIThreadOperatio
return;
}

var sessionInfo = await _renameService.StartInlineSessionAsync(document, selectedSpans.Single().Span.ToTextSpan(), cancellationToken).ConfigureAwait(false);
var sessionInfo = await renameService.StartInlineSessionAsync(document, selectedSpans.Single().Span.ToTextSpan(), cancellationToken).ConfigureAwait(false);
if (!sessionInfo.CanRename)
{
await ShowErrorDialogAsync(workspace, sessionInfo.LocalizedErrorMessage).ConfigureAwait(false);
Expand All @@ -131,7 +131,7 @@ private static bool CanRename(RenameCommandArgs args)

private async Task ShowErrorDialogAsync(Workspace workspace, string message)
{
await _threadingContext.JoinableTaskFactory.SwitchToMainThreadAsync();
await threadingContext.JoinableTaskFactory.SwitchToMainThreadAsync();
var notificationService = workspace.Services.GetService<INotificationService>();
notificationService.SendNotification(message, title: EditorFeaturesResources.Rename, severity: NotificationSeverity.Error);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public CommandState GetCommandState(ReturnKeyCommandArgs args)

public bool ExecuteCommand(ReturnKeyCommandArgs args, CommandExecutionContext context)
{
if (_renameService.ActiveSession != null)
if (renameService.ActiveSession != null)
{
CommitAndSetFocus(_renameService.ActiveSession, args.TextView, context.OperationContext);
CommitAndSetFocus(renameService.ActiveSession, args.TextView, context.OperationContext);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public CommandState GetCommandState(SaveCommandArgs args)

public bool ExecuteCommand(SaveCommandArgs args, CommandExecutionContext context)
{
if (_renameService.ActiveSession != null)
if (renameService.ActiveSession != null)
{
Commit(context.OperationContext);
SetFocusToTextView(args.TextView);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ public bool ExecuteCommand(SelectAllCommandArgs args, CommandExecutionContext co

private bool ExecuteSelectAll(ITextBuffer subjectBuffer, ITextView view)
{
if (_renameService.ActiveSession == null)
if (renameService.ActiveSession == null)
{
return false;
}

var caretPoint = view.GetCaretPoint(subjectBuffer);
if (caretPoint.HasValue)
{
if (_renameService.ActiveSession.TryGetContainingEditableSpan(caretPoint.Value, out var span))
if (renameService.ActiveSession.TryGetContainingEditableSpan(caretPoint.Value, out var span))
{
if (view.Selection.Start.Position != span.Start.Position ||
view.Selection.End.Position != span.End.Position)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public CommandState GetCommandState(RedoCommandArgs args)

public bool ExecuteCommand(UndoCommandArgs args, CommandExecutionContext context)
{
if (_renameService.ActiveSession != null)
if (renameService.ActiveSession != null)
{
for (var i = 0; i < args.Count && _renameService.ActiveSession != null; i++)
for (var i = 0; i < args.Count && renameService.ActiveSession != null; i++)
{
_renameService.ActiveSession.UndoManager.Undo(args.SubjectBuffer);
renameService.ActiveSession.UndoManager.Undo(args.SubjectBuffer);
}

return true;
Expand All @@ -33,11 +33,11 @@ public bool ExecuteCommand(UndoCommandArgs args, CommandExecutionContext context

public bool ExecuteCommand(RedoCommandArgs args, CommandExecutionContext context)
{
if (_renameService.ActiveSession != null)
if (renameService.ActiveSession != null)
{
for (var i = 0; i < args.Count && _renameService.ActiveSession != null; i++)
for (var i = 0; i < args.Count && renameService.ActiveSession != null; i++)
{
_renameService.ActiveSession.UndoManager.Redo(args.SubjectBuffer);
renameService.ActiveSession.UndoManager.Redo(args.SubjectBuffer);
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ public bool ExecuteCommand(WordDeleteToEndCommandArgs args, CommandExecutionCont

private bool HandleWordDeleteCommand(ITextBuffer subjectBuffer, ITextView view, bool deleteToStart)
{
if (_renameService.ActiveSession == null)
if (renameService.ActiveSession == null)
{
return false;
}

var caretPoint = view.GetCaretPoint(subjectBuffer);
if (caretPoint.HasValue)
{
if (_renameService.ActiveSession.TryGetContainingEditableSpan(caretPoint.Value, out var span))
if (renameService.ActiveSession.TryGetContainingEditableSpan(caretPoint.Value, out var span))
{
int start = caretPoint.Value;
int end = caretPoint.Value;
Expand Down
Loading