handle copy pasting a file

This commit is contained in:
Matt Parker
2025-10-21 19:10:07 +10:00
parent fb489ec429
commit 439a8d96f9
3 changed files with 65 additions and 2 deletions

View File

@@ -34,9 +34,11 @@ 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)
{
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);
@@ -45,6 +47,16 @@ public class IdeFileOperationsService(SharpIdeSolutionModificationService sharpI
return sharpIdeFile;
}
public async Task<SharpIdeFile> CopyFile(IFolderOrProject destinationParentNode, string sourceFilePath, string newFileName)
{
var newFilePath = Path.Combine(GetFileParentNodePath(destinationParentNode), newFileName);
if (File.Exists(newFilePath)) throw new InvalidOperationException($"File {newFilePath} already exists.");
var fileContents = await File.ReadAllTextAsync(sourceFilePath);
File.Copy(sourceFilePath, newFilePath);
var sharpIdeFile = await _sharpIdeSolutionModificationService.CreateFile(destinationParentNode, newFilePath, newFileName, fileContents);
return sharpIdeFile;
}
private static string GetFileParentNodePath(IFolderOrProject parentNode) => parentNode switch
{
SharpIdeFolder folder => folder.Path,