use DI in Godot Nodes

This commit is contained in:
Matt Parker
2025-10-18 14:20:04 +10:00
parent 4d67503e13
commit d285ce1bf2
13 changed files with 136 additions and 44 deletions

View File

@@ -1,6 +1,7 @@
using System.Threading.Channels;
using GDExtensionBindgen;
using Godot;
using SharpIDE.Application.Features.Build;
namespace SharpIDE.Godot.Features.Build;
@@ -8,10 +9,12 @@ public partial class BuildPanel : Control
{
private Terminal _terminal = null!;
private ChannelReader<string>? _buildOutputChannelReader;
[Inject] private readonly BuildService _buildService = null!;
public override void _Ready()
{
_terminal = new Terminal(GetNode<Control>("%Terminal"));
Singletons.BuildService.BuildStarted += OnBuildStarted;
_buildService.BuildStarted += OnBuildStarted;
}
public override void _Process(double delta)
@@ -27,6 +30,6 @@ public partial class BuildPanel : Control
private async Task OnBuildStarted()
{
await this.InvokeAsync(() => _terminal.Clear());
_buildOutputChannelReader ??= Singletons.BuildService.BuildTextWriter.ConsoleChannel.Reader;
_buildOutputChannelReader ??= _buildService.BuildTextWriter.ConsoleChannel.Reader;
}
}

View File

@@ -4,6 +4,7 @@ using R3;
using SharpIDE.Application.Features.Analysis;
using SharpIDE.Application.Features.Debugging;
using SharpIDE.Application.Features.Events;
using SharpIDE.Application.Features.Run;
using SharpIDE.Application.Features.SolutionDiscovery;
using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence;
@@ -18,6 +19,7 @@ public partial class CodeEditorPanel : MarginContainer
private TabContainer _tabContainer = null!;
private ExecutionStopInfo? _debuggerExecutionStopInfo;
[Inject] private readonly RunService _runService = null!;
public override void _Ready()
{
_tabContainer = GetNode<TabContainer>("TabContainer");
@@ -125,7 +127,7 @@ public partial class CodeEditorPanel : MarginContainer
_debuggerExecutionStopInfo = null;
_ = Task.GodotRun(async () =>
{
await Singletons.RunService.SendDebuggerStepOver(threadId);
await _runService.SendDebuggerStepOver(threadId);
});
}
}

View File

