better file syncing (poorly)

This commit is contained in:
Matt Parker
2025-10-18 12:47:00 +10:00
parent 639945007b
commit 4d67503e13
4 changed files with 27 additions and 9 deletions

View File

@@ -9,6 +9,7 @@ namespace SharpIDE.Application.Features.FilePersistence;
/// Holds the in memory copies of files, and manages saving/loading them to/from disk.
public class IdeOpenTabsFileManager
{
public static IdeOpenTabsFileManager Instance { get; set; } = null!;
private ConcurrentDictionary<SharpIdeFile, Lazy<Task<string>>> _openFiles = new();
/// Implicitly 'opens' a file if not already open, and returns the text.
@@ -46,12 +47,17 @@ public class IdeOpenTabsFileManager
var newTextTaskLazy = new Lazy<Task<string>>(() => File.ReadAllTextAsync(file.Path));
_openFiles[file] = newTextTaskLazy;
var textTask = newTextTaskLazy.Value;
if (file.IsRoslynWorkspaceFile)
{
var text = await textTask;
await RoslynAnalysis.UpdateDocument(file, text);
GlobalEvents.Instance.SolutionAltered.InvokeParallelFireAndForget();
}
}
public async Task<bool> ReloadFileFromDiskIfOpenInEditor(SharpIdeFile file)
{
if (!_openFiles.ContainsKey(file)) return false;
var newTextTaskLazy = new Lazy<Task<string>>(() => File.ReadAllTextAsync(file.Path));
_openFiles[file] = newTextTaskLazy;
//var textTask = newTextTaskLazy.Value;
return true;
}
public async Task SaveFileAsync(SharpIdeFile file)

View File

@@ -1,6 +1,7 @@
using SharpIDE.Application.Features.Analysis;
using SharpIDE.Application.Features.Evaluation;
using SharpIDE.Application.Features.Events;
using SharpIDE.Application.Features.FilePersistence;
using SharpIDE.Application.Features.SolutionDiscovery;
using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence;
@@ -38,6 +39,16 @@ public class IdeFileSavedToDiskHandler
private async Task HandleWorkspaceFileChanged(SharpIdeFile file)
{
// TODO: Don't reload from disk if we raised the change event ourselves (e.g. save from IDE). Cleanup this whole disaster
var wasOpenAndUpdated = await IdeOpenTabsFileManager.Instance.ReloadFileFromDiskIfOpenInEditor(file);
if (file.IsRoslynWorkspaceFile)
{
var fileText = wasOpenAndUpdated ?
await IdeOpenTabsFileManager.Instance.GetFileTextAsync(file) :
await File.ReadAllTextAsync(file.Path);
await RoslynAnalysis.UpdateDocument(file, fileText);
GlobalEvents.Instance.SolutionAltered.InvokeParallelFireAndForget();
}
await RoslynAnalysis.UpdateSolutionDiagnostics();
}
}