From 502ba20b019fca982a4234f786911c424d0f12d6 Mon Sep 17 00:00:00 2001 From: Matt Parker <61717342+MattParkerDev@users.noreply.github.com> Date: Sun, 23 Nov 2025 00:38:07 +1000 Subject: [PATCH] refactor updating documents --- .../Features/Analysis/RoslynAnalysis.cs | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/SharpIDE.Application/Features/Analysis/RoslynAnalysis.cs b/src/SharpIDE.Application/Features/Analysis/RoslynAnalysis.cs index 2611f5e..558de57 100644 --- a/src/SharpIDE.Application/Features/Analysis/RoslynAnalysis.cs +++ b/src/SharpIDE.Application/Features/Analysis/RoslynAnalysis.cs @@ -920,14 +920,21 @@ public class RoslynAnalysis(ILogger logger, BuildService buildSe var newText = oldText.WithChanges(sourceText.GetTextChanges(oldText)); - var newSolution = fileModel switch + // We don't blow up if the document is not in the workspace - this would happen e.g. for files that are excluded. + // Roslyn implementations seem to handle this with a Misc Files workspace. TODO: Investigate + var currentSolution = _workspace!.CurrentSolution; + if (currentSolution.ContainsDocument(document.Id)) { - { IsRazorFile: true } => _workspace.CurrentSolution.WithAdditionalDocumentText(document.Id, newText), - { IsCsharpFile: true } => _workspace.CurrentSolution.WithDocumentText(document.Id, newText), - _ => throw new ArgumentOutOfRangeException() - }; - - _workspace.TryApplyChanges(newSolution); + _workspace.OnDocumentTextChanged(document.Id, newText, PreservationMode.PreserveIdentity, requireDocumentPresent: false); + } + else if (currentSolution.ContainsAdditionalDocument(document.Id)) + { + _workspace.OnAdditionalDocumentTextChanged(document.Id, newText, PreservationMode.PreserveIdentity); + } + else if (currentSolution.ContainsAnalyzerConfigDocument(document.Id)) + { + _workspace.OnAnalyzerConfigDocumentTextChanged(document.Id, newText, PreservationMode.PreserveIdentity); + } } public async Task AddDocument(SharpIdeFile fileModel, string content)