handle files from added directories better (?)

This commit is contained in:
Matt Parker
2025-10-20 19:12:19 +10:00
parent 36a2acbdf7
commit 135a5f5d18
3 changed files with 25 additions and 21 deletions

View File

@@ -53,25 +53,25 @@ public class IdeFileExternalChangeHandler
{ {
// Create a new sharpIdeFile, update SolutionModel // Create a new sharpIdeFile, update SolutionModel
var sharpIdeFile = SolutionModel.AllFiles.SingleOrDefault(f => f.Path == filePath); 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 // It was likely already created via a parent folder creation
var createdFileDirectory = Path.GetDirectoryName(filePath)!; return;
// 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;
} }
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)); await _fileChangedService.SharpIdeFileAdded(sharpIdeFile, await File.ReadAllTextAsync(filePath));
} }

View File

@@ -22,6 +22,10 @@ public class SharpIdeSolutionModificationService(FileChangedService fileChangedS
parentFolder.Folders.Add(sharpIdeFolder); parentFolder.Folders.Add(sharpIdeFolder);
SolutionModel.AllFolders.AddRange((IEnumerable<SharpIdeFolder>)[sharpIdeFolder, ..allFolders]); SolutionModel.AllFolders.AddRange((IEnumerable<SharpIdeFolder>)[sharpIdeFolder, ..allFolders]);
SolutionModel.AllFiles.AddRange(allFiles); SolutionModel.AllFiles.AddRange(allFiles);
foreach (var file in allFiles)
{
await _fileChangedService.SharpIdeFileAdded(file, await File.ReadAllTextAsync(file.Path));
}
return sharpIdeFolder; return sharpIdeFolder;
} }

View File

@@ -15,7 +15,7 @@ public interface IExpandableSharpIdeNode
public bool Expanded { get; set; } public bool Expanded { get; set; }
} }
public interface IFolderOrProject public interface IFolderOrProject : IExpandableSharpIdeNode
{ {
public ObservableHashSet<SharpIdeFolder> Folders { get; init; } public ObservableHashSet<SharpIdeFolder> Folders { get; init; }
public ObservableHashSet<SharpIdeFile> Files { get; init; } public ObservableHashSet<SharpIdeFile> Files { get; init; }
@@ -84,12 +84,12 @@ public class SharpIdeSolutionFolder : ISharpIdeNode, IExpandableSharpIdeNode, IC
Projects = new ObservableHashSet<SharpIdeProjectModel>(intermediateModel.Projects.Select(x => new SharpIdeProjectModel(x, allProjects, allFiles, allFolders, this))); Projects = new ObservableHashSet<SharpIdeProjectModel>(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 Name { get; set; }
public required string FilePath { get; set; } public required string FilePath { get; set; }
public required ObservableHashSet<SharpIdeFolder> Folders { get; set; } public required ObservableHashSet<SharpIdeFolder> Folders { get; init; }
public required ObservableHashSet<SharpIdeFile> Files { get; set; } public required ObservableHashSet<SharpIdeFile> Files { get; init; }
public bool Expanded { get; set; } public bool Expanded { get; set; }
public required IExpandableSharpIdeNode Parent { get; set; } public required IExpandableSharpIdeNode Parent { get; set; }
public bool Running { get; set; } public bool Running { get; set; }