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:
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user