Skip to content

Commit 33f63da

Browse files
committed
Merge pull request #1121 from SlavaRa/feature/888
Fixes issue #888
2 parents 725dc03 + 39e869c commit 33f63da

File tree

3 files changed

+87
-37
lines changed

3 files changed

+87
-37
lines changed

External/Plugins/CodeRefactor/Commands/Rename.cs

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using ASCompletion.Completion;
66
using ASCompletion.Context;
77
using ASCompletion.Model;
8-
using CodeRefactor.Controls;
98
using CodeRefactor.Provider;
109
using PluginCore;
1110
using PluginCore.Controls;
@@ -23,18 +22,14 @@ namespace CodeRefactor.Commands
2322
/// </summary>
2423
public class Rename : RefactorCommand<IDictionary<String, List<SearchMatch>>>
2524
{
26-
private String newName;
2725
private Boolean outputResults;
2826
private FindAllReferences findAllReferencesCommand;
2927
private Move renamePackage;
30-
3128
private String oldFileName;
3229
private String newFileName;
3330

34-
public String NewName
35-
{
36-
get { return this.newName; }
37-
}
31+
public string TargetName { get; }
32+
public string NewName { get; }
3833

3934
/// <summary>
4035
/// A new Rename refactoring command.
@@ -102,19 +97,21 @@ public Rename(ASResult target, Boolean outputResults, String newName, Boolean ig
10297
string path = Path.Combine(aPath.Path, package);
10398
if (aPath.IsValid && Directory.Exists(path))
10499
{
105-
this.newName = string.IsNullOrEmpty(newName) ? GetNewName(Path.GetFileName(path)) : newName;
106-
if (string.IsNullOrEmpty(this.newName)) return;
107-
renamePackage = new Move(new Dictionary<string, string> { { path, this.newName } }, true, true);
100+
TargetName = Path.GetFileName(path);
101+
this.NewName = string.IsNullOrEmpty(newName) ? GetNewName(TargetName) : newName;
102+
if (string.IsNullOrEmpty(this.NewName)) return;
103+
renamePackage = new Move(new Dictionary<string, string> { { path, this.NewName } }, true, true);
108104
return;
109105
}
110106
}
111107
}
112108
return;
113109
}
114110

115-
this.newName = !string.IsNullOrEmpty(newName) ? newName : GetNewName(RefactoringHelper.GetRefactorTargetName(target));
111+
TargetName = RefactoringHelper.GetRefactorTargetName(target);
112+
this.NewName = !string.IsNullOrEmpty(newName) ? newName : GetNewName(TargetName);
116113

117-
if (string.IsNullOrEmpty(this.newName)) return;
114+
if (string.IsNullOrEmpty(this.NewName)) return;
118115

119116
// create a FindAllReferences refactor to get all the changes we need to make
120117
// we'll also let it output the results, at least until we implement a way of outputting the renamed results later
@@ -133,7 +130,7 @@ protected override void ExecutionImplementation()
133130
if (renamePackage != null)
134131
{
135132
renamePackage.RegisterDocumentHelper(AssociatedDocumentHelper);
136-
renamePackage.OnRefactorComplete += (o, args) => FireOnRefactorComplete();
133+
renamePackage.OnRefactorComplete += OnRenamePackageComplete;
137134
renamePackage.Execute();
138135
}
139136
else
@@ -160,13 +157,19 @@ protected override void ExecutionImplementation()
160157
/// </summary>
161158
public override Boolean IsValid()
162159
{
163-
return renamePackage != null ? renamePackage.IsValid() : !string.IsNullOrEmpty(this.newName);
160+
return renamePackage != null ? renamePackage.IsValid() : !string.IsNullOrEmpty(this.NewName);
164161
}
165162

166163
#endregion
167164

168165
#region Private Helper Methods
169166

