15
15
using PluginCore . Localization ;
16
16
using PluginCore . Managers ;
17
17
using PluginCore . Utilities ;
18
+ using ProjectManager ;
18
19
using ProjectManager . Actions ;
20
+ using ProjectManager . Controls . TreeView ;
19
21
using ProjectManager . Helpers ;
20
22
21
23
namespace CodeRefactor
@@ -35,6 +37,7 @@ public class PluginMain : IPlugin
35
37
private RefactorMenu refactorMainMenu ;
36
38
private Settings settingObject ;
37
39
private String settingFilename ;
40
+ TreeView projectTreeView ;
38
41
39
42
#region Required Properties
40
43
@@ -94,11 +97,11 @@ public Object Settings
94
97
{
95
98
get { return this . settingObject ; }
96
99
}
97
-
100
+
98
101
#endregion
99
102
100
103
#region Required Methods
101
-
104
+
102
105
/// <summary>
103
106
/// Initializes the plugin
104
107
/// </summary>
@@ -116,7 +119,7 @@ public void Dispose()
116
119
{
117
120
this . SaveSettings ( ) ;
118
121
}
119
-
122
+
120
123
/// <summary>
121
124
/// Handles the incoming events
122
125
/// </summary>
@@ -135,6 +138,7 @@ public void HandleEvent(Object sender, NotifyEvent e, HandlingPriority priority)
135
138
EventManager . DispatchEvent ( this , new DataEvent ( EventType . Command , "CodeRefactor.ContextMenu" , this . refactorContextMenu ) ) ;
136
139
// Watch resolved context for menu item updating...
137
140
ASComplete . OnResolvedContextChanged += OnResolvedContextChanged ;
141
+ DirectoryNode . OnDirectoryNodeRefresh += OnDirectoryNodeRefresh ;
138
142
this . UpdateMenuItems ( ) ;
139
143
break ;
140
144
@@ -157,7 +161,7 @@ public void HandleEvent(Object sender, NotifyEvent e, HandlingPriority priority)
157
161
}
158
162
else if ( IsValidForRename ( oldPath , newPath ) )
159
163
{
160
- RenameFile ( oldPath , newPath ) ;
164
+ MoveFile ( oldPath , newPath ) ;
161
165
e . Handled = true ;
162
166
}
163
167
break ;
@@ -177,6 +181,10 @@ public void HandleEvent(Object sender, NotifyEvent e, HandlingPriority priority)
177
181
case "ASCompletion.ContextualGenerator.AddOptions" :
178
182
OnAddRefactorOptions ( de . Data as List < ICompletionListItem > ) ;
179
183
break ;
184
+
185
+ case ProjectManagerEvents . TreeSelectionChanged :
186
+ OnTreeSelectionChanged ( ) ;
187
+ break ;
180
188
}
181
189
break ;
182
190
}
@@ -199,12 +207,20 @@ private static bool IsValidForRename(string oldPath, string newPath)
199
207
/// <summary>
200
208
/// Checks if the file or directory is valid for move command
201
209
/// </summary>
202
- private static bool IsValidForMove ( string oldPath , string newPath )
210
+ static bool IsValidForMove ( string oldPath )
203
211
{
204
212
return PluginBase . CurrentProject != null
205
- && ( File . Exists ( oldPath ) || Directory . Exists ( oldPath ) )
206
- && IsValidFile ( oldPath )
207
- && Regex . Match ( Path . GetFileNameWithoutExtension ( newPath ) , REG_IDENTIFIER , RegexOptions . Singleline ) . Success ;
213
+ && ( File . Exists ( oldPath ) || Directory . Exists ( oldPath ) )
214
+ && IsValidFile ( oldPath ) ;
215
+ }
216
+
217
+ /// <summary>
218
+ /// Checks if the file or directory is valid for move command
219
+ /// </summary>
220
+ static bool IsValidForMove ( string oldPath , string newPath )
221
+ {
222
+ newPath = Path . GetFileNameWithoutExtension ( newPath ) ;
223
+ return IsValidForMove ( oldPath ) && Regex . Match ( newPath , REG_IDENTIFIER , RegexOptions . Singleline ) . Success ;
208
224
}
209
225
210
226
/// <summary>
@@ -220,9 +236,9 @@ private static bool IsValidFile(string file)
220
236
}
221
237
222
238
#endregion
223
-
239
+
224
240
#region Event Handling
225
-
241
+
226
242
/// <summary>
227
243
/// Initializes important variables
228
244
/// </summary>
@@ -447,7 +463,12 @@ private void RenameClicked(Object sender, EventArgs e)
447
463
/// </summary>
448
464
static void MoveClicked ( object sender , EventArgs e )
449
465
{
450
- MoveDialog dialog = new MoveDialog ( PluginBase . MainForm . CurrentDocument . FileName ) ;
466
+ MoveFile ( PluginBase . MainForm . CurrentDocument . FileName ) ;
467
+ }
468
+
469
+ static void MoveFile ( string fileName )
470
+ {
471
+ MoveDialog dialog = new MoveDialog ( fileName ) ;
451
472
if ( dialog . ShowDialog ( ) != DialogResult . OK ) return ;
452
473
Dictionary < string , string > oldPathToNewPath = new Dictionary < string , string > ( ) ;
453
474
foreach ( string file in dialog . MovingFiles )
@@ -460,7 +481,7 @@ static void MoveClicked(object sender, EventArgs e)
460
481
/// <summary>
461
482
///
462
483
/// </summary>
463
- private void RenameFile ( string oldPath , string newPath )
484
+ private void MoveFile ( string oldPath , string newPath )
464
485
{
465
486
try
466
487
{
@@ -713,6 +734,40 @@ void OnAddRefactorOptions(List<ICompletionListItem> list)
713
734
}
714
735
}
715
736
737
+ void OnDirectoryNodeRefresh ( DirectoryNode node )
738
+ {
739
+ projectTreeView = node . TreeView ;
740
+ }
741
+
742
+ void OnTreeSelectionChanged ( )
743
+ {
744
+ if ( projectTreeView == null ) return ;
745
+ string path = null ;
746
+ var node = projectTreeView . SelectedNode as GenericNode ;
747
+ if ( node != null ) path = node . BackingPath ;
748
+ if ( string . IsNullOrEmpty ( path ) ) return ;
749
+ path = Path . GetFullPath ( path ) ;
750
+ if ( ! IsValidForMove ( path ) ) return ;
751
+ var menu = ( ProjectContextMenu ) projectTreeView . ContextMenuStrip ;
752
+ var index = menu . Items . IndexOf ( menu . Rename ) ;
753
+ if ( index == - 1 ) return ;
754
+ var item = new ToolStripMenuItem ( TextHelper . GetString ( "Label.Move" ) ) ;
755
+ item . ShortcutKeys = PluginBase . MainForm . GetShortcutItemKeys ( "RefactorMenu.Move" ) ;
756
+ item . Click += OnMoveItemClick ;
757
+ menu . Items . Insert ( index + 1 , item ) ;
758
+ }
759
+
760
+ void OnMoveItemClick ( object sender , EventArgs eventArgs )
761
+ {
762
+ string path = null ;
763
+ var node = projectTreeView . SelectedNode as GenericNode ;
764
+ if ( node != null ) path = node . BackingPath ;
765
+ if ( string . IsNullOrEmpty ( path ) ) return ;
766
+ path = Path . GetFullPath ( path ) ;
767
+ if ( ! IsValidForMove ( path ) ) return ;
768
+ MoveFile ( path ) ;
769
+ }
770
+
716
771
#endregion
717
772
}
718
773
}
0 commit comments