file change handling

This commit is contained in:
Matt Parker
2025-10-17 20:33:54 +10:00
parent ffdadf7a79
commit 882382bbd4
6 changed files with 20 additions and 10 deletions

View File

@@ -14,8 +14,7 @@ public class GlobalEvents
public EventWrapper<SharpIdeProjectModel, Task> ProjectStartedRunning { get; } = new(_ => Task.CompletedTask);
public EventWrapper<SharpIdeProjectModel, Task> ProjectStoppedRunning { get; } = new(_ => Task.CompletedTask);
public EventWrapper<ExecutionStopInfo, Task> DebuggerExecutionStopped { get; } = new(_ => Task.CompletedTask);
/// Meaning saved/persisted to disk
public EventWrapper<SharpIdeFile, Task> IdeFileChanged { get; } = new(_ => Task.CompletedTask);
public EventWrapper<SharpIdeFile, Task> IdeFileSavedToDisk { get; } = new(_ => Task.CompletedTask);
public FileSystemWatcherInternal FileSystemWatcherInternal { get; } = new();
}

View File

@@ -1,5 +1,6 @@
using System.Collections.Concurrent;
using SharpIDE.Application.Features.Analysis;
using SharpIDE.Application.Features.Events;
using SharpIDE.Application.Features.SolutionDiscovery;
namespace SharpIDE.Application.Features.FilePersistence;
@@ -59,6 +60,7 @@ public class IdeFileManager
var text = await GetFileTextAsync(file);
await WriteAllText(file, text);
file.IsDirty.Value = false;
GlobalEvents.Instance.IdeFileSavedToDisk.InvokeParallelFireAndForget(file);
}
public async Task UpdateInMemoryIfOpenAndSaveAsync(SharpIdeFile file, string newText)

View File

@@ -31,7 +31,8 @@ public class IdeFileExternalChangeHandler
var file = SolutionModel.AllFiles.SingleOrDefault(f => f.Path == filePath);
if (file is not null)
{
await GlobalEvents.Instance.IdeFileChanged.InvokeParallelAsync(file);
await file.FileContentsChangedExternallyFromDisk.InvokeParallelAsync();
await GlobalEvents.Instance.IdeFileSavedToDisk.InvokeParallelAsync(file);
}
}
}

View File

@@ -6,22 +6,25 @@ using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence;
namespace SharpIDE.Application.Features.FileWatching;
public class IdeFileChangeHandler
public class IdeFileSavedToDiskHandler
{
public SharpIdeSolutionModel SolutionModel { get; set; } = null!;
public IdeFileChangeHandler()
public IdeFileSavedToDiskHandler()
{
GlobalEvents.Instance.IdeFileChanged.Subscribe(HandleIdeFileChanged);
GlobalEvents.Instance.IdeFileSavedToDisk.Subscribe(HandleIdeFileChanged);
}
private async Task HandleIdeFileChanged(SharpIdeFile file)
{
await file.FileContentsChangedExternallyFromDisk.InvokeParallelAsync();
if (file.IsCsprojFile)
{
await HandleCsprojChanged(file);
}
else if (file.IsRoslynWorkspaceFile)
{
await HandleWorkspaceFileChanged(file);
}
}
private async Task HandleCsprojChanged(SharpIdeFile file)
@@ -32,4 +35,9 @@ public class IdeFileChangeHandler
await RoslynAnalysis.ReloadProject(project);
await RoslynAnalysis.UpdateSolutionDiagnostics();
}
private async Task HandleWorkspaceFileChanged(SharpIdeFile file)
{
await RoslynAnalysis.UpdateSolutionDiagnostics();
}
}

View File

@@ -51,7 +51,7 @@ public partial class IdeRoot : Control
Singletons.FileWatcher = new IdeFileWatcher();
Singletons.FileManager = new IdeFileManager();
Singletons.FileExternalChangeHandler = new IdeFileExternalChangeHandler();
Singletons.FileChangeHandler = new IdeFileChangeHandler();
Singletons.FileSavedToDiskHandler = new IdeFileSavedToDiskHandler();
}
public override void _Ready()
@@ -129,7 +129,7 @@ public partial class IdeRoot : Control
_searchWindow.Solution = solutionModel;
_searchAllFilesWindow.Solution = solutionModel;
Singletons.FileExternalChangeHandler.SolutionModel = solutionModel;
Singletons.FileChangeHandler.SolutionModel = solutionModel;
Singletons.FileSavedToDiskHandler.SolutionModel = solutionModel;
Callable.From(_solutionExplorerPanel.RepopulateTree).CallDeferred();
RoslynAnalysis.StartSolutionAnalysis(solutionModel);
Singletons.FileWatcher.StartWatching(solutionModel);

View File

@@ -13,6 +13,6 @@ public static class Singletons
public static IdeFileWatcher FileWatcher { get; set; } = null!;
public static IdeFileManager FileManager { get; set; } = null!;
public static IdeFileExternalChangeHandler FileExternalChangeHandler { get; set; } = null!;
public static IdeFileChangeHandler FileChangeHandler { get; set; } = null!;
public static IdeFileSavedToDiskHandler FileSavedToDiskHandler { get; set; } = null!;
public static AppState AppState { get; set; } = null!;
}