Use generic ref counted container for metadata

This commit is contained in:
Matt Parker
2025-10-01 00:49:47 +10:00
parent 854b14ba24
commit 9db1400ef7
7 changed files with 21 additions and 45 deletions

View File

@@ -1,20 +0,0 @@
using Godot;
using Microsoft.CodeAnalysis;
using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence;
namespace SharpIDE.Godot.Features.Problems;
// public partial class GenericContainer<T>(T item) : RefCounted
// {
// public T Item { get; } = item;
// }
public partial class DiagnosticMetadataContainer(Diagnostic diagnostic) : RefCounted
{
public Diagnostic Diagnostic { get; } = diagnostic;
}
public partial class ProjectContainer(SharpIdeProjectModel project) : RefCounted
{
public SharpIdeProjectModel Project { get; } = project;
}

View File

@@ -64,7 +64,7 @@ public partial class ProblemsPanel : Control
var treeItem = tree.CreateItem(parent);
treeItem.SetText(0, e.NewItem.Value.Name);
treeItem.SetIcon(0, CsprojIcon);
treeItem.SetMetadata(0, new ProjectContainer(e.NewItem.Value));
treeItem.SetMetadata(0, new RefCountedContainer<SharpIdeProjectModel>(e.NewItem.Value));
e.NewItem.View.Value = treeItem;
Observable.EveryValueChanged(e.NewItem.Value, s => s.Diagnostics.Count).Subscribe(s => treeItem.Visible = s is not 0).AddTo(this);
@@ -86,7 +86,7 @@ public partial class ProblemsPanel : Control
{
var diagItem = tree.CreateItem(parent);
diagItem.SetText(0, e.NewItem.Value.GetMessage());
diagItem.SetMetadata(0, new DiagnosticMetadataContainer(e.NewItem.Value));
diagItem.SetMetadata(0, new RefCountedContainer<Diagnostic>(e.NewItem.Value));
diagItem.SetIcon(0, e.NewItem.Value.Severity switch
{
DiagnosticSeverity.Error => ErrorIcon,
@@ -108,13 +108,13 @@ public partial class ProblemsPanel : Control
private void TreeOnItemActivated()
{
var selected = _tree.GetSelected();
var diagnosticContainer = selected.GetMetadata(0).As<DiagnosticMetadataContainer?>();
var diagnosticContainer = selected.GetMetadata(0).As<RefCountedContainer<Diagnostic>?>();
if (diagnosticContainer is null) return;
var diagnostic = diagnosticContainer.Diagnostic;
var diagnostic = diagnosticContainer.Item;
var parentTreeItem = selected.GetParent();
var projectContainer = parentTreeItem.GetMetadata(0).As<ProjectContainer?>();
var projectContainer = parentTreeItem.GetMetadata(0).As<RefCountedContainer<SharpIdeProjectModel>?>();
if (projectContainer is null) return;
var projectModel = projectContainer.Project;
var projectModel = projectContainer.Item;
OpenDocumentContainingDiagnostic(diagnostic, projectModel);
}

View File

@@ -0,0 +1,10 @@
using Godot;
using Microsoft.CodeAnalysis;
using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence;
namespace SharpIDE.Godot.Features.Problems;
public partial class RefCountedContainer<T>(T item) : RefCounted
{
public T Item { get; } = item;
}

View File

@@ -1,14 +0,0 @@
using Godot;
using SharpIDE.Application.Features.SolutionDiscovery;
namespace SharpIDE.Godot.Features.SolutionExplorer;
public partial class SharpIdeFileGodotContainer : RefCounted
{
public required SharpIdeFile File { get; init; }
}
// public partial class GodotContainer<T>(T value) : GodotObject where T : class
// {
// public T Value { get; init; } = value;
// }

View File

@@ -3,6 +3,7 @@ using Godot;
using SharpIDE.Application.Features.Analysis;
using SharpIDE.Application.Features.SolutionDiscovery;
using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence;
using SharpIDE.Godot.Features.Problems;
namespace SharpIDE.Godot.Features.SolutionExplorer;
@@ -32,9 +33,9 @@ public partial class SolutionExplorerPanel : MarginContainer
{
var selected = _tree.GetSelected();
if (selected is null) return;
var sharpIdeFileContainer = selected.GetMetadata(0).As<SharpIdeFileGodotContainer?>();
var sharpIdeFileContainer = selected.GetMetadata(0).As<RefCountedContainer<SharpIdeFile>?>();
if (sharpIdeFileContainer is null) return;
var sharpIdeFile = sharpIdeFileContainer.File;
var sharpIdeFile = sharpIdeFileContainer.Item;
Guard.Against.Null(sharpIdeFile, nameof(sharpIdeFile));
GodotGlobalEvents.Instance.InvokeFileSelected(sharpIdeFile);
}
@@ -60,7 +61,7 @@ public partial class SolutionExplorerPanel : MarginContainer
private static TreeItem? FindItemRecursive(TreeItem item, SharpIdeFile file)
{
var metadata = item.GetMetadata(0);
if (metadata.As<SharpIdeFileGodotContainer?>()?.File == file)
if (metadata.As<RefCountedContainer<SharpIdeFile>?>()?.Item == file)
return item;
var child = item.GetFirstChild();
@@ -160,7 +161,7 @@ public partial class SolutionExplorerPanel : MarginContainer
var fileItem = _tree.CreateItem(parent);
fileItem.SetText(0, file.Name);
fileItem.SetIcon(0, CsharpFileIcon);
var container = new SharpIdeFileGodotContainer { File = file };
var container = new RefCountedContainer<SharpIdeFile>(file);
fileItem.SetMetadata(0, container);
}
}