From f7235f4c3cda4f892abfcb24f230ba14455d6886 Mon Sep 17 00:00:00 2001 From: Manuel Gasser <38509042+magasser@users.noreply.github.com> Date: Sun, 21 Dec 2025 03:30:36 +0100 Subject: [PATCH] Use correct C# file template when adding new file (#46) Co-authored-by: Matt Parker <61717342+MattParkerDev@users.noreply.github.com> --- .../FileWatching/IdeFileOperationsService.cs | 5 ++- .../Features/FileWatching/NewFileTemplates.cs | 5 +-- .../Dialogs/NewCsharpFileDialog.cs | 35 +++++++++++++++---- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/SharpIDE.Application/Features/FileWatching/IdeFileOperationsService.cs b/src/SharpIDE.Application/Features/FileWatching/IdeFileOperationsService.cs index cef349f..8886418 100644 --- a/src/SharpIDE.Application/Features/FileWatching/IdeFileOperationsService.cs +++ b/src/SharpIDE.Application/Features/FileWatching/IdeFileOperationsService.cs @@ -64,14 +64,13 @@ public class IdeFileOperationsService(SharpIdeSolutionModificationService sharpI await _sharpIdeSolutionModificationService.RemoveFile(file); } - // TODO: Pass class/interface/enum type to create different templates - public async Task CreateCsFile(IFolderOrProject parentNode, string newFileName) + public async Task CreateCsFile(IFolderOrProject parentNode, string newFileName, string typeKeyword) { var newFilePath = Path.Combine(GetFileParentNodePath(parentNode), newFileName); if (File.Exists(newFilePath)) throw new InvalidOperationException($"File {newFilePath} already exists."); var className = Path.GetFileNameWithoutExtension(newFileName); var @namespace = NewFileTemplates.ComputeNamespace(parentNode); - var fileText = NewFileTemplates.CsharpClass(className, @namespace); + var fileText = NewFileTemplates.CsharpFile(className, @namespace, typeKeyword); await File.WriteAllTextAsync(newFilePath, fileText); var sharpIdeFile = await _sharpIdeSolutionModificationService.CreateFile(parentNode, newFilePath, newFileName, fileText); return sharpIdeFile; diff --git a/src/SharpIDE.Application/Features/FileWatching/NewFileTemplates.cs b/src/SharpIDE.Application/Features/FileWatching/NewFileTemplates.cs index 0a647c5..f8700f4 100644 --- a/src/SharpIDE.Application/Features/FileWatching/NewFileTemplates.cs +++ b/src/SharpIDE.Application/Features/FileWatching/NewFileTemplates.cs @@ -5,15 +5,16 @@ namespace SharpIDE.Application.Features.FileWatching; public static class NewFileTemplates { - public static string CsharpClass(string className, string @namespace) + public static string CsharpFile(string className, string @namespace, string typeKeyword) { var text = $$""" namespace {{@namespace}}; - public class {{className}} + public {{typeKeyword}} {{className}} { } + """; return text; } diff --git a/src/SharpIDE.Godot/Features/SolutionExplorer/ContextMenus/Dialogs/NewCsharpFileDialog.cs b/src/SharpIDE.Godot/Features/SolutionExplorer/ContextMenus/Dialogs/NewCsharpFileDialog.cs index f6d80ff..f69ee9b 100644 --- a/src/SharpIDE.Godot/Features/SolutionExplorer/ContextMenus/Dialogs/NewCsharpFileDialog.cs +++ b/src/SharpIDE.Godot/Features/SolutionExplorer/ContextMenus/Dialogs/NewCsharpFileDialog.cs @@ -7,6 +7,12 @@ namespace SharpIDE.Godot.Features.SolutionExplorer.ContextMenus.Dialogs; public partial class NewCsharpFileDialog : ConfirmationDialog { + private const string ClassType = "Class"; + private const string InterfaceType = "Interface"; + private const string RecordType = "Record"; + private const string StructType = "Struct"; + private const string EnumType = "Enum"; + private LineEdit _nameLineEdit = null!; private ItemList _fileTypeItemList = null!; @@ -22,11 +28,11 @@ public partial class NewCsharpFileDialog : ConfirmationDialog _nameLineEdit.GrabFocus(); _nameLineEdit.SelectAll(); _fileTypeItemList = GetNode("%FileTypeItemList"); - _fileTypeItemList.AddItem("Class", _classIcon); - _fileTypeItemList.AddItem("Interface", _classIcon); - _fileTypeItemList.AddItem("Record", _classIcon); - _fileTypeItemList.AddItem("Struct", _classIcon); - _fileTypeItemList.AddItem("Enum", _classIcon); + _fileTypeItemList.AddItem(ClassType, _classIcon); + _fileTypeItemList.AddItem(InterfaceType, _classIcon); + _fileTypeItemList.AddItem(RecordType, _classIcon); + _fileTypeItemList.AddItem(StructType, _classIcon); + _fileTypeItemList.AddItem(EnumType, _classIcon); _fileTypeItemList.Select(0); _fileTypeItemList.ItemSelected += FileTypeItemListOnItemSelected; Confirmed += OnConfirmed; @@ -71,9 +77,14 @@ public partial class NewCsharpFileDialog : ConfirmationDialog fileName += ".cs"; } + var selectedIndex = _fileTypeItemList.GetSelectedItems().Single(); + var selectedFileTypeDisplayName = _fileTypeItemList.GetItemText(selectedIndex); + + var typeKeyword = GetCsharpTypeKeywordFromUiDisplayName(selectedFileTypeDisplayName); + _ = Task.GodotRun(async () => { - var sharpIdeFile = await _ideFileOperationsService.CreateCsFile(ParentNode, fileName); + var sharpIdeFile = await _ideFileOperationsService.CreateCsFile(ParentNode, fileName, typeKeyword); GodotGlobalEvents.Instance.FileExternallySelected.InvokeParallelFireAndForget(sharpIdeFile, null); }); QueueFree(); @@ -83,4 +94,14 @@ public partial class NewCsharpFileDialog : ConfirmationDialog { return string.IsNullOrWhiteSpace(name); } -} \ No newline at end of file + + private static string GetCsharpTypeKeywordFromUiDisplayName(string typeDisplayName) => typeDisplayName switch + { + ClassType => "class", + InterfaceType => "interface", + RecordType => "record", + StructType => "struct", + EnumType => "enum", + _ => throw new ArgumentOutOfRangeException(nameof(typeDisplayName), $"The file type '{typeDisplayName}' is not supported.") + }; +}