update MSBuildWorkspace on doc change

This commit is contained in:
Matt Parker
2025-08-18 20:20:39 +10:00
parent d54e037cf9
commit 589b1a982e
3 changed files with 40 additions and 0 deletions

View File

@@ -288,4 +288,19 @@ public static class RoslynAnalysis
// }
}
}
// TODO: Use AdhocWorkspace or something else, to avoid writing to disk on every change
public static void UpdateDocument(SharpIdeFile fileModel, string newContent)
{
Guard.Against.Null(fileModel, nameof(fileModel));
Guard.Against.NullOrEmpty(newContent, nameof(newContent));
var project = _workspace!.CurrentSolution.Projects.Single(s => s.FilePath == ((IChildSharpIdeNode)fileModel).GetNearestProjectNode()!.FilePath);
var document = project.Documents.Single(s => s.FilePath == fileModel.Path);
Guard.Against.Null(document, nameof(document));
//var updatedDocument = document.WithText(SourceText.From(newContent));
var newSolution = _workspace.CurrentSolution.WithDocumentText(document.Id, SourceText.From(newContent));
_workspace.TryApplyChanges(newSolution);
}
}