Skip to content

Commit

Permalink
Keep selection on rename invocation (dotnet#62654)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryzngard authored and adamperlin committed Jul 19, 2022
1 parent 08c8e4f commit a248db8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public RenameFlyout(RenameFlyoutViewModel viewModel, ITextView textView)
// On load focus the first tab target
Loaded += (s, e) =>
{
Focus();
MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
IdentifierTextBox.Focus();
IdentifierTextBox.Select(_viewModel.StartingSelection.Start, _viewModel.StartingSelection.Length);
};

InitializeComponent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.CodeAnalysis.InlineRename;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Rename;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.PlatformUI.OleComponentSupport;

namespace Microsoft.CodeAnalysis.Editor.Implementation.InlineRename
Expand All @@ -24,11 +25,13 @@ internal class RenameFlyoutViewModel : INotifyPropertyChanged, IDisposable
private bool _isReplacementTextValid = true;
public event PropertyChangedEventHandler? PropertyChanged;

public RenameFlyoutViewModel(InlineRenameSession session)
public RenameFlyoutViewModel(InlineRenameSession session, TextSpan selectionSpan)
{
_session = session;
_session.ReplacementTextChanged += OnReplacementTextChanged;
_session.ReplacementsComputed += OnReplacementsComputed;
StartingSelection = selectionSpan;

ComputeRenameFile();
RegisterOleComponent();
}
Expand Down Expand Up @@ -128,6 +131,8 @@ public bool IsExpanded
public bool IsRenameOverloadsEditable
=> !_session.MustRenameOverloads;

public TextSpan StartingSelection { get; }

public void Submit()
{
_session.Commit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,22 @@ private void UpdateAdornments()
return;
}

// Get the active selection to make sure the rename text is selected in the same way
var originalSpan = _renameService.ActiveSession.TriggerSpan;
var selectionSpan = _textView.Selection.SelectedSpans.First();

var start = selectionSpan.IsEmpty
? 0
: selectionSpan.Start - originalSpan.Start; // The length from the identifier to the start of selection

var length = selectionSpan.IsEmpty
? originalSpan.Length
: selectionSpan.Length;

var identifierSelection = new TextSpan(start, length);

var adornment = new RenameFlyout(
(RenameFlyoutViewModel)s_createdViewModels.GetValue(_renameService.ActiveSession, session => new RenameFlyoutViewModel(session)),
(RenameFlyoutViewModel)s_createdViewModels.GetValue(_renameService.ActiveSession, session => new RenameFlyoutViewModel(session, identifierSelection)),
_textView);

_adornmentLayer.AddAdornment(
Expand Down
10 changes: 7 additions & 3 deletions src/EditorFeatures/Core/InlineRename/InlineRenameSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ internal partial class InlineRenameSession : IInlineRenameSession, IFeatureContr
private readonly IAsynchronousOperationListener _asyncListener;
private readonly Solution _baseSolution;
private readonly Document _triggerDocument;
private readonly SnapshotSpan _triggerSpan;
private readonly ITextView _triggerView;
private readonly IDisposable _inlineRenameSessionDurationLogBlock;
private readonly IThreadingContext _threadingContext;
Expand All @@ -59,6 +58,11 @@ internal partial class InlineRenameSession : IInlineRenameSession, IFeatureContr
private bool _previewChanges;
private readonly Dictionary<ITextBuffer, OpenTextBufferManager> _openTextBuffers = new Dictionary<ITextBuffer, OpenTextBufferManager>();

/// <summary>
/// The original <see cref="SnapshotSpan"/> for the identifier that rename was triggered on
/// </summary>
public SnapshotSpan TriggerSpan { get; }

/// <summary>
/// If non-null, the current text of the replacement. Linked spans added will automatically be updated with this
/// text.
Expand Down Expand Up @@ -131,7 +135,7 @@ public InlineRenameSession(
_threadingContext = threadingContext;
_renameInfo = renameInfo;

_triggerSpan = triggerSpan;
TriggerSpan = triggerSpan;
_triggerDocument = triggerSpan.Snapshot.GetOpenDocumentInCurrentContextWithChanges();
if (_triggerDocument == null)
{
Expand Down Expand Up @@ -757,7 +761,7 @@ private async Task<bool> CommitWorkerAsync(bool previewChanges, CancellationToke
// and applying the desired edits ourselves.
var factory = _workspace.Services.GetRequiredService<IBackgroundWorkIndicatorFactory>();
using var context = factory.Create(
_triggerView, _triggerSpan, EditorFeaturesResources.Computing_Rename_information,
_triggerView, TriggerSpan, EditorFeaturesResources.Computing_Rename_information,
cancelOnEdit: false, cancelOnFocusLost: false);

await CommitCoreAsync(context, previewChanges).ConfigureAwait(false);
Expand Down

0 comments on commit a248db8

Please sign in to comment.