Skip to content

Commit cc84a8f

Browse files
author
SlavaRa
committed
Fixes issue #888
1 parent db235bf commit cc84a8f

File tree

3 files changed

+78
-45
lines changed

3 files changed

+78
-45
lines changed

External/Plugins/CodeRefactor/Commands/Rename.cs

Lines changed: 14 additions & 30 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,20 @@ 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+
this.NewName = string.IsNullOrEmpty(newName) ? GetNewName(Path.GetFileName(path)) : newName;
101+
if (string.IsNullOrEmpty(this.NewName)) return;
102+
renamePackage = new Move(new Dictionary<string, string> { { path, this.NewName } }, true, true);
108103
return;
109104
}
110105
}
111106
}
112107
return;
113108
}
114109

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

117-
if (string.IsNullOrEmpty(this.newName)) return;
113+
if (string.IsNullOrEmpty(this.NewName)) return;
118114

119115
// create a FindAllReferences refactor to get all the changes we need to make
120116
// we'll also let it output the results, at least until we implement a way of outputting the renamed results later
@@ -159,7 +155,7 @@ protected override void ExecutionImplementation()
159155
/// </summary>
160156
public override Boolean IsValid()
161157
{
162-
return renamePackage != null ? renamePackage.IsValid() : !string.IsNullOrEmpty(this.newName);
158+
return renamePackage != null ? renamePackage.IsValid() : !string.IsNullOrEmpty(this.NewName);
163159
}
164160

165161
#endregion
@@ -191,19 +187,8 @@ private bool ValidateTargets()
191187
if (!isEnum && !isClass && !isGlobalFunction && !isGlobalNamespace)
192188
return true;
193189

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

208193
// Is this possible? should return false? I'm inclined to think so
209194
if (inFile == null) return true;
@@ -212,7 +197,7 @@ private bool ValidateTargets()
212197
String oldName = Path.GetFileNameWithoutExtension(oldFileName);
213198

214199
// Private classes and similars
215-
if (string.IsNullOrEmpty(oldName) || !oldName.Equals(originName))
200+
if (string.IsNullOrEmpty(oldName) || !oldName.Equals(member.Name))
216201
return true;
217202

218203
String fullPath = Path.GetFullPath(inFile.FileName);
@@ -242,7 +227,7 @@ private void OnFindAllReferencesCompleted(Object sender, RefactorCompleteEventAr
242227
var doc = AssociatedDocumentHelper.LoadDocument(entry.Key);
243228
var sci = doc.SciControl;
244229
// replace matches in the current file with the new name
245-
RefactoringHelper.ReplaceMatches(entry.Value, sci, this.newName);
230+
RefactoringHelper.ReplaceMatches(entry.Value, sci, this.NewName);
246231
//Uncomment if we want to keep modified files
247232
//if (sci.IsModify) AssociatedDocumentHelper.MarkDocumentToKeep(entry.Key);
248233
doc.Save();
@@ -289,7 +274,6 @@ private void RenameFile(IDictionary<string, List<SearchMatch>> results)
289274
project.SetDocumentClass(newFileName, true);
290275
project.Save();
291276
}
292-
293277
}
294278

295279
if (results.ContainsKey(oldFileName))
@@ -302,7 +286,7 @@ private void RenameFile(IDictionary<string, List<SearchMatch>> results)
302286
}
303287

