diff --git a/src/SharpIDE.Application/Features/FileWatching/SharpIdeSolutionModificationService.cs b/src/SharpIDE.Application/Features/FileWatching/SharpIdeSolutionModificationService.cs index f21ca2e..037327d 100644 --- a/src/SharpIDE.Application/Features/FileWatching/SharpIdeSolutionModificationService.cs +++ b/src/SharpIDE.Application/Features/FileWatching/SharpIdeSolutionModificationService.cs @@ -13,13 +13,24 @@ public class SharpIdeSolutionModificationService(FileChangedService fileChangedS public SharpIdeSolutionModel SolutionModel { get; set; } = null!; /// The directory must already exist on disk - public async Task AddDirectory(IFolderOrProject parentFolder, string directoryName) + public async Task AddDirectory(IFolderOrProject parentNode, string directoryName) { - var addedDirectoryPath = Path.Combine(parentFolder.ChildNodeBasePath, directoryName); + var addedDirectoryPath = Path.Combine(parentNode.ChildNodeBasePath, directoryName); var allFiles = new ConcurrentBag(); var allFolders = new ConcurrentBag(); - var sharpIdeFolder = new SharpIdeFolder(new DirectoryInfo(addedDirectoryPath), parentFolder, allFiles, allFolders); - parentFolder.Folders.Add(sharpIdeFolder); + var sharpIdeFolder = new SharpIdeFolder(new DirectoryInfo(addedDirectoryPath), parentNode, allFiles, allFolders); + + var correctInsertionPosition = parentNode.Folders.list.BinarySearch(sharpIdeFolder, SharpIdeFolderComparer.Instance); + if (correctInsertionPosition < 0) + { + correctInsertionPosition = ~correctInsertionPosition; + } + else + { + throw new InvalidOperationException("Folder already exists in the containing folder or project"); + } + + parentNode.Folders.Insert(correctInsertionPosition, sharpIdeFolder); SolutionModel.AllFolders.AddRange((IEnumerable)[sharpIdeFolder, ..allFolders]); SolutionModel.AllFiles.AddRange(allFiles); foreach (var file in allFiles) diff --git a/src/SharpIDE.Godot/Features/SolutionExplorer/SolutionExplorerPanel.cs b/src/SharpIDE.Godot/Features/SolutionExplorer/SolutionExplorerPanel.cs index 1e71b62..2b2b08b 100644 --- a/src/SharpIDE.Godot/Features/SolutionExplorer/SolutionExplorerPanel.cs +++ b/src/SharpIDE.Godot/Features/SolutionExplorer/SolutionExplorerPanel.cs @@ -225,7 +225,7 @@ public partial class SolutionExplorerPanel : MarginContainer foldersView.ObserveChanged() .SubscribeAwait(async (innerEvent, ct) => await (innerEvent.Action switch { - NotifyCollectionChangedAction.Add => this.InvokeAsync(() => innerEvent.NewItem.View.Value = CreateFolderTreeItem(_tree, projectItem, innerEvent.NewItem.Value)), + NotifyCollectionChangedAction.Add => this.InvokeAsync(() => innerEvent.NewItem.View.Value = CreateFolderTreeItem(_tree, projectItem, innerEvent.NewItem.Value, innerEvent.NewStartingIndex)), NotifyCollectionChangedAction.Remove => FreeTreeItem(innerEvent.OldItem.View.Value), _ => Task.CompletedTask })).AddTo(this); @@ -244,9 +244,9 @@ public partial class SolutionExplorerPanel : MarginContainer } [RequiresGodotUiThread] - private TreeItem CreateFolderTreeItem(Tree tree, TreeItem parent, SharpIdeFolder sharpIdeFolder) + private TreeItem CreateFolderTreeItem(Tree tree, TreeItem parent, SharpIdeFolder sharpIdeFolder, int newStartingIndex = -1) { - var folderItem = tree.CreateItem(parent); + var folderItem = tree.CreateItem(parent, newStartingIndex); folderItem.SetText(0, sharpIdeFolder.Name); folderItem.SetIcon(0, FolderIcon); folderItem.SetMetadata(0, new RefCountedContainer(sharpIdeFolder)); @@ -264,7 +264,7 @@ public partial class SolutionExplorerPanel : MarginContainer subFoldersView.ObserveChanged() .SubscribeAwait(async (innerEvent, ct) => await (innerEvent.Action switch { - NotifyCollectionChangedAction.Add => this.InvokeAsync(() => innerEvent.NewItem.View.Value = CreateFolderTreeItem(_tree, folderItem, innerEvent.NewItem.Value)), + NotifyCollectionChangedAction.Add => this.InvokeAsync(() => innerEvent.NewItem.View.Value = CreateFolderTreeItem(_tree, folderItem, innerEvent.NewItem.Value, innerEvent.NewStartingIndex)), NotifyCollectionChangedAction.Remove => FreeTreeItem(innerEvent.OldItem.View.Value), _ => Task.CompletedTask })).AddTo(this);