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);
}
// TODO: Pass class/interface/enum type to create different templates
public async Task<SharpIdeFile> CreateCsFile(IFolderOrProject parentNode, string newFileName)
public async Task<SharpIdeFile> 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;

View File

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