Skip to content

Commit

Permalink
Fix new rename showing overloads when not applicable (#62559)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryzngard authored Jul 18, 2022
1 parent d2ff1d8 commit fdedcf5
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@

<StackPanel Orientation="Vertical" Visibility="{Binding IsExpanded, Converter={StaticResource BooleanToVisibilityConverter}}">
<CheckBox Content="{Binding ElementName=control, Path=RenameOverloads}" Margin="0,5,0,0" IsChecked="{Binding Path=RenameOverloadFlag, Mode=TwoWay}"
Name="OverloadsCheckbox" Visibility="{Binding RenameOverloadsVisibility}" IsEnabled="{Binding IsRenameOverloadsEditable}" />
Name="OverloadsCheckbox" Visibility="{Binding IsRenameOverloadsVisible, Converter={StaticResource BooleanToVisibilityConverter}, Mode=OneWay}" IsEnabled="{Binding IsRenameOverloadsEditable}" />
<CheckBox Name="CommentsCheckbox" Content="{Binding ElementName=control, Path=SearchInComments}" Margin="0,5,0,0" IsChecked="{Binding Path=RenameInCommentsFlag, Mode=TwoWay}" />
<CheckBox Name="StringsCheckbox" Content="{Binding ElementName=control, Path=SearchInStrings}" Margin="0,5,0,0" IsChecked="{Binding Path=RenameInStringsFlag, Mode=TwoWay}" />
<CheckBox Name="FileRenameCheckbox"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ namespace Microsoft.CodeAnalysis.Editor.Implementation.InlineRename
internal class RenameFlyoutViewModel : INotifyPropertyChanged, IDisposable
{
private readonly InlineRenameSession _session;
private readonly bool _registerOleComponent;
private OleComponent? _oleComponent;
private bool _disposedValue;
private bool _isReplacementTextValid = true;
public event PropertyChangedEventHandler? PropertyChanged;

public RenameFlyoutViewModel(InlineRenameSession session, TextSpan selectionSpan)
public RenameFlyoutViewModel(InlineRenameSession session, TextSpan selectionSpan, bool registerOleComponent)
{
_session = session;
_registerOleComponent = registerOleComponent;
_session.ReplacementTextChanged += OnReplacementTextChanged;
_session.ReplacementsComputed += OnReplacementsComputed;
StartingSelection = selectionSpan;
Expand All @@ -49,6 +51,8 @@ public string IdentifierText
}
}

public InlineRenameSession Session => _session;

public bool AllowFileRename => _session.FileRenameInfo == InlineRenameFileRenameInfo.Allowed && _isReplacementTextValid;
public bool ShowFileRename => _session.FileRenameInfo != InlineRenameFileRenameInfo.NotAllowed;

Expand Down Expand Up @@ -131,6 +135,9 @@ public bool IsExpanded
public bool IsRenameOverloadsEditable
=> !_session.MustRenameOverloads;

public bool IsRenameOverloadsVisible
=> _session.HasRenameOverloads;

public TextSpan StartingSelection { get; }

public void Submit()
Expand All @@ -157,6 +164,13 @@ public void Dispose()
/// </summary>
public void RegisterOleComponent()
{
// In unit testing we won't have an OleComponentManager available, so
// calls to OleComponent.CreateHostedComponent will throw
if (!_registerOleComponent)
{
return;
}

Debug.Assert(_oleComponent is null);

_oleComponent = OleComponent.CreateHostedComponent("Microsoft CodeAnalysis Inline Rename");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private void UpdateAdornments()
var identifierSelection = new TextSpan(start, length);

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

_adornmentLayer.AddAdornment(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ Imports Microsoft.CodeAnalysis.Rename

Namespace Microsoft.CodeAnalysis.Editor.UnitTests.Rename
<[UseExportProvider]>
Public Class DashboardTests
Public Class RenameViewModelTests
<WpfTheory>
<CombinatorialData, Trait(Traits.Feature, Traits.Features.Rename)>
Public Async Function RenameWithNoOverload(host As RenameTestHost) As Task
Await VerifyDashboard(
Await VerifyViewModels(
(<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
Expand All @@ -41,7 +41,7 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.Rename
<WpfTheory>
<CombinatorialData, Trait(Traits.Feature, Traits.Features.Rename)>
Public Async Function RenameWithOverload(host As RenameTestHost) As Task
Await VerifyDashboard(
Await VerifyViewModels(
(<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
Expand Down Expand Up @@ -73,7 +73,7 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.Rename
<WorkItem(883263, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/883263")>
<CombinatorialData, Trait(Traits.Feature, Traits.Features.Rename)>
Public Async Function RenameWithInvalidOverload(host As RenameTestHost) As Task
Await VerifyDashboard(
Await VerifyViewModels(
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
Expand Down Expand Up @@ -103,7 +103,7 @@ class Program
<CombinatorialData, Trait(Traits.Feature, Traits.Features.Rename)>
<WorkItem(853839, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/853839")>
Public Async Function RenameAttributeAlias(host As RenameTestHost) As Task
Await VerifyDashboard(
Await VerifyViewModels(
(<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
Expand All @@ -123,7 +123,7 @@ class AttributeAttribute : System.Attribute { }
<CombinatorialData, Trait(Traits.Feature, Traits.Features.Rename)>
<WorkItem(700923, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/700923"), WorkItem(700925, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/700925")>
Public Async Function RenameWithOverloadAndInStringsAndComments(host As RenameTestHost) As Task
Await VerifyDashboard(
Await VerifyViewModels(
(<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
Expand Down Expand Up @@ -160,7 +160,7 @@ class AttributeAttribute : System.Attribute { }
<CombinatorialData, Trait(Traits.Feature, Traits.Features.Rename)>
<WorkItem(700923, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/700923"), WorkItem(700925, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/700925")>
Public Async Function RenameInComments(host As RenameTestHost) As Task
Await VerifyDashboard(
Await VerifyViewModels(
(<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
Expand Down Expand Up @@ -198,7 +198,7 @@ class $$Program
<CombinatorialData, Trait(Traits.Feature, Traits.Features.Rename)>
<WorkItem(700923, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/700923"), WorkItem(700925, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/700925")>
Public Async Function RenameInStrings(host As RenameTestHost) As Task
Await VerifyDashboard(
Await VerifyViewModels(
(<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
Expand Down Expand Up @@ -236,7 +236,7 @@ class $$Program
<CombinatorialData, Trait(Traits.Feature, Traits.Features.Rename)>
<WorkItem(700923, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/700923"), WorkItem(700925, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/700925")>
Public Async Function RenameInCommentsAndStrings(host As RenameTestHost) As Task
Await VerifyDashboard(
Await VerifyViewModels(
(<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
Expand Down Expand Up @@ -274,7 +274,7 @@ class $$Program
<WpfTheory>
<CombinatorialData, Trait(Traits.Feature, Traits.Features.Rename)>
Public Async Function NonConflictingEditWithMultipleLocations(host As RenameTestHost) As Task
Await VerifyDashboard(
Await VerifyViewModels(
(<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
Expand All @@ -295,7 +295,7 @@ class $$Program
<WpfTheory>
<CombinatorialData, Trait(Traits.Feature, Traits.Features.Rename)>
Public Async Function NonConflictingEditWithSingleLocation(host As RenameTestHost) As Task
Await VerifyDashboard(
Await VerifyViewModels(
(<Workspace>
<Project Language="C#" CommonReferences="true">
<Document>
Expand All @@ -316,7 +316,7 @@ class $$Program
<WpfTheory>
<CombinatorialData, Trait(Traits.Feature, Traits.Features.Rename)>
Public Async Function ParameterConflictingWithInstanceField(host As RenameTestHost) As Task
Await VerifyDashboard(
Await VerifyViewModels(
(<Workspace>
<Project Language="C#">
<Document>
Expand All @@ -341,7 +341,7 @@ class $$Program
<WpfTheory>
<CombinatorialData, Trait(Traits.Feature, Traits.Features.Rename)>
Public Async Function ParameterConflictingWithInstanceFieldMoreThanOnce(host As RenameTestHost) As Task
Await VerifyDashboard(
Await VerifyViewModels(
(<Workspace>
<Project Language="C#">
<Document>
Expand All @@ -365,7 +365,7 @@ class $$Program
<WpfTheory>
<CombinatorialData, Trait(Traits.Feature, Traits.Features.Rename)>
Public Async Function ParameterConflictingWithLocal_Unresolvable(host As RenameTestHost) As Task
Await VerifyDashboard(
Await VerifyViewModels(
(<Workspace>
<Project Language="C#">
<Document>
Expand All @@ -388,7 +388,7 @@ class $$Program
<WpfTheory>
<CombinatorialData, Trait(Traits.Feature, Traits.Features.Rename)>
Public Async Function MoreThanOneUnresolvableConflicts(host As RenameTestHost) As Task
Await VerifyDashboard(
Await VerifyViewModels(
(<Workspace>
<Project Language="C#">
<Document>
Expand All @@ -413,7 +413,7 @@ class $$Program
<WpfTheory>
<CombinatorialData, Trait(Traits.Feature, Traits.Features.Rename)>
Public Async Function ConflictsAcrossLanguages_Resolvable(host As RenameTestHost) As Task
Await VerifyDashboard(
Await VerifyViewModels(
(<Workspace>
<Project Language="C#" AssemblyName="CSharpAssembly" CommonReferences="true">
<Document>
Expand Down Expand Up @@ -450,7 +450,7 @@ class $$Program
<WpfTheory>
<CombinatorialData, Trait(Traits.Feature, Traits.Features.Rename)>
Public Async Function RenameWithNameof_FromDefinition_DoesNotForceRenameOverloadsOption(host As RenameTestHost) As Task
Await VerifyDashboard(
Await VerifyViewModels(
(<Workspace>
<Project Language="C#" AssemblyName="CSharpAssembly" CommonReferences="true">
<Document>
Expand All @@ -474,7 +474,7 @@ class C
<WpfTheory>
<CombinatorialData, Trait(Traits.Feature, Traits.Features.Rename)>
Public Async Function RenameWithNameof_FromReference_DoesForceRenameOverloadsOption(host As RenameTestHost) As Task
Await VerifyDashboard(
Await VerifyViewModels(
(<Workspace>
<Project Language="C#" AssemblyName="CSharpAssembly" CommonReferences="true">
<Document>
Expand All @@ -498,7 +498,7 @@ class C
<WpfTheory>
<CombinatorialData, Trait(Traits.Feature, Traits.Features.Rename)>
Public Async Function RenameWithNameof_FromDefinition_WithRenameOverloads_Cascading(host As RenameTestHost) As Task
Await VerifyDashboard(
Await VerifyViewModels(
(<Workspace>
<Project Language="C#" AssemblyName="CSharpAssembly" CommonReferences="true">
<Document>
Expand Down Expand Up @@ -530,7 +530,7 @@ class D : B
hasRenameOverload:=True)
End Function

Friend Shared Async Function VerifyDashboard(
Friend Shared Async Function VerifyViewModels(
test As XElement,
newName As String,
searchResultText As String,
Expand Down Expand Up @@ -613,14 +613,30 @@ class D : B
Assert.Equal(severity, model.Severity)
End Using

Using flyout = New RenameFlyout(
New RenameFlyoutViewModel(DirectCast(sessionInfo.Session, InlineRenameSession), selectionSpan:=Nothing, registerOleComponent:=False), ' Don't registerOleComponent in tests, it requires OleComponentManagers that don't exist in our host
textView:=cursorDocument.GetTextView())

Await WaitForRename(workspace)

Dim model = DirectCast(flyout.DataContext, RenameFlyoutViewModel)

Assert.Equal(hasRenameOverload, model.Session.HasRenameOverloads)
Assert.Equal(hasRenameOverload, model.IsRenameOverloadsVisible)
Assert.Equal(isRenameOverloadsEditable, model.IsRenameOverloadsEditable)
If Not isRenameOverloadsEditable Then
Assert.True(model.RenameOverloadsFlag)
End If
End Using

sessionInfo.Session.Cancel()
End Using
End Function

<WpfTheory>
<CombinatorialData, Trait(Traits.Feature, Traits.Features.Rename)>
Public Async Function RenameWithReferenceInUnchangeableDocument(host As RenameTestHost) As Task
Await VerifyDashboard(
Await VerifyViewModels(
(<Workspace>
<Project Language="C#">
<Document>
Expand Down

0 comments on commit fdedcf5

Please sign in to comment.