From 135a5f5d18e04c25df517b053ad781dc3d9f5bd5 Mon Sep 17 00:00:00 2001 From: Matt Parker <61717342+MattParkerDev@users.noreply.github.com> Date: Mon, 20 Oct 2025 19:12:19 +1000 Subject: [PATCH] handle files from added directories better (?) --- .../IdeFileExternalChangeHandler.cs | 34 +++++++++---------- .../SharpIdeSolutionModificationService.cs | 4 +++ .../VsPersistence/SharpIdeModels.cs | 8 ++--- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/SharpIDE.Application/Features/FileWatching/IdeFileExternalChangeHandler.cs b/src/SharpIDE.Application/Features/FileWatching/IdeFileExternalChangeHandler.cs index 22a3cd9..2c300b8 100644 --- a/src/SharpIDE.Application/Features/FileWatching/IdeFileExternalChangeHandler.cs +++ b/src/SharpIDE.Application/Features/FileWatching/IdeFileExternalChangeHandler.cs @@ -53,25 +53,25 @@ public class IdeFileExternalChangeHandler { // Create a new sharpIdeFile, update SolutionModel var sharpIdeFile = SolutionModel.AllFiles.SingleOrDefault(f => f.Path == filePath); - if (sharpIdeFile == null) + if (sharpIdeFile is not null) { - // 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)!; - - // TODO: Handle being contained by a project directly - //var containingProject = SolutionModel.AllProjects.SingleOrDefault(p => createdFileDirectory == Path.GetDirectoryName(p.FilePath)); - var containingFolder = SolutionModel.AllFolders.SingleOrDefault(f => f.Path == createdFileDirectory); - if (containingFolder is null) - { - // TODO: Create the folder and add it to the solution model - } - - sharpIdeFile = new SharpIdeFile(filePath, Path.GetFileName(filePath), containingFolder, []); - containingFolder.Files.Add(sharpIdeFile); - SolutionModel.AllFiles.Add(sharpIdeFile); - // sharpIdeFile = TODO; + // It was likely already created via a parent folder creation + return; } - Guard.Against.Null(sharpIdeFile, nameof(sharpIdeFile)); + // 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.Path == createdFileDirectory) ?? SolutionModel.AllProjects.SingleOrDefault(s => s.FilePath == createdFileDirectory); + if (containingFolderOrProject is null) + { + Console.WriteLine($"Error - Containing Folder or Project of {filePath} does not exist"); + return; + } + + sharpIdeFile = new SharpIdeFile(filePath, Path.GetFileName(filePath), containingFolderOrProject, []); + containingFolderOrProject.Files.Add(sharpIdeFile); + SolutionModel.AllFiles.Add(sharpIdeFile); + await _fileChangedService.SharpIdeFileAdded(sharpIdeFile, await File.ReadAllTextAsync(filePath)); } diff --git a/src/SharpIDE.Application/Features/FileWatching/SharpIdeSolutionModificationService.cs b/src/SharpIDE.Application/Features/FileWatching/SharpIdeSolutionModificationService.cs index bd70138..d0ca750 100644 --- a/src/SharpIDE.Application/Features/FileWatching/SharpIdeSolutionModificationService.cs +++ b/src/SharpIDE.Application/Features/FileWatching/SharpIdeSolutionModificationService.cs @@ -22,6 +22,10 @@ public class SharpIdeSolutionModificationService(FileChangedService fileChangedS parentFolder.Folders.Add(sharpIdeFolder); SolutionModel.AllFolders.AddRange((IEnumerable)[sharpIdeFolder, ..allFolders]); SolutionModel.AllFiles.AddRange(allFiles); + foreach (var file in allFiles) + { + await _fileChangedService.SharpIdeFileAdded(file, await File.ReadAllTextAsync(file.Path)); + } return sharpIdeFolder; } diff --git a/src/SharpIDE.Application/Features/SolutionDiscovery/VsPersistence/SharpIdeModels.cs b/src/SharpIDE.Application/Features/SolutionDiscovery/VsPersistence/SharpIdeModels.cs index de4b3d8..85f1739 100644 --- a/src/SharpIDE.Application/Features/SolutionDiscovery/VsPersistence/SharpIdeModels.cs +++ b/src/SharpIDE.Application/Features/SolutionDiscovery/VsPersistence/SharpIdeModels.cs @@ -15,7 +15,7 @@ public interface IExpandableSharpIdeNode public bool Expanded { get; set; } } -public interface IFolderOrProject +public interface IFolderOrProject : IExpandableSharpIdeNode { public ObservableHashSet Folders { get; init; } public ObservableHashSet Files { get; init; } @@ -84,12 +84,12 @@ public class SharpIdeSolutionFolder : ISharpIdeNode, IExpandableSharpIdeNode, IC Projects = new ObservableHashSet(intermediateModel.Projects.Select(x => new SharpIdeProjectModel(x, allProjects, allFiles, allFolders, this))); } } -public class SharpIdeProjectModel : ISharpIdeNode, IExpandableSharpIdeNode, IChildSharpIdeNode +public class SharpIdeProjectModel : ISharpIdeNode, IExpandableSharpIdeNode, IChildSharpIdeNode, IFolderOrProject { public required string Name { get; set; } public required string FilePath { get; set; } - public required ObservableHashSet Folders { get; set; } - public required ObservableHashSet Files { get; set; } + public required ObservableHashSet Folders { get; init; } + public required ObservableHashSet Files { get; init; } public bool Expanded { get; set; } public required IExpandableSharpIdeNode Parent { get; set; } public bool Running { get; set; }