Create cs file v2

This commit is contained in:
Matt Parker
2025-10-20 19:45:58 +10:00
parent 7d379939e9
commit 6fa0049f62
4 changed files with 42 additions and 1 deletions

View File

@@ -18,4 +18,19 @@ public class IdeFileOperationsService(SharpIdeSolutionModificationService sharpI
Directory.Delete(folder.Path, true);
await _sharpIdeSolutionModificationService.RemoveDirectory(folder);
}
// public async Task DeleteFile(SharpIdeFile file)
// {
// File.Delete(file.Path);
// await _sharpIdeSolutionModificationService.RemoveFile(file);
// }
public async Task CreateCsFile(SharpIdeFolder parentFolder, string newFileName, string @namespace)
{
var newFilePath = Path.Combine(parentFolder.Path, newFileName);
var className = Path.GetFileNameWithoutExtension(newFileName);
var fileText = NewFileTemplates.CsharpClass(className, @namespace);
await File.WriteAllTextAsync(newFilePath, fileText);
await _sharpIdeSolutionModificationService.CreateFile(parentFolder, newFileName, fileText);
}
}

View File

@@ -0,0 +1,17 @@
namespace SharpIDE.Application.Features.FileWatching;
public static class NewFileTemplates
{
public static string CsharpClass(string className, string @namespace)
{
var text = $$"""
namespace {{@namespace}};
public class {{className}}
{
}
""";
return text;
}
}

View File

@@ -59,4 +59,13 @@ public class SharpIdeSolutionModificationService(FileChangedService fileChangedS
await _fileChangedService.SharpIdeFileRemoved(file);
}
}
public async Task CreateFile(SharpIdeFolder parentFolder, string fileName, string contents)
{
var newFilePath = Path.Combine(parentFolder.Path, fileName);
var sharpIdeFile = new SharpIdeFile(newFilePath, fileName, parentFolder, []);
parentFolder.Files.Add(sharpIdeFile);
SolutionModel.AllFiles.Add(sharpIdeFile);
await _fileChangedService.SharpIdeFileAdded(sharpIdeFile, contents);
}
}

View File

@@ -72,7 +72,7 @@ public partial class NewCsharpFileDialog : ConfirmationDialog
_ = Task.GodotRun(async () =>
{
//await _ideFileOperationsService.CreateCSharpFile(ParentFolder, fileName);
await _ideFileOperationsService.CreateCsFile(ParentFolder, fileName, "asdf");
});
QueueFree();
}