open file from problem

This commit is contained in:
Matt Parker
2025-09-13 15:35:52 +10:00
parent 3fb602d8fc
commit b4291cb7f7
6 changed files with 51 additions and 7 deletions

View File

@@ -0,0 +1,15 @@
using Godot;
using Microsoft.CodeAnalysis;
using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence;
namespace SharpIDE.Godot.Features.Problems;
public partial class DiagnosticMetadataContainer(Diagnostic diagnostic) : GodotObject
{
public Diagnostic Diagnostic { get; } = diagnostic;
}
public partial class ProjectContainer(SharpIdeProjectModel project) : GodotObject
{
public SharpIdeProjectModel Project { get; } = project;
}

View File

@@ -0,0 +1 @@
uid://bpo4ti23q3bhu

View File

@@ -3,6 +3,7 @@ using Godot;
using Microsoft.CodeAnalysis;
using ObservableCollections;
using R3;
using SharpIDE.Application.Features.SolutionDiscovery;
using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence;
namespace SharpIDE.Godot.Features.Problems;
@@ -26,6 +27,7 @@ public partial class ProblemsPanel : Control
public override void _Ready()
{
_tree = GetNode<Tree>("%Tree");
_tree.ItemActivated += TreeOnItemActivated;
_rootItem = _tree.CreateItem();
_rootItem.SetText(0, "Problems");
Observable.EveryValueChanged(this, manager => manager.Solution)
@@ -62,6 +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));
e.NewItem.View.Value = treeItem;
Observable.EveryValueChanged(e.NewItem.Value, s => s.Diagnostics.Count).Subscribe(s => treeItem.Visible = s is not 0).AddTo(this);
@@ -83,6 +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.SetIcon(0, e.NewItem.Value.Severity switch
{
DiagnosticSeverity.Error => ErrorIcon,
@@ -100,4 +104,26 @@ public partial class ProblemsPanel : Control
treeItem?.Free();
});
}
private void TreeOnItemActivated()
{
var selected = _tree.GetSelected();
var diagnosticContainer = selected.GetMetadata(0).As<DiagnosticMetadataContainer?>();
if (diagnosticContainer is null) return;
var diagnostic = diagnosticContainer.Diagnostic;
var parentTreeItem = selected.GetParent();
var projectContainer = parentTreeItem.GetMetadata(0).As<ProjectContainer?>();
if (projectContainer is null) return;
var projectModel = projectContainer.Project;
OpenDocumentContainingDiagnostic(diagnostic, projectModel);
}
private static void OpenDocumentContainingDiagnostic(Diagnostic diagnostic, SharpIdeProjectModel projectModel)
{
// TODO: probably store a flat list of all files in each project to avoid recursion
var file = projectModel.Files
.Concat(projectModel.Folders.SelectMany(f => f.GetAllFiles()))
.Single(s => s.Path == diagnostic.Location.SourceTree?.GetMappedLineSpan(diagnostic.Location.SourceSpan).Path);
GodotGlobalEvents.InvokeFileSelected(file);
}
}