1515using PluginCore . Localization ;
1616using PluginCore . Managers ;
1717using PluginCore . Utilities ;
18+ using ProjectManager ;
1819using ProjectManager . Actions ;
20+ using ProjectManager . Controls . TreeView ;
1921using ProjectManager . Helpers ;
2022
2123namespace CodeRefactor
@@ -37,7 +39,9 @@ public class PluginMain : IPlugin
3739 private String settingFilename ;
3840
3941 #region Required Properties
40-
42+
43+ TreeView projectTreeView ;
44+
4145 /// <summary>
4246 /// Api level of the plugin
4347 /// </summary>
@@ -94,11 +98,11 @@ public Object Settings
9498 {
9599 get { return this . settingObject ; }
96100 }
97-
101+
98102 #endregion
99103
100104 #region Required Methods
101-
105+
102106 /// <summary>
103107 /// Initializes the plugin
104108 /// </summary>
@@ -116,7 +120,7 @@ public void Dispose()
116120 {
117121 this . SaveSettings ( ) ;
118122 }
119-
123+
120124 /// <summary>
121125 /// Handles the incoming events
122126 /// </summary>
@@ -135,6 +139,7 @@ public void HandleEvent(Object sender, NotifyEvent e, HandlingPriority priority)
135139 EventManager . DispatchEvent ( this , new DataEvent ( EventType . Command , "CodeRefactor.ContextMenu" , this . refactorContextMenu ) ) ;
136140 // Watch resolved context for menu item updating...
137141 ASComplete . OnResolvedContextChanged += OnResolvedContextChanged ;
142+ DirectoryNode . OnDirectoryNodeRefresh += OnDirectoryNodeRefresh ;
138143 this . UpdateMenuItems ( ) ;
139144 break ;
140145
@@ -157,7 +162,7 @@ public void HandleEvent(Object sender, NotifyEvent e, HandlingPriority priority)
157162 }
158163 else if ( IsValidForRename ( oldPath , newPath ) )
159164 {
160- RenameFile ( oldPath , newPath ) ;
165+ MoveFile ( oldPath , newPath ) ;
161166 e . Handled = true ;
162167 }
163168 break ;
@@ -177,6 +182,10 @@ public void HandleEvent(Object sender, NotifyEvent e, HandlingPriority priority)
177182 case "ASCompletion.ContextualGenerator.AddOptions" :
178183 OnAddRefactorOptions ( de . Data as List < ICompletionListItem > ) ;
179184 break ;
185+
186+ case ProjectManagerEvents . TreeSelectionChanged :
187+ OnTreeSelectionChanged ( ) ;
188+ break ;
180189 }
181190 break ;
182191 }
@@ -199,12 +208,20 @@ private static bool IsValidForRename(string oldPath, string newPath)
199208 /// <summary>
200209 /// Checks if the file or directory is valid for move command
201210 /// </summary>
202- private static bool IsValidForMove ( string oldPath , string newPath )
211+ static bool IsValidForMove ( string oldPath )
203212 {
204213 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 ;
208225 }
209226
210227 /// <summary>
@@ -220,9 +237,9 @@ private static bool IsValidFile(string file)
220237 }
221238
222239 #endregion
223-
240+
224241 #region Event Handling
225-
242+
226243 /// <summary>
227244 /// Initializes important variables
228245 /// </summary>
@@ -446,7 +463,12 @@ private void RenameClicked(Object sender, EventArgs e)
446463 /// </summary>
447464 static void MoveClicked ( object sender , EventArgs e )
448465 {
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 ) ;
450472 if ( dialog . ShowDialog ( ) != DialogResult . OK ) return ;
451473 Dictionary < string , string > oldPathToNewPath = new Dictionary < string , string > ( ) ;
452474 foreach ( string file in dialog . MovingFiles )
@@ -459,7 +481,7 @@ static void MoveClicked(object sender, EventArgs e)
459481 /// <summary>
460482 ///
461483 /// </summary>
462- private void RenameFile ( string oldPath , string newPath )
484+ private void MoveFile ( string oldPath , string newPath )
463485 {
464486 try
465487 {
@@ -712,6 +734,40 @@ void OnAddRefactorOptions(List<ICompletionListItem> list)
712734 }
713735 }
714736
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+
715771 #endregion
716772 }
717773}
0 commit comments