refactor file insertion logic

This commit is contained in:
Matt Parker
2025-10-31 18:29:41 +10:00
parent 8a693ce19d
commit 5e170a752e
2 changed files with 13 additions and 18 deletions

View File

@@ -125,7 +125,18 @@ public class SharpIdeSolutionModificationService(FileChangedService fileChangedS
public async Task<SharpIdeFile> CreateFile(IFolderOrProject parentNode, string newFilePath, string fileName, string contents)
{
var sharpIdeFile = new SharpIdeFile(newFilePath, fileName, parentNode, []);
parentNode.Files.Add(sharpIdeFile);
var correctInsertionPosition = parentNode.Files.list.BinarySearch(sharpIdeFile, SharpIdeFileComparer.Instance);
if (correctInsertionPosition < 0)
{
correctInsertionPosition = ~correctInsertionPosition;
}
else
{
throw new InvalidOperationException("File already exists in the containing folder or project");
}
parentNode.Files.Insert(correctInsertionPosition, sharpIdeFile);
SolutionModel.AllFiles.Add(sharpIdeFile);
await _fileChangedService.SharpIdeFileAdded(sharpIdeFile, contents);
return sharpIdeFile;