diff --git a/src/SharpIDE.Application/Features/FileWatching/IdeFileExternalChangeHandler.cs b/src/SharpIDE.Application/Features/FileWatching/IdeFileExternalChangeHandler.cs index f26d923..9f91625 100644 --- a/src/SharpIDE.Application/Features/FileWatching/IdeFileExternalChangeHandler.cs +++ b/src/SharpIDE.Application/Features/FileWatching/IdeFileExternalChangeHandler.cs @@ -90,14 +90,12 @@ public class IdeFileExternalChangeHandler private async Task OnFileCreated(string filePath) { - // Create a new sharpIdeFile, update SolutionModel var sharpIdeFile = SolutionModel.AllFiles.SingleOrDefault(f => f.Path == filePath); if (sharpIdeFile is not null) { // It was likely already created via a parent folder creation return; } - // If sharpIdeFile is null, it means the file was created externally, and we need to create it and add it to the solution model var createdFileDirectory = Path.GetDirectoryName(filePath)!; var containingFolderOrProject = (IFolderOrProject?)SolutionModel.AllFolders.SingleOrDefault(f => f.ChildNodeBasePath == createdFileDirectory) ?? SolutionModel.AllProjects.SingleOrDefault(s => s.ChildNodeBasePath == createdFileDirectory); @@ -107,21 +105,7 @@ public class IdeFileExternalChangeHandler return; } - sharpIdeFile = new SharpIdeFile(filePath, Path.GetFileName(filePath), containingFolderOrProject, []); - - var correctInsertionPosition = containingFolderOrProject.Files.list.BinarySearch(sharpIdeFile, SharpIdeFileComparer.Instance); - if (correctInsertionPosition < 0) - { - correctInsertionPosition = ~correctInsertionPosition; - } - else - { - throw new InvalidOperationException("File already exists in the containing folder or project"); - } - containingFolderOrProject.Files.Insert(correctInsertionPosition, sharpIdeFile); - SolutionModel.AllFiles.Add(sharpIdeFile); - - await _fileChangedService.SharpIdeFileAdded(sharpIdeFile, await File.ReadAllTextAsync(filePath)); + await _sharpIdeSolutionModificationService.CreateFile(containingFolderOrProject, filePath, Path.GetFileName(filePath), await File.ReadAllTextAsync(filePath)); } private async Task OnFileChanged(string filePath) diff --git a/src/SharpIDE.Application/Features/FileWatching/SharpIdeSolutionModificationService.cs b/src/SharpIDE.Application/Features/FileWatching/SharpIdeSolutionModificationService.cs index ba6a484..f21ca2e 100644 --- a/src/SharpIDE.Application/Features/FileWatching/SharpIdeSolutionModificationService.cs +++ b/src/SharpIDE.Application/Features/FileWatching/SharpIdeSolutionModificationService.cs @@ -125,7 +125,18 @@ public class SharpIdeSolutionModificationService(FileChangedService fileChangedS public async Task 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;