From 91f19a0552d2c8f4fba2a78ea735c694465f4a21 Mon Sep 17 00:00:00 2001 From: Matt Parker <61717342+MattParkerDev@users.noreply.github.com> Date: Sun, 23 Nov 2025 18:30:44 +1000 Subject: [PATCH] ignore file changes not in workspace --- .../Features/Analysis/RoslynAnalysis.cs | 15 +++++++++++---- .../Features/FileWatching/FileChangedService.cs | 6 ++++-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/SharpIDE.Application/Features/Analysis/RoslynAnalysis.cs b/src/SharpIDE.Application/Features/Analysis/RoslynAnalysis.cs index ac20934..d9dbdb5 100644 --- a/src/SharpIDE.Application/Features/Analysis/RoslynAnalysis.cs +++ b/src/SharpIDE.Application/Features/Analysis/RoslynAnalysis.cs @@ -920,7 +920,8 @@ public class RoslynAnalysis(ILogger logger, BuildService buildSe return null; } - public async Task UpdateDocument(SharpIdeFile fileModel, string newContent) + // Returns true if the document was found and updated, otherwise false + public async Task UpdateDocument(SharpIdeFile fileModel, string newContent) { using var _ = SharpIdeOtel.Source.StartActivity($"{nameof(RoslynAnalysis)}.{nameof(UpdateDocument)}"); await _solutionLoadedTcs.Task; @@ -937,7 +938,7 @@ public class RoslynAnalysis(ILogger logger, BuildService buildSe if (documentId is null) { _logger.LogWarning("UpdateDocument failed: Document '{DocumentPath}' not found in workspace", fileModel.Path); - return; + return false; } var newText = SourceText.From(newContent, Encoding.UTF8); @@ -957,6 +958,11 @@ public class RoslynAnalysis(ILogger logger, BuildService buildSe { _workspace.OnAnalyzerConfigDocumentTextChanged(documentId, newText, PreservationMode.PreserveIdentity); } + else + { + return false; + } + return true; } public async Task AddDocument(SharpIdeFile fileModel, string content) @@ -1022,7 +1028,7 @@ public class RoslynAnalysis(ILogger logger, BuildService buildSe }, WorkspaceChangeKind.DocumentAdded, documentId: documentId); } - public async Task RemoveDocument(SharpIdeFile fileModel) + public async Task RemoveDocument(SharpIdeFile fileModel) { using var _ = SharpIdeOtel.Source.StartActivity($"{nameof(RoslynAnalysis)}.{nameof(AddDocument)}"); await _solutionLoadedTcs.Task; @@ -1038,7 +1044,7 @@ public class RoslynAnalysis(ILogger logger, BuildService buildSe if (documentId is null) { _logger.LogWarning("RemoveDocument failed: Document '{DocumentPath}' not found in workspace", fileModel.Path); - return; + return false; } var documentKind = _workspace.CurrentSolution.GetDocumentKind(documentId); Guard.Against.Null(documentKind); @@ -1050,6 +1056,7 @@ public class RoslynAnalysis(ILogger logger, BuildService buildSe case TextDocumentKind.AnalyzerConfigDocument: _workspace.OnAnalyzerConfigDocumentRemoved(documentId); break; default: throw new ArgumentOutOfRangeException(nameof(documentKind)); } + return true; } public async Task MoveDocument(SharpIdeFile sharpIdeFile, string oldFilePath) diff --git a/src/SharpIDE.Application/Features/FileWatching/FileChangedService.cs b/src/SharpIDE.Application/Features/FileWatching/FileChangedService.cs index 688ca07..00eb5b7 100644 --- a/src/SharpIDE.Application/Features/FileWatching/FileChangedService.cs +++ b/src/SharpIDE.Application/Features/FileWatching/FileChangedService.cs @@ -132,7 +132,8 @@ public class FileChangedService private async Task HandleWorkspaceFileChanged(SharpIdeFile file, string newContents) { - await _roslynAnalysis.UpdateDocument(file, newContents); + var fileUpdatedInWorkspace = await _roslynAnalysis.UpdateDocument(file, newContents); + if (fileUpdatedInWorkspace is false) return; GlobalEvents.Instance.SolutionAltered.InvokeParallelFireAndForget(); _updateSolutionDiagnosticsQueue.AddWork(); } @@ -146,7 +147,8 @@ public class FileChangedService private async Task HandleWorkspaceFileRemoved(SharpIdeFile file) { - await _roslynAnalysis.RemoveDocument(file); + var success = await _roslynAnalysis.RemoveDocument(file); + if (success is false) return; GlobalEvents.Instance.SolutionAltered.InvokeParallelFireAndForget(); _updateSolutionDiagnosticsQueue.AddWork(); }