167+
void OnRenamePackageComplete(object sender, RefactorCompleteEventArgs<IDictionary<string, List<SearchMatch>>> args)
168+
{
169+
Results = args.Results;
170+
FireOnRefactorComplete();
171+
}
172+
170173
private bool ValidateTargets()
171174
{
172175
ASResult target = findAllReferencesCommand.CurrentTarget;
@@ -192,19 +195,8 @@ private bool ValidateTargets()
192195
if (!isEnum && !isClass && !isGlobalFunction && !isGlobalNamespace)
193196
return true;
194197

195-
FileModel inFile;
196-
String originName;
197-
198-
if (isEnum || isClass)
199-
{
200-
inFile = target.Type.InFile;
201-
originName = target.Type.Name;
202-
}
203-
else
204-
{
205-
inFile = target.Member.InFile;
206-
originName = target.Member.Name;
207-
}
198+
var member = isEnum || isClass ? target.Type : target.Member;
199+
FileModel inFile = member.InFile;
208200

209201
// Is this possible? should return false? I'm inclined to think so
210202
if (inFile == null) return true;
@@ -213,7 +205,7 @@ private bool ValidateTargets()
213205
String oldName = Path.GetFileNameWithoutExtension(oldFileName);
214206

215207
// Private classes and similars
216-
if (string.IsNullOrEmpty(oldName) || !oldName.Equals(originName))
208+
if (string.IsNullOrEmpty(oldName) || !oldName.Equals(member.Name))
217209
return true;
218210

219211
String fullPath = Path.GetFullPath(inFile.FileName);
@@ -243,7 +235,7 @@ private void OnFindAllReferencesCompleted(Object sender, RefactorCompleteEventAr
243235
var doc = AssociatedDocumentHelper.LoadDocument(entry.Key);
244236
var sci = doc.SciControl;
245237
// replace matches in the current file with the new name
246-
RefactoringHelper.ReplaceMatches(entry.Value, sci, this.newName);
238+
RefactoringHelper.ReplaceMatches(entry.Value, sci, this.NewName);
247239
//Uncomment if we want to keep modified files
248240
//if (sci.IsModify) AssociatedDocumentHelper.MarkDocumentToKeep(entry.Key);
249241
doc.Save();
@@ -290,7 +282,6 @@ private void RenameFile(IDictionary<string, List<SearchMatch>> results)
290282
project.SetDocumentClass(newFileName, true);
291283
project.Save();
292284
}
293-
294285
}
295286

296287
if (results.ContainsKey(oldFileName))
@@ -303,7 +294,7 @@ private void RenameFile(IDictionary<string, List<SearchMatch>> results)
303294
}
304295

305296
/// <summary>
306-
///
297+
/// Outputs the results to the TraceManager
307298
/// </summary>
308299
private void ReportResults()
309300
{

External/Plugins/CodeRefactor/Provider/RefactoringHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public static ASResult GetDefaultRefactorTarget()
9999
ScintillaControl sci = PluginBase.MainForm.CurrentDocument.SciControl;
100100
if (!ASContext.Context.IsFileValid || (sci == null)) return null;
101101
int position = sci.WordEndPosition(sci.CurrentPos, true);
102-
return DeclarationLookupResult(sci, position);
102+
return ASComplete.GetExpressionType(sci, position);
103103
}
104104

105105
public static string GetRefactorTargetName(ASResult target)

