diff --git a/src/SharpIDE.Application/Features/FilePersistence/IdeFileManager.cs b/src/SharpIDE.Application/Features/FilePersistence/IdeFileManager.cs index 6c6691f..77c0167 100644 --- a/src/SharpIDE.Application/Features/FilePersistence/IdeFileManager.cs +++ b/src/SharpIDE.Application/Features/FilePersistence/IdeFileManager.cs @@ -76,15 +76,11 @@ public class IdeFileManager private static async Task WriteAllText(SharpIdeFile file, string text) { - file.SuppressDiskChangeEvents.Value = true; + file.SuppressDiskChangeEvents = true; await File.WriteAllTextAsync(file.Path, text); Console.WriteLine($"Saved file {file.Path}"); - _ = Task.Delay(300).ContinueWith(_ => - { - Console.WriteLine($"Re-enabling disk change events for {file.Path}"); - file.SuppressDiskChangeEvents.Value = false; - Console.WriteLine($"Value is now {file.SuppressDiskChangeEvents.Value}"); - }, CancellationToken.None, TaskContinuationOptions.RunContinuationsAsynchronously, TaskScheduler.Default); + file.LastIdeWriteTime = DateTimeOffset.Now; + file.SuppressDiskChangeEvents = false; } public async Task SaveAllOpenFilesAsync() diff --git a/src/SharpIDE.Application/Features/FileWatching/IdeFileChangeHandler.cs b/src/SharpIDE.Application/Features/FileWatching/IdeFileChangeHandler.cs index 22134f5..c681ff0 100644 --- a/src/SharpIDE.Application/Features/FileWatching/IdeFileChangeHandler.cs +++ b/src/SharpIDE.Application/Features/FileWatching/IdeFileChangeHandler.cs @@ -15,7 +15,16 @@ public class IdeFileChangeHandler { var sharpIdeFile = SolutionModel.AllFiles.SingleOrDefault(f => f.Path == filePath); if (sharpIdeFile is null) return; - if (sharpIdeFile.SuppressDiskChangeEvents.Value is true) 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($"IdeFileChangeHandler: Ignored - {filePath}"); + return; + } + } Console.WriteLine($"IdeFileChangeHandler: Changed - {filePath}"); await sharpIdeFile.FileContentsChangedExternallyFromDisk.InvokeParallelAsync(); } diff --git a/src/SharpIDE.Application/Features/SolutionDiscovery/SharpIdeFile.cs b/src/SharpIDE.Application/Features/SolutionDiscovery/SharpIdeFile.cs index a3a4af6..ba843b7 100644 --- a/src/SharpIDE.Application/Features/SolutionDiscovery/SharpIdeFile.cs +++ b/src/SharpIDE.Application/Features/SolutionDiscovery/SharpIdeFile.cs @@ -16,7 +16,8 @@ public class SharpIdeFile : ISharpIdeNode, IChildSharpIdeNode public bool IsCsharpFile => Path.EndsWith(".cs", StringComparison.OrdinalIgnoreCase); public bool IsRoslynWorkspaceFile => IsCsharpFile || IsRazorFile || IsCshtmlFile; public required ReactiveProperty IsDirty { get; init; } - public required ReactiveProperty SuppressDiskChangeEvents { get; init; } + public required bool SuppressDiskChangeEvents { get; set; } // probably has concurrency issues + public required DateTimeOffset? LastIdeWriteTime { get; set; } public EventWrapper FileContentsChangedExternallyFromDisk { get; } = new(() => Task.CompletedTask); public EventWrapper FileContentsChangedExternally { get; } = new(() => Task.CompletedTask); @@ -27,7 +28,7 @@ public class SharpIdeFile : ISharpIdeNode, IChildSharpIdeNode Name = name; Parent = parent; IsDirty = new ReactiveProperty(false); - SuppressDiskChangeEvents = new ReactiveProperty(false); + SuppressDiskChangeEvents = false; allFiles.Add(this); } }