304288
/// <summary>
305-
///
289+
/// Outputs the results to the TraceManager
306290
/// </summary>
307291
private void ReportResults()
308292
{

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: 63 additions & 14 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,6 +18,7 @@ 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
{
@@ -28,20 +30,27 @@ public static void AddToQueue(ASResult target, bool outputResults)
2830
string label = TextHelper.GetString("Label.NewName");
2931
string title = string.Format(TextHelper.GetString("Title.RenameDialog"), originalName);
3032
LineEntryDialog askName = new LineEntryDialog(title, label, originalName);
31-
if (askName.ShowDialog() == DialogResult.OK)
33+
if (askName.ShowDialog() != DialogResult.OK) return;
34+
string newName = askName.Line.Trim();
35+
if (newName.Length == 0 || newName == originalName) return;
36+
var cmd = new Rename(target, outputResults, newName);
37+
queue.Add(cmd);
38+
if (ASContext.Context.CurrentModel.haXe && target.Member != null &&
39+
(target.Member.Flags & (FlagType.Getter | FlagType.Setter)) > 0)
3240
{
33-
string newName = askName.Line.Trim();
34-
if (newName.Length == 0 || newName == originalName) return;
35-
queue.Add(new Rename(target, outputResults, newName));
36-
if (ASContext.Context.CurrentModel.haXe && target.Member != null &&
37-
(target.Member.Flags & (FlagType.Getter | FlagType.Setter)) > 0)
38-
{
39-
List<MemberModel> list = target.Member.Parameters;
40-
if (list[0].Name == "get") RenameMember(target.InClass, "get_" + originalName, "get_" + newName, outputResults);
41-
if (list[1].Name == "set") RenameMember(target.InClass, "set_" + originalName, "set_" + newName, outputResults);
42-
}
43-
if (currentCommand == null) ExecuteFirst();
41+
List<MemberModel> list = target.Member.Parameters;
42+
if (list[0].Name == "get") RenameMember(target.InClass, "get_" + originalName, "get_" + newName, outputResults);
43+
if (list[1].Name == "set") RenameMember(target.InClass, "set_" + originalName, "set_" + newName, outputResults);
4444
}
45+
if (currentCommand != null) return;
46+
var doc = PluginBase.MainForm.CurrentDocument;
47+
startState = new StartState
48+
{
49+
FileName = doc.FileName,
50+
CursorPosition = doc.SciControl.CurrentPos,
51+
Cmd = cmd
52+
};
53+
ExecuteFirst();
4554
}
4655

4756
static void RenameMember(ClassModel inClass, string name, string newName, bool outputResults)
@@ -53,7 +62,7 @@ static void RenameMember(ClassModel inClass, string name, string newName, bool o
5362
if (result.Member == null) return;
5463
queue.Add(new Rename(result, outputResults, newName));
5564
}
56-
65+
5766
static void ExecuteFirst()
5867
{
5968
try
@@ -74,7 +83,47 @@ static void ExecuteFirst()
7483
static void OnRefactorComplete(object sender, RefactorCompleteEventArgs<IDictionary<string, List<SearchMatch>>> e)
7584
{
7685
if (queue.Count > 0) ExecuteFirst();
77-
else currentCommand = null;
86+
else
87+
{
88+
RestoreStartState();
89+
currentCommand = null;
90+
startState = null;
91+
}
7892
}
93+
94+
static void RestoreStartState()
95+
{
96+
var fileName = startState.FileName;
97+
var cursorPosition = startState.CursorPosition;
98+
var cmd = startState.Cmd;
99+
var charsDiff = cmd.NewName.Length - cmd.TargetName.Length;
100+
foreach (var entry in cmd.Results)
101+
{
102+
if (entry.Key != fileName) continue;
103+
SearchMatch match = null;
104+
foreach (var tmpMatch in entry.Value)
105+
{
106+
var start = tmpMatch.Index - charsDiff;
107+
if (cursorPosition >= start)
108+
{
109+
charsDiff += charsDiff;
110+
match = tmpMatch;
111+
}
112+
else break;
113+
}
114+
var doc = (ITabbedDocument) PluginBase.MainForm.OpenEditableDocument(fileName);
115+
var sci = doc.SciControl;
116+
var pos = sci.PositionFromLine(match.Line - 1) + match.Column;
117+
sci.SetSel(pos, pos);
118+
break;
119+
}
120+
}
121+
}
122+
123+
class StartState
124+
{
125+
public string FileName;
126+
public int CursorPosition;
127+
public Rename Cmd;
79128
}
80129
}

0 commit comments

Comments
 (0)