refactor file creation for a project parent

This commit is contained in:
Matt Parker
2025-10-20 20:09:43 +10:00
parent 0c3a3f7265
commit e2f9829aa0
5 changed files with 19 additions and 12 deletions

View File

@@ -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")
};
}

View File

@@ -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<string>();
IFolderOrProject? current = folder;

View File

@@ -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);
}

View File

@@ -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();
}

View File

@@ -89,7 +89,7 @@ public partial class SolutionExplorerPanel
else if (actionId is CreateNewSubmenuOptions.CSharpFile)
{
var newCsharpFileDialog = _newCsharpFileDialogScene.Instantiate<NewCsharpFileDialog>();
newCsharpFileDialog.ParentFolder = folder;
newCsharpFileDialog.ParentNode = folder;
AddChild(newCsharpFileDialog);
newCsharpFileDialog.PopupCentered();
}