Skip to content

Commit 96e142f

Browse files
author
SlavaRa
committed
closes #237
1 parent f5ffe6a commit 96e142f

File tree

5 files changed

+90
-11
lines changed

5 files changed

+90
-11
lines changed

External/Plugins/CodeRefactor/CodeRefactor.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
<Compile Include="Provider\DocumentHelper.cs" />
115115
<Compile Include="Provider\MovingHelper.cs" />
116116
<Compile Include="Provider\RefactoringHelper.cs" />
117+
<Compile Include="Provider\RenamingHelper.cs" />
117118
<Compile Include="Provider\UserInterfaceManager.cs" />
118119
<Compile Include="RefactorItem.cs" />
119120
<Compile Include="Settings.cs" />

External/Plugins/CodeRefactor/Commands/Rename.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,8 @@ public Rename(ASResult target, Boolean outputResults, String newName, Boolean ig
111111
}
112112
return;
113113
}
114-
Boolean isEnum = target.Type.IsEnum();
115-
Boolean isVoid = target.Type.IsVoid();
116-
Boolean isClass = !isVoid && target.IsStatic && (target.Member == null || RefactoringHelper.CheckFlag(target.Member.Flags, FlagType.Constructor));
117114

118-
if (!string.IsNullOrEmpty(newName))
119-
this.newName = newName;
120-
else if (isEnum || isClass)
121-
this.newName = GetNewName(target.Type.Name);
122-
else
123-
this.newName = GetNewName(target.Member.Name);
115+
this.newName = !string.IsNullOrEmpty(newName) ? newName : GetNewName(RefactoringHelper.GetRefactorTargetName(target));
124116

125117
if (string.IsNullOrEmpty(this.newName)) return;
126118

External/Plugins/CodeRefactor/PluginMain.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,8 +423,7 @@ private void RenameClicked(Object sender, EventArgs e)
423423
{
424424
try
425425
{
426-
Rename command = new Rename(true);
427-
command.Execute();
426+
RenamingHelper.AddToQueue(RefactoringHelper.GetDefaultRefactorTarget());
428427
}
429428
catch (Exception ex)
430429
{

External/Plugins/CodeRefactor/Provider/RefactoringHelper.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ public static ASResult GetDefaultRefactorTarget()
102102
return DeclarationLookupResult(sci, position);
103103
}
104104

105+
public static string GetRefactorTargetName(ASResult target)
106+
{
107+
ClassModel type = target.Type;
108+
MemberModel member = target.Member;
109+
if (type.IsEnum() || !type.IsVoid() && target.IsStatic && (member == null || (member.Flags & FlagType.Constructor) > 0))
110+
return type.Name;
111+
return member.Name;
112+
}
113+
105114
/// <summary>
106115
/// Retrieves the refactoring target based on the file.
107116
/// </summary>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using CodeRefactor.Commands;
2+
using PluginCore.FRService;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Windows.Forms;
7+
using ASCompletion.Completion;
8+
using ASCompletion.Context;
9+
using ASCompletion.Model;
10+
using PluginCore.Localization;
11+
using PluginCore.Managers;
12+
using ProjectManager.Helpers;
13+
14+
namespace CodeRefactor.Provider
15+
{
16+
class RenamingHelper
17+
{
18+
static readonly List<Rename> queue = new List<Rename>();
19+
static Rename currentCommand;
20+
21+
public static void AddToQueue(ASResult target)
22+
{
23+
AddToQueue(target, true);
24+
}
25+
public static void AddToQueue(ASResult target, bool outputResults)
26+
{
27+
string name = RefactoringHelper.GetRefactorTargetName(target);
28+
string label = TextHelper.GetString("Label.NewName");
29+
string title = string.Format(TextHelper.GetString("Title.RenameDialog"), name);
30+
LineEntryDialog askName = new LineEntryDialog(title, label, name);
31+
if (askName.ShowDialog() == DialogResult.OK && askName.Line.Trim().Length > 0 && askName.Line.Trim() != name)
32+
{
33+
string newName = askName.Line.Trim();
34+
queue.Add(new Rename(target, outputResults, newName));
35+
if (ASContext.Context.CurrentModel.haXe && target.Member != null &&
36+
(target.Member.Flags & (FlagType.Getter | FlagType.Setter)) > 0)
37+
{
38+
List<MemberModel> list = target.Member.Parameters;
39+
if (list[0].Name == "get") RenameMember(target.InClass, $"get_{name}", $"get_{newName}", outputResults);
40+
if (list[1].Name == "set") RenameMember(target.InClass, $"set_{name}", $"set_{newName}", outputResults);
41+
}
42+
if (currentCommand == null) ExecuteFirst();
43+
}
44+
}
45+
46+
static void RenameMember(ClassModel inClass, string name, string newName, bool outputResults)
47+
{
48+
MemberModel m = inClass.Members.Cast<MemberModel>().FirstOrDefault(it => it.Name == name);
49+
if (m == null) return;
50+
ASResult result = new ASResult();
51+
ASComplete.FindMember(name, inClass, result, FlagType.Dynamic | FlagType.Function, 0);
52+
queue.Add(new Rename(result, outputResults, newName));
53+
}
54+
55+
static void ExecuteFirst()
56+
{
57+
try
58+
{
59+
currentCommand = queue[0];
60+
queue.Remove(currentCommand);
61+
currentCommand.OnRefactorComplete += OnRefactorComplete;
62+
currentCommand.Execute();
63+
}
64+
catch (Exception ex)
65+
{
66+
queue.Clear();
67+
currentCommand = null;
68+
ErrorManager.ShowError(ex);
69+
}
70+
}
71+
72+
static void OnRefactorComplete(object sender, RefactorCompleteEventArgs<IDictionary<string, List<SearchMatch>>> e)
73+
{
74+
if (queue.Count > 0) ExecuteFirst();
75+
else currentCommand = null;
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)