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);
}
}

View File

@@ -59,6 +59,7 @@ highlight_current_line = true
gutters_draw_line_numbers = true
code_completion_enabled = true
auto_brace_completion_enabled = true
auto_brace_completion_highlight_matching = true
script = ExtResource("2_qjf5e")
[node name="CodeFixesMenu" type="PopupMenu" parent="VBoxContainer/HBoxContainer/HSplitContainer/SharpIdeCodeEdit"]

View File

@@ -45,9 +45,33 @@ public partial class SharpIdeCodeEdit : CodeEdit
_currentLine = GetCaretLine();
GD.Print($"Selection changed to line {_currentLine}, start {_selectionStartCol}, end {_selectionEndCol}");
};
TextChanged += OnTextChanged;
this.SyntaxHighlighter = _syntaxHighlighter;
}
private void OnTextChanged()
{
// update the MSBuildWorkspace
RoslynAnalysis.UpdateDocument(_currentFile, Text);
_ = Task.Run(async () =>
{
try
{
var syntaxHighlighting = await RoslynAnalysis.GetDocumentSyntaxHighlighting(_currentFile);
var diagnostics = await RoslynAnalysis.GetDocumentDiagnostics(_currentFile);
Callable.From(() =>
{
SetSyntaxHighlightingModel(syntaxHighlighting);
SetDiagnosticsModel(diagnostics);
}).CallDeferred();
}
catch (Exception ex)
{
GD.PrintErr($"Error Calling OnTextChanged: {ex.Message}");
}
});
}
private void OnCodeFixSelected(long id)
{
GD.Print($"Code fix selected: {id}");