add add file v1

This commit is contained in:
Matt Parker
2025-10-19 20:20:27 +10:00
parent 1a7c2acbd9
commit 5e1f1dfdd6
3 changed files with 58 additions and 1 deletions

View File

@@ -23,6 +23,15 @@ public class FileChangedService(RoslynAnalysis roslynAnalysis, IdeOpenTabsFileMa
public SharpIdeSolutionModel SolutionModel { get; set; } = null!;
public async Task SharpIdeFileAdded(SharpIdeFile file, string content)
{
if (file.IsRoslynWorkspaceFile)
{
await HandleWorkspaceFileAdded(file, content);
}
// TODO: handle csproj added
}
// All file changes should go via this service
public async Task SharpIdeFileChanged(SharpIdeFile file, string newContents, FileChangeType changeType)
{
@@ -86,4 +95,15 @@ public class FileChangedService(RoslynAnalysis roslynAnalysis, IdeOpenTabsFileMa
GlobalEvents.Instance.SolutionAltered.InvokeParallelFireAndForget();
await _roslynAnalysis.UpdateSolutionDiagnostics(newCts.Token);
}
private async Task HandleWorkspaceFileAdded(SharpIdeFile file, string contents)
{
var newCts = new CancellationTokenSource();
var oldCts = Interlocked.Exchange(ref _updateSolutionDiagnosticsCts, newCts);
await oldCts.CancelAsync();
oldCts.Dispose();
await _roslynAnalysis.AddDocument(file, contents);
GlobalEvents.Instance.SolutionAltered.InvokeParallelFireAndForget();
await _roslynAnalysis.UpdateSolutionDiagnostics(newCts.Token);
}
}

View File

@@ -1,4 +1,6 @@
using SharpIDE.Application.Features.Events;
using Ardalis.GuardClauses;
using SharpIDE.Application.Features.Events;
using SharpIDE.Application.Features.SolutionDiscovery;
using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence;
namespace SharpIDE.Application.Features.FileWatching;
@@ -11,6 +13,20 @@ public class IdeFileExternalChangeHandler
{
_fileChangedService = fileChangedService;
GlobalEvents.Instance.FileSystemWatcherInternal.FileChanged.Subscribe(OnFileChanged);
GlobalEvents.Instance.FileSystemWatcherInternal.FileCreated.Subscribe(OnFileCreated);
}
private async Task OnFileCreated(string filePath)
{
// Create a new sharpIdeFile, update SolutionModel
var sharpIdeFile = SolutionModel.AllFiles.SingleOrDefault(f => f.Path == filePath);
if (sharpIdeFile == 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
// sharpIdeFile = TODO;
}
Guard.Against.Null(sharpIdeFile, nameof(sharpIdeFile));
await _fileChangedService.SharpIdeFileAdded(sharpIdeFile, await File.ReadAllTextAsync(filePath));
}
private async Task OnFileChanged(string filePath)