Files
SharpIDE/src/SharpIDE.Application/Features/FileWatching/IdeFileExternalChangeHandler.cs
2025-10-17 20:33:54 +10:00

39 lines
1.3 KiB
C#

using SharpIDE.Application.Features.Analysis;
using SharpIDE.Application.Features.Evaluation;
using SharpIDE.Application.Features.Events;
using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence;
namespace SharpIDE.Application.Features.FileWatching;
public class IdeFileExternalChangeHandler
{
public SharpIdeSolutionModel SolutionModel { get; set; } = null!;
public IdeFileExternalChangeHandler()
{
GlobalEvents.Instance.FileSystemWatcherInternal.FileChanged.Subscribe(OnFileChanged);
}
private async Task OnFileChanged(string filePath)
{
var sharpIdeFile = SolutionModel.AllFiles.SingleOrDefault(f => f.Path == filePath);
if (sharpIdeFile is null) return;
if (sharpIdeFile.SuppressDiskChangeEvents is true) return;
if (sharpIdeFile.LastIdeWriteTime is not null)
{
var now = DateTimeOffset.Now;
if (now - sharpIdeFile.LastIdeWriteTime.Value < TimeSpan.FromMilliseconds(300))
{
Console.WriteLine($"IdeFileExternalChangeHandler: Ignored - {filePath}");
return;
}
}
Console.WriteLine($"IdeFileExternalChangeHandler: Changed - {filePath}");
var file = SolutionModel.AllFiles.SingleOrDefault(f => f.Path == filePath);
if (file is not null)
{
await file.FileContentsChangedExternallyFromDisk.InvokeParallelAsync();
await GlobalEvents.Instance.IdeFileSavedToDisk.InvokeParallelAsync(file);
}
}
}