Skip to content

Commit

Permalink
Add renaming overridden methods automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
puff committed May 22, 2023
1 parent 7f8de9d commit 9cff432
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ dnSpy.Extension.EasyRename
A simple [dnSpy](https://github.com/dnSpyEx/dnSpy) extension for easily renaming members.

## Features
- [ ] Rename overridden methods automatically.
- [x] Rename overridden methods automatically.
- [x] Rename type when renaming a constructor method.

### Warnings
Expand Down
34 changes: 34 additions & 0 deletions dnSpy.Extension.EasyRename/Commands/RenameMemberCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.ComponentModel.Composition;
using System.Linq;
using dnlib.DotNet;
using dnSpy.Contracts.App;
using dnSpy.Contracts.Documents.Tabs;
Expand Down Expand Up @@ -48,13 +49,46 @@ public override void Execute(IMenuItemContext context)
if (isConstructor)
member.DeclaringType.Name = newName;
else
{
if (member is MethodDef method && (method.IsAbstract || method.IsVirtual))
{
foreach(var t in method.Module.GetTypes().Where(x => HasBaseType(x, method.DeclaringType, false)))
{
var @override = t.FindMethod(method.Name, method.MethodSig);
if (@override is { IsVirtual: true })
@override.Name = newName;
}
}

member.Name = newName;
}

var moduleDocNode = _documentTabService.DocumentTreeView.FindNode(member.Module)!;
_documentTabService.DocumentTreeView.TreeView.RefreshAllNodes();
_documentTabService.RefreshModifiedDocument(moduleDocNode.Document);
}

/// <summary>
/// Checks whether a type implements a specific base type.
/// </summary>
/// <param name="type">The type to check on.</param>
/// <param name="baseType">The base type to check for.</param>
/// <param name="implicit">Whether to check the type itself against the base type as well.</param>
/// <returns>Whether the type implements a specific base type.</returns>
private static bool HasBaseType(ITypeDefOrRef type, ITypeDefOrRef baseType, bool @implicit = true)
{
var bt = @implicit ? type : type.GetBaseType();
while (bt is not null)
{
if (bt.Equals(baseType))
return true;

bt = bt.GetBaseType();
}

return false;
}

private static IMemberDef? GetMemberDef(IMenuItemContext context)
{
if (context.CreatorObject.Guid == new Guid(MenuConstants.GUIDOBJ_DOCUMENTVIEWERCONTROL_GUID))
Expand Down

0 comments on commit 9cff432

Please sign in to comment.