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
@@ -35,6 +37,7 @@ public class PluginMain : IPlugin
3537 private RefactorMenu refactorMainMenu ;
3638 private Settings settingObject ;
3739 private String settingFilename ;
40+ TreeView projectTreeView ;
3841
3942 #region Required Properties
4043
@@ -94,11 +97,11 @@ public Object Settings
9497 {
9598 get { return this . settingObject ; }
9699 }
97-
100+
98101 #endregion
99102
100103 #region Required Methods
101-
104+
102105 /// <summary>
103106 /// Initializes the plugin
104107 /// </summary>
@@ -116,7 +119,7 @@ public void Dispose()
116119 {
117120 this . SaveSettings ( ) ;
118121 }
119-
122+
120123 /// <summary>
121124 /// Handles the incoming events
122125 /// </summary>
@@ -135,6 +138,7 @@ public void HandleEvent(Object sender, NotifyEvent e, HandlingPriority priority)
135138 EventManager . DispatchEvent ( this , new DataEvent ( EventType . Command , "CodeRefactor.ContextMenu" , this . refactorContextMenu ) ) ;
136139 // Watch resolved context for menu item updating...
137140 ASComplete . OnResolvedContextChanged += OnResolvedContextChanged ;
141+ DirectoryNode . OnDirectoryNodeRefresh += OnDirectoryNodeRefresh ;
138142 this . UpdateMenuItems ( ) ;
139143 break ;
140144
@@ -157,7 +161,7 @@ public void HandleEvent(Object sender, NotifyEvent e, HandlingPriority priority)
157161 }
158162 else if ( IsValidForRename ( oldPath , newPath ) )
159163 {
160- RenameFile ( oldPath , newPath ) ;
164+ MoveFile ( oldPath , newPath ) ;
161165 e . Handled = true ;
162166 }
163167 break ;
@@ -177,6 +181,10 @@ public void HandleEvent(Object sender, NotifyEvent e, HandlingPriority priority)
177181 case "ASCompletion.ContextualGenerator.AddOptions" :
178182 OnAddRefactorOptions ( de . Data as List < ICompletionListItem > ) ;
179183 break ;
184+
185+ case ProjectManagerEvents . TreeSelectionChanged :
186+ OnTreeSelectionChanged ( ) ;
187+ break ;
180188 }
181189 break ;
182190 }
@@ -199,12 +207,20 @@ private static bool IsValidForRename(string oldPath, string newPath)
199207 /// <summary>
200208 /// Checks if the file or directory is valid for move command
201209 /// </summary>
202- private static bool IsValidForMove ( string oldPath , string newPath )
210+ static bool IsValidForMove ( string oldPath )
203211 {
204212 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 ;
208224 }
209225
210226 /// <summary>
@@ -220,9 +236,9 @@ private static bool IsValidFile(string file)
220236 }
221237
222238 #endregion
223-
239+
224240 #region Event Handling
225-
241+
226242 /// <summary>
227243 /// Initializes important variables
228244 /// </summary>
@@ -447,7 +463,12 @@ private void RenameClicked(Object sender, EventArgs e)
447463 /// </summary>
448464 static void MoveClicked ( object sender , EventArgs e )
449465 {
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 ) ;
451472 if ( dialog . ShowDialog ( ) != DialogResult . OK ) return ;
452473 Dictionary < string , string > oldPathToNewPath = new Dictionary < string , string > ( ) ;
453474 foreach ( string file in dialog . MovingFiles )
@@ -460,7 +481,7 @@ static void MoveClicked(object sender, EventArgs e)
460481 /// <summary>
461482 ///
462483 /// </summary>
463- private void RenameFile ( string oldPath , string newPath )
484+ private void MoveFile ( string oldPath , string newPath )
464485 {
465486 try
466487 {
@@ -713,6 +734,40 @@ void OnAddRefactorOptions(List<ICompletionListItem> list)
713734 }
714735 }
715736
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+
716771 #endregion
717772 }
718773}
0 commit comments