Skip to content
This repository has been archived by the owner on Oct 4, 2021. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #4794 from mono/solutionfile-add-rename
Implement "add new" and "rename" for solution folder files
  • Loading branch information
slluis committed May 16, 2018
2 parents f90fdfc + d22dc8c commit b8a651d
Show file tree
Hide file tree
Showing 12 changed files with 186 additions and 93 deletions.
Expand Up @@ -1361,27 +1361,31 @@ void StartLabelEditInternal()

node.ExpandToNode (); //make sure the parent of the node that is being edited is expanded

string nodeName = node.NodeName;

GetNodeInfo (iter).Label = GLib.Markup.EscapeText (nodeName);
store.EmitRowChanged (store.GetPath (iter), iter);
string editText = node.NodeName;

// Get and validate the initial text selection
int nameLength = nodeName != null ? nodeName.Length : 0,
selectionStart = 0, selectionLength = nameLength;
int editTextLength = editText != null ? editText.Length : 0,
selectionStart = 0, selectionLength = editTextLength;

foreach (NodeBuilder b in node.NodeBuilderChain) {
try {
NodeCommandHandler handler = b.CommandHandler;
handler.SetCurrentNode(node);
handler.OnRenameStarting(ref selectionStart, ref selectionLength);
handler.OnRenameStarting(ref editText, ref selectionStart, ref selectionLength);
} catch (Exception ex) {
LoggingService.LogError (ex.ToString ());
}
}
if (selectionStart < 0 || selectionStart >= nameLength)

editTextLength = editText != null ? editText.Length : 0;

GetNodeInfo (iter).Label = GLib.Markup.EscapeText (editText);
store.EmitRowChanged (store.GetPath (iter), iter);