External/Plugins/CodeRefactor/Provider/RenamingHelper.cs

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using ASCompletion.Completion;
88
using ASCompletion.Context;
99
using ASCompletion.Model;
10+
using PluginCore;
1011
using PluginCore.Localization;
1112
using PluginCore.Managers;
1213
using ProjectManager.Helpers;
@@ -17,14 +18,20 @@ class RenamingHelper
1718
{
1819
static readonly List<Rename> queue = new List<Rename>();
1920
static Rename currentCommand;
21+
static StartState startState;
2022

2123
public static void AddToQueue(ASResult target)
2224
{
2325
AddToQueue(target, true);
2426
}
2527
public static void AddToQueue(ASResult target, bool outputResults)
2628
{
27-
if (target.IsPackage) queue.Add(new Rename(target, outputResults));
29+
Rename cmd = null;
30+
if (target.IsPackage)
31+
{
32+
cmd = new Rename(target, outputResults);
33+
queue.Add(cmd);
34+
}
2835
else
2936
{
3037
string originalName = RefactoringHelper.GetRefactorTargetName(target);
@@ -34,7 +41,8 @@ public static void AddToQueue(ASResult target, bool outputResults)
3441
if (askName.ShowDialog() != DialogResult.OK) return;
3542
string newName = askName.Line.Trim();
3643
if (newName.Length == 0 || newName == originalName) return;
37-
queue.Add(new Rename(target, outputResults, newName));
44+
cmd = new Rename(target, outputResults, newName);
45+
queue.Add(cmd);
3846
if (ASContext.Context.CurrentModel.haXe && target.Member != null &&
3947
(target.Member.Flags & (FlagType.Getter | FlagType.Setter)) > 0)
4048
{
@@ -43,7 +51,18 @@ public static void AddToQueue(ASResult target, bool outputResults)
4351
if (list[1].Name == "set") RenameMember(target.InClass, "set_" + originalName, "set_" + newName, outputResults);
4452
}
4553
}
46-
if (currentCommand == null) ExecuteFirst();
54+
if (currentCommand != null) return;
55+
if (cmd != null)
56+
{
57+
var doc = PluginBase.MainForm.CurrentDocument;
58+
startState = new StartState
59+
{
60+
FileName = doc.FileName,
61+
CursorPosition = doc.SciControl.CurrentPos,
62+
Cmd = cmd
63+
};
64+
}
65+
ExecuteFirst();
4766
}
4867

4968
static void RenameMember(ClassModel inClass, string name, string newName, bool outputResults)
@@ -55,7 +74,7 @@ static void RenameMember(ClassModel inClass, string name, string newName, bool o
5574
if (result.Member == null) return;
5675
queue.Add(new Rename(result, outputResults, newName));
5776
}
58-
77+
5978
static void ExecuteFirst()
6079
{
6180
try
@@ -76,7 +95,47 @@ static void ExecuteFirst()
7695
static void OnRefactorComplete(object sender, RefactorCompleteEventArgs<IDictionary<string, List<SearchMatch>>> e)
7796
{
7897
if (queue.Count > 0) ExecuteFirst();
79-
else currentCommand = null;
98+
else
99+
{
100+
if (startState != null) RestoreStartState();
101+
currentCommand = null;
102+
startState = null;
103+
}
80104
}
105+
106+
static void RestoreStartState()
107+
{
108+
var fileName = startState.FileName;
109+
var cursorPosition = startState.CursorPosition;
110+
var cmd = startState.Cmd;
111+
var charsDiff = cmd.NewName.Length - cmd.TargetName.Length;
112+
foreach (var entry in cmd.Results)
113+
{
114+
if (entry.Key != fileName) continue;
115+
SearchMatch match = null;
116+
foreach (var tmpMatch in entry.Value)
117+
{
118+
var start = tmpMatch.Index - charsDiff;
119+
if (cursorPosition >= start)
120+
{
121+
charsDiff += charsDiff;
122+
match = tmpMatch;
123+
}
124+
else break;
125+
}
126+
var doc = (ITabbedDocument) PluginBase.MainForm.OpenEditableDocument(fileName);
127+
var sci = doc.SciControl;
128+
var pos = sci.PositionFromLine(match.Line - 1) + match.Column;
129+
sci.SetSel(pos, pos);
130+
break;
131+
}
132+
}
133+
}
134+
135+
class StartState
136+
{
137+
public string FileName;
138+
public int CursorPosition;
139+
public Rename Cmd;
81140
}
82141
}

0 commit comments

Comments
 (0)