From e2f9829aa09bb5e437072766e7157781cc799b32 Mon Sep 17 00:00:00 2001 From: Matt Parker <61717342+MattParkerDev@users.noreply.github.com> Date: Mon, 20 Oct 2025 20:09:43 +1000 Subject: [PATCH] refactor file creation for a project parent --- .../FileWatching/IdeFileOperationsService.cs | 15 +++++++++++---- .../Features/FileWatching/NewFileTemplates.cs | 2 +- .../SharpIdeSolutionModificationService.cs | 7 +++---- .../ContextMenus/Dialogs/NewCsharpFileDialog.cs | 5 +++-- .../ContextMenus/FolderContextMenu.cs | 2 +- 5 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/SharpIDE.Application/Features/FileWatching/IdeFileOperationsService.cs b/src/SharpIDE.Application/Features/FileWatching/IdeFileOperationsService.cs index fcd76cf..6926d13 100644 --- a/src/SharpIDE.Application/Features/FileWatching/IdeFileOperationsService.cs +++ b/src/SharpIDE.Application/Features/FileWatching/IdeFileOperationsService.cs @@ -26,13 +26,20 @@ public class IdeFileOperationsService(SharpIdeSolutionModificationService sharpI await _sharpIdeSolutionModificationService.RemoveFile(file); } - public async Task CreateCsFile(SharpIdeFolder parentFolder, string newFileName) + public async Task CreateCsFile(IFolderOrProject parentNode, string newFileName) { - var newFilePath = Path.Combine(parentFolder.Path, newFileName); + var newFilePath = Path.Combine(GetFileParentNodePath(parentNode), newFileName); var className = Path.GetFileNameWithoutExtension(newFileName); - var @namespace = NewFileTemplates.ComputeNamespace(parentFolder); + var @namespace = NewFileTemplates.ComputeNamespace(parentNode); var fileText = NewFileTemplates.CsharpClass(className, @namespace); await File.WriteAllTextAsync(newFilePath, fileText); - await _sharpIdeSolutionModificationService.CreateFile(parentFolder, newFileName, fileText); + await _sharpIdeSolutionModificationService.CreateFile(parentNode, newFilePath, newFileName, fileText); } + + private static string GetFileParentNodePath(IFolderOrProject parentNode) => parentNode switch + { + SharpIdeFolder folder => folder.Path, + SharpIdeProjectModel project => Path.GetDirectoryName(project.FilePath)!, + _ => throw new InvalidOperationException("Parent node must be a folder or project") + }; } diff --git a/src/SharpIDE.Application/Features/FileWatching/NewFileTemplates.cs b/src/SharpIDE.Application/Features/FileWatching/NewFileTemplates.cs index d7d0356..0a647c5 100644 --- a/src/SharpIDE.Application/Features/FileWatching/NewFileTemplates.cs +++ b/src/SharpIDE.Application/Features/FileWatching/NewFileTemplates.cs @@ -18,7 +18,7 @@ public static class NewFileTemplates return text; } - public static string ComputeNamespace(SharpIdeFolder folder) + public static string ComputeNamespace(IFolderOrProject folder) { var names = new List(); IFolderOrProject? current = folder; diff --git a/src/SharpIDE.Application/Features/FileWatching/SharpIdeSolutionModificationService.cs b/src/SharpIDE.Application/Features/FileWatching/SharpIdeSolutionModificationService.cs index 8be443b..b67e5a2 100644 --- a/src/SharpIDE.Application/Features/FileWatching/SharpIdeSolutionModificationService.cs +++ b/src/SharpIDE.Application/Features/FileWatching/SharpIdeSolutionModificationService.cs @@ -60,11 +60,10 @@ public class SharpIdeSolutionModificationService(FileChangedService fileChangedS } } - public async Task CreateFile(SharpIdeFolder parentFolder, string fileName, string contents) + public async Task CreateFile(IFolderOrProject parentNode, string newFilePath, string fileName, string contents) { - var newFilePath = Path.Combine(parentFolder.Path, fileName); - var sharpIdeFile = new SharpIdeFile(newFilePath, fileName, parentFolder, []); - parentFolder.Files.Add(sharpIdeFile); + var sharpIdeFile = new SharpIdeFile(newFilePath, fileName, parentNode, []); + parentNode.Files.Add(sharpIdeFile); SolutionModel.AllFiles.Add(sharpIdeFile); await _fileChangedService.SharpIdeFileAdded(sharpIdeFile, contents); } diff --git a/src/SharpIDE.Godot/Features/SolutionExplorer/ContextMenus/Dialogs/NewCsharpFileDialog.cs b/src/SharpIDE.Godot/Features/SolutionExplorer/ContextMenus/Dialogs/NewCsharpFileDialog.cs index c686f02..33bd3f2 100644 --- a/src/SharpIDE.Godot/Features/SolutionExplorer/ContextMenus/Dialogs/NewCsharpFileDialog.cs +++ b/src/SharpIDE.Godot/Features/SolutionExplorer/ContextMenus/Dialogs/NewCsharpFileDialog.cs @@ -1,6 +1,7 @@ using Godot; using SharpIDE.Application.Features.FileWatching; using SharpIDE.Application.Features.SolutionDiscovery; +using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence; namespace SharpIDE.Godot.Features.SolutionExplorer.ContextMenus.Dialogs; @@ -9,7 +10,7 @@ public partial class NewCsharpFileDialog : ConfirmationDialog private LineEdit _nameLineEdit = null!; private ItemList _fileTypeItemList = null!; - public SharpIdeFolder ParentFolder { get; set; } = null!; + public IFolderOrProject ParentNode { get; set; } = null!; [Inject] private readonly IdeFileOperationsService _ideFileOperationsService = null!; @@ -72,7 +73,7 @@ public partial class NewCsharpFileDialog : ConfirmationDialog _ = Task.GodotRun(async () => { - await _ideFileOperationsService.CreateCsFile(ParentFolder, fileName); + await _ideFileOperationsService.CreateCsFile(ParentNode, fileName); }); QueueFree(); } diff --git a/src/SharpIDE.Godot/Features/SolutionExplorer/ContextMenus/FolderContextMenu.cs b/src/SharpIDE.Godot/Features/SolutionExplorer/ContextMenus/FolderContextMenu.cs index 985f415..3ef4d2d 100644 --- a/src/SharpIDE.Godot/Features/SolutionExplorer/ContextMenus/FolderContextMenu.cs +++ b/src/SharpIDE.Godot/Features/SolutionExplorer/ContextMenus/FolderContextMenu.cs @@ -89,7 +89,7 @@ public partial class SolutionExplorerPanel else if (actionId is CreateNewSubmenuOptions.CSharpFile) { var newCsharpFileDialog = _newCsharpFileDialogScene.Instantiate(); - newCsharpFileDialog.ParentFolder = folder; + newCsharpFileDialog.ParentNode = folder; AddChild(newCsharpFileDialog); newCsharpFileDialog.PopupCentered(); }