if (selectionStart < 0 || selectionStart >= editTextLength)
selectionStart = 0;
if (selectionStart + selectionLength > nameLength)
selectionLength = nameLength - selectionStart;
if (selectionStart + selectionLength > editTextLength)
selectionLength = editTextLength - selectionStart;
// This will apply the selection as soon as possible
GLib.Idle.Add (() => {
var editable = currentLabelEditable;
Expand Down
Expand Up @@ -85,7 +85,12 @@ object ICommandRouter.GetNextCommandTarget ()
protected ExtensibleTreeView Tree {
get { return tree; }
}


public virtual void OnRenameStarting (ref string startingText, ref int selectionStart, ref int selectionLength)
{
OnRenameStarting (ref selectionStart, ref selectionLength);
}

public virtual void OnRenameStarting (ref int selectionStart, ref int selectionLength)
{
}
Expand Down
Expand Up @@ -272,10 +272,10 @@ async System.Threading.Tasks.Task DropNode (HashSet<SolutionItem> projectsToSave
}
else if (dataObject is SolutionFolderFileNode) {
var sff = (SolutionFolderFileNode)dataObject;
sff.Parent.Files.Remove (sff.FileName);
sff.Parent.Files.Remove (sff.Path);

await IdeApp.ProjectOperations.SaveAsync (sff.Parent.ParentSolution);
source = ((SolutionFolderFileNode)dataObject).FileName;
source = ((SolutionFolderFileNode)dataObject).Path;
sourceProject = null;
what = null;
} else
Expand Down Expand Up @@ -433,7 +433,6 @@ public async void AddNewFileToProject()
CurrentNode.Expanded = true;
if (IdeApp.Workbench.ActiveDocument != null)
IdeApp.Workbench.ActiveDocument.Window.SelectWindow ();
await IdeApp.ProjectOperations.SaveAsync (project);
}

void OnFileInserted (ITreeNavigator nav)
Expand Down
Expand Up @@ -26,6 +26,7 @@

using System;
using System.Collections.Generic;
using System.IO;
using MonoDevelop.Projects;
using MonoDevelop.Ide.Gui.Components;
using MonoDevelop.Core;
Expand All @@ -49,16 +50,21 @@ class SolutionFolderFileNodeBuilder: TypeNodeBuilder

public override string GetNodeName (ITreeNavigator thisNode, object dataObject)
{
return ((SolutionFolderFileNode)dataObject).FileName;
return ((SolutionFolderFileNode)dataObject).Path;
}

public override void GetNodeAttributes (ITreeNavigator treeNavigator, object dataObject, ref NodeAttributes attributes)
{
attributes |= NodeAttributes.AllowRename;
}

public override void BuildNode (ITreeBuilder treeBuilder, object dataObject, NodeInfo nodeInfo)
{
SolutionFolderFileNode file = (SolutionFolderFileNode) dataObject;
nodeInfo.Label = file.FileName.FileName;
if (!System.IO.File.Exists (file.FileName))
nodeInfo.Label = file.Name;
if (!System.IO.File.Exists (file.Path))
nodeInfo.Label = "<span foreground='" + Styles.ErrorForegroundColor.ToHexString (false) + "'>" + nodeInfo.Label + "</span>";
nodeInfo.Icon = DesktopService.GetIconForFile (file.FileName, Gtk.IconSize.Menu);
nodeInfo.Icon = DesktopService.GetIconForFile (file.Path, Gtk.IconSize.Menu);
}

public override object GetParentObject (object dataObject)
Expand Down Expand Up @@ -87,17 +93,17 @@ public override void DeleteMultipleItems ()
foreach (ITreeNavigator nav in CurrentNodes) {
SolutionFolderFileNode file = (SolutionFolderFileNode) nav.DataItem;
if (file.Parent.IsRoot)
msg.Text = GettextCatalog.GetString ("Are you sure you want to remove the file {0} from the solution folder {1}?", file.FileName.FileName, file.Parent.Name);
msg.Text = GettextCatalog.GetString ("Are you sure you want to remove the file {0} from the solution folder {1}?", file.Name, file.Parent.Name);
else
msg.Text = GettextCatalog.GetString ("Are you sure you want to remove the file {0} from the solution {1}?", file.FileName.FileName, file.Parent.ParentSolution.Name);
msg.Text = GettextCatalog.GetString ("Are you sure you want to remove the file {0} from the solution {1}?", file.Name, file.Parent.ParentSolution.Name);
AlertButton result = MessageService.AskQuestion (msg);
if (result == AlertButton.Cancel)
return;

file.Parent.Files.Remove (file.FileName);
file.Parent.Files.Remove (file.Path);

if (result == AlertButton.Delete) {
FileService.DeleteFile (file.FileName);
FileService.DeleteFile (file.Path);
}

if (file.Parent != null && file.Parent.ParentSolution != null) {
Expand All @@ -111,7 +117,7 @@ public override void DeleteMultipleItems ()
public override void ActivateItem ()
{
SolutionFolderFileNode file = (SolutionFolderFileNode) CurrentNode.DataItem;
IdeApp.Workbench.OpenDocument (file.FileName, project: null);
IdeApp.Workbench.OpenDocument (file.Path, project: null);
}

public override DragOperation CanDragNode ()
Expand All @@ -123,32 +129,60 @@ public override DragOperation CanDragNode ()
public void OnOpenWith (object ob)
{
var finfo = (SolutionFolderFileNode)CurrentNode.DataItem;
((FileViewer)ob).OpenFile (finfo.FileName);
((FileViewer)ob).OpenFile (finfo.Path);
}

[CommandUpdateHandler (ViewCommands.OpenWithList)]
public void OnOpenWithUpdate (CommandArrayInfo info)
{
var pf = (SolutionFolderFileNode)CurrentNode.DataItem;
ProjectFileNodeCommandHandler.PopulateOpenWithViewers (info, null, pf.FileName);
ProjectFileNodeCommandHandler.PopulateOpenWithViewers (info, null, pf.Path);
}

public override void OnRenameStarting (ref string startingText, ref int selectionStart, ref int selectionLength)
{
var file = (SolutionFolderFileNode)CurrentNode.DataItem;
startingText = file.Name;
selectionStart = 0;
selectionLength = Path.GetFileNameWithoutExtension (startingText).Length;
}

public async override void RenameItem (string newName)
{
var file = (SolutionFolderFileNode)CurrentNode.DataItem;
var oldPath = file.Path;
if (SystemFileNodeCommandHandler.RenameFileWithConflictCheck (oldPath, newName, out string newPath)) {
//FIXME: implement this as a rename rather than an add/remove
file.Parent.Files.Remove (oldPath);
file.Parent.Files.Add (newPath);
await IdeApp.ProjectOperations.SaveAsync (file.Parent.ParentSolution);
}
}
}

class SolutionFolderFileNode: IFileItem
{
FilePath file;
FilePath path;
SolutionFolder parent;

public SolutionFolderFileNode (FilePath file, SolutionFolder parent)
public SolutionFolderFileNode (FilePath path, SolutionFolder parent)
{
this.file = file;
this.Path = path;
this.parent = parent;
}

public FilePath FileName {
get { return this.file; }
set { this.file = value; }

public FilePath Path {
get { return path; }
set { path = value; }
}

public string Name {
get { return path.FileName; }
}

//this is named misleadingly, so hide it away
FilePath IFileItem.FileName {
get { return path; }
}

public SolutionFolder Parent {
Expand All @@ -158,16 +192,16 @@ public SolutionFolderFileNode (FilePath file, SolutionFolder parent)

public override bool Equals (object obj)
{
SolutionFolderFileNode other = obj as SolutionFolderFileNode;
var other = obj as SolutionFolderFileNode;
if (other == null)
return false;
return file == other.file && parent == other.parent;
return path == other.path && parent == other.parent;
}

public override int GetHashCode ()
{
unchecked {
return file.GetHashCode () + parent.GetHashCode ();
return path.GetHashCode () | parent.GetHashCode ();
}
}
}
Expand Down
Expand Up @@ -321,16 +321,18 @@ public void OnUpdateReload (CommandInfo info)
}
}

/* [CommandHandler (ProjectCommands.AddNewFiles)]
protected void OnAddNewFiles ()
[CommandHandler (ProjectCommands.AddNewFiles)]
protected async void OnAddNewFiles ()
{
SolutionFolder folder = (SolutionFolder) CurrentNode.DataItem;
if (IdeApp.ProjectOperations.CreateProjectFile (null, folder.BaseDirectory)) {
IdeApp.ProjectOperations.Save (folder.ParentSolution);
CurrentNode.Expanded = true;
var folder = (SolutionFolder) CurrentNode.DataItem;
if (!IdeApp.ProjectOperations.CreateSolutionFolderFile (folder)) {
return;
}
}*/

CurrentNode.Expanded = true;
if (IdeApp.Workbench.ActiveDocument != null)
IdeApp.Workbench.ActiveDocument.Window.SelectWindow ();
}

[CommandHandler (ProjectCommands.AddFiles)]
protected async void OnAddFiles ()
{
Expand Down
Expand Up @@ -333,16 +333,18 @@ public void OnEditSolutionUpdate (CommandInfo info)
var solution = (Solution) CurrentNode.DataItem;
info.Visible = info.Enabled = !string.IsNullOrEmpty (solution.FileName) && File.Exists (solution.FileName);
}
/* [CommandHandler (ProjectCommands.AddNewFiles)]
protected void OnAddNewFiles ()

[CommandHandler (ProjectCommands.AddNewFiles)]
protected async void OnAddNewFiles ()
{
Solution sol = (Solution) CurrentNode.DataItem;
if (IdeApp.ProjectOperations.CreateProjectFile (null, sol.BaseDirectory)) {
IdeApp.ProjectOperations.Save (sol);
CurrentNode.Expanded = true;
var sln = (Solution)CurrentNode.DataItem;
if (!IdeApp.ProjectOperations.CreateSolutionFolderFile (sln.RootFolder)) {
return;
}
}*/
CurrentNode.Expanded = true;
if (IdeApp.Workbench.ActiveDocument != null)
IdeApp.Workbench.ActiveDocument.Window.SelectWindow ();
}