@@ -9,6 +9,8 @@ using SharpIDE.Application;
using SharpIDE.Application.Features.Analysis;
using SharpIDE.Application.Features.Debugging;
using SharpIDE.Application.Features.Events;
using SharpIDE.Application.Features.FilePersistence;
using SharpIDE.Application.Features.Run;
using SharpIDE.Application.Features.SolutionDiscovery;
using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence;
using SharpIDE.RazorAccess;
@@ -39,6 +41,10 @@ public partial class SharpIdeCodeEdit : CodeEdit
private ImmutableArray<CodeAction> _currentCodeActionsInPopup = [];
private bool _fileChangingSuppressBreakpointToggleEvent;
[Inject] private readonly IdeOpenTabsFileManager _openTabsFileManager = null!;
[Inject] private readonly RunService _runService = null!;
public override void _Ready()
{
SyntaxHighlighter = _syntaxHighlighter;
@@ -112,7 +118,7 @@ public partial class SharpIdeCodeEdit : CodeEdit
var lineInt = (int)line;
var breakpointAdded = IsLineBreakpointed(lineInt);
var lineForDebugger = lineInt + 1; // Godot is 0-indexed, Debugging is 1-indexed
var breakpoints = Singletons.RunService.Breakpoints.GetOrAdd(_currentFile, []);
var breakpoints = _runService.Breakpoints.GetOrAdd(_currentFile, []);
if (breakpointAdded)
{
breakpoints.Add(new Breakpoint { Line = lineForDebugger } );
@@ -265,7 +271,7 @@ public partial class SharpIdeCodeEdit : CodeEdit
_ = Task.GodotRun(async () =>
{
_currentFile.IsDirty.Value = true;
await Singletons.OpenTabsFileManager.UpdateFileTextInMemory(_currentFile, Text);
await _openTabsFileManager.UpdateFileTextInMemory(_currentFile, Text);
await _textChangedCts.CancelAsync(); // Currently the below methods throw, TODO Fix with suppress throwing, and handle
_textChangedCts.Dispose();
_textChangedCts = new CancellationTokenSource();
@@ -298,7 +304,7 @@ public partial class SharpIdeCodeEdit : CodeEdit
// TODO: This can be more efficient - we can just update in memory and proceed with highlighting etc. Save to disk in background.
foreach (var (affectedFile, updatedText) in affectedFiles)
{
await Singletons.OpenTabsFileManager.UpdateInMemoryIfOpenAndSaveAsync(affectedFile, updatedText);
await _openTabsFileManager.UpdateInMemoryIfOpenAndSaveAsync(affectedFile, updatedText);
affectedFile.FileContentsChangedExternally.InvokeParallelFireAndForget();
}
});
@@ -306,7 +312,7 @@ public partial class SharpIdeCodeEdit : CodeEdit
private async Task OnFileChangedExternallyInMemory()
{
var fileContents = await Singletons.OpenTabsFileManager.GetFileTextAsync(_currentFile);
var fileContents = await _openTabsFileManager.GetFileTextAsync(_currentFile);
var syntaxHighlighting = RoslynAnalysis.GetDocumentSyntaxHighlighting(_currentFile);
var razorSyntaxHighlighting = RoslynAnalysis.GetRazorDocumentSyntaxHighlighting(_currentFile);
var diagnostics = RoslynAnalysis.GetDocumentDiagnostics(_currentFile);
@@ -341,7 +347,7 @@ public partial class SharpIdeCodeEdit : CodeEdit
{
await Task.CompletedTask.ConfigureAwait(ConfigureAwaitOptions.ForceYielding); // get off the UI thread
_currentFile = file;
var readFileTask = Singletons.OpenTabsFileManager.GetFileTextAsync(file);
var readFileTask = _openTabsFileManager.GetFileTextAsync(file);
_currentFile.FileContentsChangedExternally.Subscribe(OnFileChangedExternallyInMemory);
_currentFile.FileContentsChangedExternallyFromDisk.Subscribe(OnFileChangedExternallyFromDisk);
@@ -365,7 +371,7 @@ public partial class SharpIdeCodeEdit : CodeEdit
private async Task OnFileChangedExternallyFromDisk()
{
await Singletons.OpenTabsFileManager.ReloadFileFromDisk(_currentFile);
await _openTabsFileManager.ReloadFileFromDisk(_currentFile);
await OnFileChangedExternallyInMemory();
}
@@ -432,14 +438,14 @@ public partial class SharpIdeCodeEdit : CodeEdit
{
_ = Task.GodotRun(async () =>
{
await Singletons.OpenTabsFileManager.SaveAllOpenFilesAsync();
await _openTabsFileManager.SaveAllOpenFilesAsync();
});
}
else if (@event.IsActionPressed(InputStringNames.SaveFile))
{
_ = Task.GodotRun(async () =>
{
await Singletons.OpenTabsFileManager.SaveFileAsync(_currentFile);
await _openTabsFileManager.SaveFileAsync(_currentFile);
});
}
}

View File

@@ -2,6 +2,7 @@ using Ardalis.GuardClauses;
using Godot;
using SharpIDE.Application.Features.Debugging;
using SharpIDE.Application.Features.Events;
using SharpIDE.Application.Features.Run;
using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence;
namespace SharpIDE.Godot.Features.Debug_.Tab.SubTabs;
@@ -14,6 +15,8 @@ public partial class ThreadsVariablesSubTab : Control
private VBoxContainer _variablesVboxContainer = null!;
public SharpIdeProjectModel Project { get; set; } = null!;
// private ThreadModel? _selectedThread = null!; // null when not at a stop point
[Inject] private readonly RunService _runService = null!;
public override void _Ready()
{
@@ -26,7 +29,7 @@ public partial class ThreadsVariablesSubTab : Control
private async Task OnDebuggerExecutionStopped(ExecutionStopInfo stopInfo)
{
var result = await Singletons.RunService.GetInfoAtStopPoint();
var result = await _runService.GetInfoAtStopPoint();
var threadScenes = result.Threads.Select(s =>
{
var threadListItem = _threadListItemScene.Instantiate<Control>();

View File

@@ -1,4 +1,5 @@
using Godot;
using SharpIDE.Application.Features.Run;
using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence;
using SharpIDE.Godot.Features.BottomPanel;
@@ -11,6 +12,8 @@ public partial class RunMenuItem : HBoxContainer
private Button _runButton = null!;
private Button _debugButton = null!;
private Button _stopButton = null!;
[Inject] private readonly RunService _runService = null!;
public override void _Ready()
{
_label = GetNode<Label>("Label");
@@ -47,18 +50,18 @@ public partial class RunMenuItem : HBoxContainer
private async void OnStopButtonPressed()
{
await Singletons.RunService.CancelRunningProject(Project);
await _runService.CancelRunningProject(Project);
}
private async void OnRunButtonPressed()
{
GodotGlobalEvents.Instance.BottomPanelTabExternallySelected.InvokeParallelFireAndForget(BottomPanelType.Run);
await Singletons.RunService.RunProject(Project).ConfigureAwait(false);
await _runService.RunProject(Project).ConfigureAwait(false);
}
private async void OnDebugButtonPressed()
{
GodotGlobalEvents.Instance.BottomPanelTabExternallySelected.InvokeParallelFireAndForget(BottomPanelType.Debug);
await Singletons.RunService.RunProject(Project, true).ConfigureAwait(false);
await _runService.RunProject(Project, true).ConfigureAwait(false);
}
}

View File

@@ -1,6 +1,9 @@
using Godot;
using SharpIDE.Application.Features.Analysis;
using SharpIDE.Application.Features.Build;
using SharpIDE.Application.Features.Evaluation;
using SharpIDE.Application.Features.Events;
using SharpIDE.Application.Features.Run;
using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence;
using SharpIDE.Godot.Features.BottomPanel;
@@ -18,6 +21,9 @@ file enum ProjectContextMenuOptions
public partial class SolutionExplorerPanel
{
private Texture2D _runIcon = ResourceLoader.Load<Texture2D>("uid://bkty6563cthj8");
[Inject] private readonly BuildService _buildService = null!;
[Inject] private readonly RunService _runService = null!;
private void OpenContextMenuProject(SharpIdeProjectModel project)
{
var menu = new PopupMenu();
@@ -38,7 +44,7 @@ public partial class SolutionExplorerPanel
_ = Task.GodotRun(async () =>
{
GodotGlobalEvents.Instance.BottomPanelTabExternallySelected.InvokeParallelFireAndForget(BottomPanelType.Run);
await Singletons.RunService.RunProject(project);
await _runService.RunProject(project);
});
}
if (actionId is ProjectContextMenuOptions.Build)
@@ -63,9 +69,9 @@ public partial class SolutionExplorerPanel
menu.Position = new Vector2I((int)globalMousePosition.X, (int)globalMousePosition.Y);
menu.Popup();
}
private static async Task MsBuildProject(SharpIdeProjectModel project, BuildType buildType)
private async Task MsBuildProject(SharpIdeProjectModel project, BuildType buildType)
{
GodotGlobalEvents.Instance.BottomPanelTabExternallySelected.InvokeParallelFireAndForget(BottomPanelType.Build);
await Singletons.BuildService.MsBuildAsync(project.FilePath, buildType);
await _buildService.MsBuildAsync(project.FilePath, buildType);
}
}