Skip to content

Commit

Permalink
Stop renaming references to Me
Browse files Browse the repository at this point in the history
Class module references to Me should not be renamed to not break the
code.
Fixes #2710
  • Loading branch information
Vogel612 committed Nov 25, 2017
1 parent 2c7c83e commit 002efc8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
4 changes: 3 additions & 1 deletion RetailCoder.VBE/Refactorings/Rename/RenameRefactoring.cs
Expand Up @@ -476,7 +476,9 @@ private void RenameStandardElements(Declaration target, string newName)

private void RenameReferences(Declaration target, string newName)
{
var modules = target.References.GroupBy(r => r.QualifiedModuleName);
var modules = target.References
.Where(reference => reference.Context.GetText() != "Me")
.GroupBy(r => r.QualifiedModuleName);
foreach (var grouping in modules)
{
_modulesToRewrite.Add(grouping.Key);
Expand Down
42 changes: 42 additions & 0 deletions RubberduckTests/Refactoring/RenameTests.cs
Expand Up @@ -1962,6 +1962,48 @@ public void RenameRefactoring_RenameViewModel_IsValidName_ChangeCasingNotValid()
Assert.IsFalse(renameViewModel.IsValidName);
}


[TestMethod]
[TestCategory("Refactorings")]
[TestCategory("Rename")]
public void RenameRefactoring_RenameClassModule_DoesNotChangeMeReferences()
{
const string newName = "RenamedClassModule";

//Input
const string inputCode =
@"Property Get Self() As IClassModule
Set Self = Me
End Property";

var selection = new Selection(3, 27, 3, 27);

IVBComponent component;
var vbe = MockVbeBuilder.BuildFromSingleModule(inputCode, "ClassModule1", ComponentType.ClassModule, out component, selection);
using (var state = MockParser.CreateAndParse(vbe.Object))
{

var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(component), selection);

var msgbox = new Mock<IMessageBox>();
msgbox.Setup(m => m.Show(It.IsAny<string>(), It.IsAny<string>(), MessageBoxButtons.YesNo, It.IsAny<MessageBoxIcon>()))
.Returns(DialogResult.Yes);

var vbeWrapper = vbe.Object;
var model = new RenameModel(vbeWrapper, state, qualifiedSelection) { NewName = newName };
model.Target = model.Declarations.FirstOrDefault(i => i.DeclarationType == DeclarationType.ClassModule && i.IdentifierName == "ClassModule1");

//SetupFactory
var factory = SetupFactory(model);

var refactoring = new RenameRefactoring(vbeWrapper, factory.Object, msgbox.Object, state);
refactoring.Refactor(model.Target);

Assert.AreSame(newName, component.CodeModule.Name);
Assert.AreEqual(inputCode, component.CodeModule.GetLines(0, component.CodeModule.CountOfLines));
}

}
#endregion

#region Test Execution
Expand Down

0 comments on commit 002efc8

Please sign in to comment.