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
@@ -37,7 +39,9 @@ public class PluginMain : IPlugin
37
39
private String settingFilename ;
38
40
39
41
#region Required Properties
40
-
42
+
43
+ TreeView projectTreeView ;
44
+
41
45
/// <summary>
42
46
/// Api level of the plugin
43
47
/// </summary>
@@ -94,11 +98,11 @@ public Object Settings
94
98
{
95
99
get { return this . settingObject ; }
96
100
}
97
-
101
+
98
102
#endregion
99
103
100
104
#region Required Methods
101
-
105
+
102
106
/// <summary>
103
107
/// Initializes the plugin
104
108
/// </summary>
@@ -116,7 +120,7 @@ public void Dispose()
116
120
{
117
121
this . SaveSettings ( ) ;
118
122
}
119
-
123
+
120
124
/// <summary>
121
125
/// Handles the incoming events
122
126
/// </summary>
@@ -135,6 +139,7 @@ public void HandleEvent(Object sender, NotifyEvent e, HandlingPriority priority)
135
139
EventManager . DispatchEvent ( this , new DataEvent ( EventType . Command , "CodeRefactor.ContextMenu" , this . refactorContextMenu ) ) ;
136
140
// Watch resolved context for menu item updating...
137
141
ASComplete . OnResolvedContextChanged += OnResolvedContextChanged ;
142
+ DirectoryNode . OnDirectoryNodeRefresh += OnDirectoryNodeRefresh ;
138
143
this . UpdateMenuItems ( ) ;
139
144
break ;
140
145
@@ -157,7 +162,7 @@ public void HandleEvent(Object sender, NotifyEvent e, HandlingPriority priority)
157
162
}
158
163
else if ( IsValidForRename ( oldPath , newPath ) )
159
164
{
160
- RenameFile ( oldPath , newPath ) ;
165
+ MoveFile ( oldPath , newPath ) ;
161
166
e . Handled = true ;
162
167
}
163
168
break ;
@@ -177,6 +182,10 @@ public void HandleEvent(Object sender, NotifyEvent e, HandlingPriority priority)
177
182
case "ASCompletion.ContextualGenerator.AddOptions" :
178
183
OnAddRefactorOptions ( de . Data as List < ICompletionListItem > ) ;
179
184
break ;
185
+
186
+ case ProjectManagerEvents . TreeSelectionChanged :
187
+ OnTreeSelectionChanged ( ) ;
188
+ break ;
180
189
}
181
190
break ;
182
191
}
@@ -199,12 +208,20 @@ private static bool IsValidForRename(string oldPath, string newPath)
199
208
/// <summary>
200
209
/// Checks if the file or directory is valid for move command
201
210
/// </summary>
202
- private static bool IsValidForMove ( string oldPath , string newPath )
211
+ static bool IsValidForMove ( string oldPath )
203
212
{
204
213
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 ;
214
+ && ( File . Exists ( oldPath ) || Directory . Exists ( oldPath ) )
215
+ && IsValidFile ( oldPath ) ;
216
+ }
217
+
218
+ /// <summary>
219
+ /// Checks if the file or directory is valid for move command
220
+ /// </summary>
221
+ static bool IsValidForMove ( string oldPath , string newPath )
222
+ {
223
+ newPath = Path . GetFileNameWithoutExtension ( newPath ) ;
224
+ return IsValidForMove ( oldPath ) && Regex . Match ( newPath , REG_IDENTIFIER , RegexOptions . Singleline ) . Success ;
208
225
}
209
226
210
227
/// <summary>
@@ -220,9 +237,9 @@ private static bool IsValidFile(string file)
220
237
}
221
238
222
239
#endregion
223
-
240
+
224
241
#region Event Handling
225
-
242
+
226
243
/// <summary>
227
244
/// Initializes important variables
228
245
/// </summary>
@@ -446,7 +463,12 @@ private void RenameClicked(Object sender, EventArgs e)
446
463
/// </summary>
447
464
static void MoveClicked ( object sender , EventArgs e )
448
465
{
449
- 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 ) ;
450
472
if ( dialog . ShowDialog ( ) != DialogResult . OK ) return ;
451
473
Dictionary < string , string > oldPathToNewPath = new Dictionary < string , string > ( ) ;
452
474
foreach ( string file in dialog . MovingFiles )
@@ -459,7 +481,7 @@ static void MoveClicked(object sender, EventArgs e)
459
481
/// <summary>
460
482
///
461
483
/// </summary>
462
- private void RenameFile ( string oldPath , string newPath )
484
+ private void MoveFile ( string oldPath , string newPath )
463
485
{
464
486
try
465
487
{
@@ -712,6 +734,40 @@ void OnAddRefactorOptions(List<ICompletionListItem> list)
712
734
}
713
735
}
714
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
+
715
771
#endregion
716
772
}
717
773
}
0 commit comments