Skip to content

Commit 18170ed

Browse files
committed
Merge pull request #1105 from SlavaRa/feature/CoeRefactor_ProjectManager_ContextMenu_Move
Adds item "Move" to context menu of Project Manager
2 parents 30e951c + fb8863d commit 18170ed

File tree

3 files changed

+81
-33
lines changed

3 files changed

+81
-33
lines changed

External/Plugins/CodeRefactor/Controls/MoveDialog.Designer.cs

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

External/Plugins/CodeRefactor/Controls/MoveDialog.cs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,23 @@ namespace CodeRefactor.Controls
1111
{
1212
public partial class MoveDialog : Form
1313
{
14-
static IEnumerable<string> GetClasspaths(string path)
14+
IEnumerable<string> GetClasspaths(string path)
1515
{
1616
return GetClasspaths(path, null);
1717
}
18-
static IEnumerable<string> GetClasspaths(string path, string projectDirName)
18+
IEnumerable<string> GetClasspaths(string path, string projectDirName)
1919
{
20-
string[] directories = Directory.GetDirectories(path, "*", SearchOption.AllDirectories);
21-
List<string> result = new List<string> {GetClasspath(path, projectDirName)};
22-
result.AddRange(directories.Select(it => GetClasspath(it, projectDirName)));
20+
var directories = new List<string> {path};
21+
directories.AddRange(Directory.GetDirectories(path, "*", SearchOption.AllDirectories));
22+
directories.RemoveAll(it =>
23+
{
24+
foreach (var movingFile in MovingFiles)
25+
{
26+
if (it.StartsWith(movingFile) || Path.GetDirectoryName(movingFile) == it) return true;
27+
}
28+
return false;
29+
});
30+
var result = directories.Select(it => GetClasspath(it, projectDirName));
2331
return result;
2432
}
2533

@@ -114,15 +122,6 @@ void FillTree()
114122
if (tree.Items.Count > 0) tree.SelectedIndex = 0;
115123
}
116124

117-
bool GetCanProcess()
118-
{
119-
object selectedItem = tree.SelectedItem;
120-
if (selectedItem == null) return false;
121-
string selectedPath = selectedItem.ToString();
122-
if (!Path.IsPathRooted(selectedPath)) selectedPath = Path.GetFullPath(selectedPath);
123-
return MovingFiles.All(file => Path.GetDirectoryName(file) != selectedPath);
124-
}
125-
126125
void OnShowExternalClasspathsCheckStateChanged(object sender, EventArgs e)
127126
{
128127
RefreshTree();
@@ -157,12 +156,7 @@ void OnTreeMouseDoubleClick(object sender, MouseEventArgs e)
157156
int indexFromPoint = tree.IndexFromPoint(e.Location);
158157
if (indexFromPoint == -1) return;
159158
tree.SelectedItem = tree.Items[indexFromPoint];
160-
if (GetCanProcess()) DialogResult = DialogResult.OK;
161-
}
162-
163-
void OnTreeSelectedValueChanged(object sender, EventArgs eventArgs)
164-
{
165-
processButton.Enabled = GetCanProcess();
159+
DialogResult = DialogResult.OK;
166160
}
167161

168162
protected override void OnKeyDown(KeyEventArgs e)

External/Plugins/CodeRefactor/PluginMain.cs

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
using PluginCore.Localization;
1616
using PluginCore.Managers;
1717
using PluginCore.Utilities;
18+
using ProjectManager;
1819
using ProjectManager.Actions;
20+
using ProjectManager.Controls.TreeView;
1921
using ProjectManager.Helpers;
2022

2123
namespace 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

Comments
 (0)