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