[CommandHandler (ProjectCommands.AddFiles)]
protected void OnAddFiles ()
Expand Down
Expand Up @@ -53,6 +53,7 @@ public SystemFile (FilePath absolutePath, WorkspaceObject parent, bool showTrans

public FilePath Path {
get { return absolutePath; }
internal set { absolutePath = value; }
}

FilePath IFileItem.FileName {
Expand Down
Expand Up @@ -84,27 +84,42 @@ public override void BuildNode (ITreeBuilder treeBuilder, object dataObject, Nod

class SystemFileNodeCommandHandler: NodeCommandHandler
{
public override void OnRenameStarting (ref int selectionStart, ref int selectionLength)
{
string name = CurrentNode.NodeName;
selectionStart = 0;
selectionLength = Path.GetFileNameWithoutExtension (name).Length;
}

public override void RenameItem (string newName)
{
SystemFile file = CurrentNode.DataItem as SystemFile;
string oldname = file.Path;
var file = (SystemFile)CurrentNode.DataItem;
if (RenameFileWithConflictCheck (file.Path, newName, out string newPath)) {
file.Path = newPath;
}
}

string newname = Path.Combine (Path.GetDirectoryName (oldname), newName);
if (newname != oldname) {
try {
if (!FileService.IsValidPath (newname) || ProjectFolderCommandHandler.ContainsDirectorySeparator (newName)) {
MessageService.ShowWarning (GettextCatalog.GetString ("The name you have chosen contains illegal characters. Please choose a different name."));
} else if (File.Exists (newname) || Directory.Exists (newname)) {
MessageService.ShowWarning (GettextCatalog.GetString ("File or directory name is already in use. Please choose a different one."));
} else {
FileService.RenameFile (oldname, newname);
}
} catch (System.ArgumentException) { // new file name with wildcard (*, ?) characters in it
public static bool RenameFileWithConflictCheck (FilePath oldPath, string newName, out string newPath)
{
newPath = oldPath.ParentDirectory.Combine (newName);
if (oldPath == newPath) {
return false;
}
try {
if (!FileService.IsValidPath (newPath) || ProjectFolderCommandHandler.ContainsDirectorySeparator (newName)) {
MessageService.ShowWarning (GettextCatalog.GetString ("The name you have chosen contains illegal characters. Please choose a different name."));
} catch (System.IO.IOException ex) {
MessageService.ShowError (GettextCatalog.GetString ("There was an error renaming the file."), ex);
} else if (File.Exists (newPath) || Directory.Exists (newPath)) {
MessageService.ShowWarning (GettextCatalog.GetString ("File or directory name is already in use. Please choose a different one."));
} else {
FileService.RenameFile (oldPath, newPath);
return true;
}
} catch (ArgumentException) { // new file name with wildcard (*, ?) characters in it
MessageService.ShowWarning (GettextCatalog.GetString ("The name you have chosen contains illegal characters. Please choose a different name."));
} catch (IOException ex) {
MessageService.ShowError (GettextCatalog.GetString ("There was an error renaming the file."), ex);
}
return false;
}

public override void ActivateItem ()
Expand Down

0 comments on commit b8a651d

Please sign in to comment.