Use correct C# file template when adding new file (#46)

Co-authored-by: Matt Parker <61717342+MattParkerDev@users.noreply.github.com>
This commit is contained in:
Manuel Gasser
2025-12-21 03:30:36 +01:00
committed by GitHub
parent 68817fd7dd
commit f7235f4c3c
3 changed files with 33 additions and 12 deletions

View File

@@ -64,14 +64,13 @@ public class IdeFileOperationsService(SharpIdeSolutionModificationService sharpI
await _sharpIdeSolutionModificationService.RemoveFile(file); await _sharpIdeSolutionModificationService.RemoveFile(file);
} }
// TODO: Pass class/interface/enum type to create different templates public async Task<SharpIdeFile> CreateCsFile(IFolderOrProject parentNode, string newFileName, string typeKeyword)
public async Task<SharpIdeFile> CreateCsFile(IFolderOrProject parentNode, string newFileName)
{ {
var newFilePath = Path.Combine(GetFileParentNodePath(parentNode), newFileName); var newFilePath = Path.Combine(GetFileParentNodePath(parentNode), newFileName);
if (File.Exists(newFilePath)) throw new InvalidOperationException($"File {newFilePath} already exists."); if (File.Exists(newFilePath)) throw new InvalidOperationException($"File {newFilePath} already exists.");
var className = Path.GetFileNameWithoutExtension(newFileName); var className = Path.GetFileNameWithoutExtension(newFileName);
var @namespace = NewFileTemplates.ComputeNamespace(parentNode); var @namespace = NewFileTemplates.ComputeNamespace(parentNode);
var fileText = NewFileTemplates.CsharpClass(className, @namespace); var fileText = NewFileTemplates.CsharpFile(className, @namespace, typeKeyword);
await File.WriteAllTextAsync(newFilePath, fileText); await File.WriteAllTextAsync(newFilePath, fileText);
var sharpIdeFile = await _sharpIdeSolutionModificationService.CreateFile(parentNode, newFilePath, newFileName, fileText); var sharpIdeFile = await _sharpIdeSolutionModificationService.CreateFile(parentNode, newFilePath, newFileName, fileText);
return sharpIdeFile; return sharpIdeFile;

View File

@@ -5,15 +5,16 @@ namespace SharpIDE.Application.Features.FileWatching;
public static class NewFileTemplates public static class NewFileTemplates
{ {
public static string CsharpClass(string className, string @namespace) public static string CsharpFile(string className, string @namespace, string typeKeyword)
{ {
var text = $$""" var text = $$"""
namespace {{@namespace}}; namespace {{@namespace}};
public class {{className}} public {{typeKeyword}} {{className}}
{ {
} }
"""; """;
return text; return text;
} }

View File

@@ -7,6 +7,12 @@ namespace SharpIDE.Godot.Features.SolutionExplorer.ContextMenus.Dialogs;
public partial class NewCsharpFileDialog : ConfirmationDialog 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 LineEdit _nameLineEdit = null!;
private ItemList _fileTypeItemList = null!; private ItemList _fileTypeItemList = null!;
@@ -22,11 +28,11 @@ public partial class NewCsharpFileDialog : ConfirmationDialog
_nameLineEdit.GrabFocus(); _nameLineEdit.GrabFocus();
_nameLineEdit.SelectAll(); _nameLineEdit.SelectAll();
_fileTypeItemList = GetNode<ItemList>("%FileTypeItemList"); _fileTypeItemList = GetNode<ItemList>("%FileTypeItemList");
_fileTypeItemList.AddItem("Class", _classIcon); _fileTypeItemList.AddItem(ClassType, _classIcon);
_fileTypeItemList.AddItem("Interface", _classIcon); _fileTypeItemList.AddItem(InterfaceType, _classIcon);
_fileTypeItemList.AddItem("Record", _classIcon); _fileTypeItemList.AddItem(RecordType, _classIcon);
_fileTypeItemList.AddItem("Struct", _classIcon); _fileTypeItemList.AddItem(StructType, _classIcon);
_fileTypeItemList.AddItem("Enum", _classIcon); _fileTypeItemList.AddItem(EnumType, _classIcon);
_fileTypeItemList.Select(0); _fileTypeItemList.Select(0);
_fileTypeItemList.ItemSelected += FileTypeItemListOnItemSelected; _fileTypeItemList.ItemSelected += FileTypeItemListOnItemSelected;
Confirmed += OnConfirmed; Confirmed += OnConfirmed;
@@ -71,9 +77,14 @@ public partial class NewCsharpFileDialog : ConfirmationDialog
fileName += ".cs"; fileName += ".cs";
} }
var selectedIndex = _fileTypeItemList.GetSelectedItems().Single();
var selectedFileTypeDisplayName = _fileTypeItemList.GetItemText(selectedIndex);
var typeKeyword = GetCsharpTypeKeywordFromUiDisplayName(selectedFileTypeDisplayName);
_ = Task.GodotRun(async () => _ = Task.GodotRun(async () =>
{ {
var sharpIdeFile = await _ideFileOperationsService.CreateCsFile(ParentNode, fileName); var sharpIdeFile = await _ideFileOperationsService.CreateCsFile(ParentNode, fileName, typeKeyword);
GodotGlobalEvents.Instance.FileExternallySelected.InvokeParallelFireAndForget(sharpIdeFile, null); GodotGlobalEvents.Instance.FileExternallySelected.InvokeParallelFireAndForget(sharpIdeFile, null);
}); });
QueueFree(); QueueFree();
@@ -83,4 +94,14 @@ public partial class NewCsharpFileDialog : ConfirmationDialog
{ {
return string.IsNullOrWhiteSpace(name); return string.IsNullOrWhiteSpace(name);
} }
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.")
};
} }