Skip to content

Commit

Permalink
Merge pull request #75294 from olegtk/dev/olegtk/DontStompOnUserCaret…
Browse files Browse the repository at this point in the history
…AutoRename

don't mess with user caret in smart rename
  • Loading branch information
Cosifne authored Sep 30, 2024
2 parents 7c66b29 + acec6ee commit 56ce4d1
Showing 1 changed file with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using Microsoft.CodeAnalysis.Editor.Implementation.InlineRename;

Expand All @@ -23,6 +22,7 @@ internal sealed partial class SmartRenameUserInputComboBox : ComboBox, IRenameUs
private readonly SmartRenameViewModel _smartRenameViewModel;
private readonly RenameFlyoutViewModel _baseViewModel;
private readonly Lazy<TextBox> _innerTextBox;
private bool _userChangedTextSelection;

internal SmartRenameUserInputComboBox(RenameFlyoutViewModel viewModel)
{
Expand All @@ -36,7 +36,9 @@ internal SmartRenameUserInputComboBox(RenameFlyoutViewModel viewModel)
_innerTextBox = new Lazy<TextBox>(() =>
{
ApplyTemplate();
return (TextBox)GetTemplateChild(InnerTextBox)!;
var textBox = (TextBox)GetTemplateChild(InnerTextBox)!;
textBox.SelectionChanged += (sender, e) => _userChangedTextSelection = true;
return textBox;
});

_smartRenameViewModel.SuggestedNames.CollectionChanged += SuggestedNames_CollectionChanged;
Expand Down Expand Up @@ -125,6 +127,12 @@ private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs

private void SuggestedNames_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
Focus();
// RenameFlyout handles GotFocus event by calling SelectAllText, which selects all text in the input text box.
// If user changed selection (e.g. by moving the caret or selecting text in the input text box we don't want
// to step on it once rename suggestions became available.
if (!_userChangedTextSelection)
{
Focus();
}
}
}

0 comments on commit 56ce4d1

Please sign in to comment.