mirror of https://github.com/icsharpcode/ILSpy.git
Browse Source
Adds the IMemberTreeNode interface (the same contract WPF uses) and makes the five entity-bearing tree-node types — Type / Method / Field / Property / Event — implement it so generic tooling can reach the underlying IEntity without knowing which concrete tree node it has on hand. Assisted-by: Claude:claude-opus-4-7:Claude Codepull/3755/head
8 changed files with 238 additions and 5 deletions
@ -0,0 +1,118 @@ |
|||||||
|
// Copyright (c) 2026 AlphaSierraPapa for the SharpDevelop Team
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
||||||
|
// software and associated documentation files (the "Software"), to deal in the Software
|
||||||
|
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||||
|
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||||
|
// to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all copies or
|
||||||
|
// substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||||
|
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||||
|
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||||
|
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||||
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
// DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
using System.Linq; |
||||||
|
using System.Threading.Tasks; |
||||||
|
|
||||||
|
using Avalonia.Headless.NUnit; |
||||||
|
|
||||||
|
using AwesomeAssertions; |
||||||
|
|
||||||
|
using ICSharpCode.ILSpy.Properties; |
||||||
|
using ICSharpCode.ILSpyX.TreeView; |
||||||
|
|
||||||
|
using ILSpy; |
||||||
|
using ILSpy.AppEnv; |
||||||
|
using ILSpy.Commands; |
||||||
|
using ILSpy.TreeNodes; |
||||||
|
using ILSpy.ViewModels; |
||||||
|
using ILSpy.Views; |
||||||
|
|
||||||
|
using NUnit.Framework; |
||||||
|
|
||||||
|
namespace ICSharpCode.ILSpy.Tests; |
||||||
|
|
||||||
|
[TestFixture] |
||||||
|
public class CopyFullyQualifiedNameTests |
||||||
|
{ |
||||||
|
[AvaloniaTest] |
||||||
|
public async Task Copy_Entry_Is_Registered() |
||||||
|
{ |
||||||
|
// "Copy fully qualified name" appears as an [ExportContextMenuEntry] tagged with the
|
||||||
|
// localized "CopyName" header. Verifies it's discovered through MEF.
|
||||||
|
|
||||||
|
// Arrange + Act — boot.
|
||||||
|
var window = AppComposition.Current.GetExport<MainWindow>(); |
||||||
|
window.Show(); |
||||||
|
var registry = AppComposition.Current.GetExport<ContextMenuEntryRegistry>(); |
||||||
|
|
||||||
|
// Assert — registry contains an entry with the CopyName resource header.
|
||||||
|
registry.Entries.Should().Contain( |
||||||
|
e => e.Metadata.Header == nameof(Resources.CopyName)); |
||||||
|
} |
||||||
|
|
||||||
|
[AvaloniaTest] |
||||||
|
public async Task Copy_Entry_Is_Visible_Only_For_Single_Member_Tree_Node() |
||||||
|
{ |
||||||
|
// IsVisible: exactly one selected node, and it must be an IMemberTreeNode (the
|
||||||
|
// interface implemented by Type/Method/Field/Property/Event tree nodes that exposes
|
||||||
|
// an IEntity). Hidden for assembly nodes, multi-selections, and empty selections.
|
||||||
|
|
||||||
|
// Arrange — boot, locate the registered entry + a sample type and assembly node.
|
||||||
|
var window = AppComposition.Current.GetExport<MainWindow>(); |
||||||
|
window.Show(); |
||||||
|
var vm = (MainWindowViewModel)window.DataContext!; |
||||||
|
await vm.AssemblyTreeModel.WaitForAssembliesAsync(minimumCount: 3); |
||||||
|
var registry = AppComposition.Current.GetExport<ContextMenuEntryRegistry>(); |
||||||
|
var entry = registry.Entries |
||||||
|
.Single(e => e.Metadata.Header == nameof(Resources.CopyName)) |
||||||
|
.Value; |
||||||
|
|
||||||
|
var typeNode = vm.AssemblyTreeModel.FindNode<TypeTreeNode>( |
||||||
|
"System.Linq", "System.Linq", "System.Linq.Enumerable"); |
||||||
|
var assemblyNode = vm.AssemblyTreeModel.FindNode<AssemblyTreeNode>("System.Linq"); |
||||||
|
|
||||||
|
// Assert — visible only for a single member-tree-node selection.
|
||||||
|
entry.IsVisible(new TextViewContext { SelectedTreeNodes = new[] { (SharpTreeNode)typeNode } }) |
||||||
|
.Should().BeTrue(); |
||||||
|
entry.IsVisible(new TextViewContext { SelectedTreeNodes = new SharpTreeNode[] { typeNode, assemblyNode } }) |
||||||
|
.Should().BeFalse("multi-selection isn't supported"); |
||||||
|
entry.IsVisible(new TextViewContext { SelectedTreeNodes = new[] { (SharpTreeNode)assemblyNode } }) |
||||||
|
.Should().BeFalse("assembly nodes are not IMemberTreeNodes"); |
||||||
|
entry.IsVisible(new TextViewContext { SelectedTreeNodes = null }) |
||||||
|
.Should().BeFalse(); |
||||||
|
} |
||||||
|
|
||||||
|
[AvaloniaTest] |
||||||
|
public async Task Copy_Entry_Computes_The_Reflection_Name_Of_The_Selected_Member() |
||||||
|
{ |
||||||
|
// CopyName's payload is the IEntity.ReflectionName of the selected member — the same
|
||||||
|
// stable language-independent identifier used by FindNodeByPath et al. Tested by
|
||||||
|
// reading the public computed property; the actual clipboard write is a thin wrapper
|
||||||
|
// around that text.
|
||||||
|
|
||||||
|
// Arrange — boot, locate the type node + the registered entry's typed wrapper.
|
||||||
|
var window = AppComposition.Current.GetExport<MainWindow>(); |
||||||
|
window.Show(); |
||||||
|
var vm = (MainWindowViewModel)window.DataContext!; |
||||||
|
await vm.AssemblyTreeModel.WaitForAssembliesAsync(minimumCount: 3); |
||||||
|
var registry = AppComposition.Current.GetExport<ContextMenuEntryRegistry>(); |
||||||
|
var entry = (CopyFullyQualifiedNameContextMenuEntry)registry.Entries |
||||||
|
.Single(e => e.Metadata.Header == nameof(Resources.CopyName)) |
||||||
|
.Value; |
||||||
|
|
||||||
|
var typeNode = vm.AssemblyTreeModel.FindNode<TypeTreeNode>( |
||||||
|
"System.Linq", "System.Linq", "System.Linq.Enumerable"); |
||||||
|
|
||||||
|
// Act — compute the would-be-copied text for that selection.
|
||||||
|
var text = entry.GetTextToCopy(new TextViewContext { SelectedTreeNodes = new[] { (SharpTreeNode)typeNode } }); |
||||||
|
|
||||||
|
// Assert — matches the expected ReflectionName.
|
||||||
|
text.Should().Be("System.Linq.Enumerable"); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,69 @@ |
|||||||
|
// Copyright (c) 2026 AlphaSierraPapa for the SharpDevelop Team
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
||||||
|
// software and associated documentation files (the "Software"), to deal in the Software
|
||||||
|
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||||
|
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||||
|
// to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all copies or
|
||||||
|
// substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||||
|
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||||
|
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||||
|
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||||
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
// DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
using System.Composition; |
||||||
|
|
||||||
|
using Avalonia; |
||||||
|
using Avalonia.Controls; |
||||||
|
using Avalonia.Input.Platform; |
||||||
|
|
||||||
|
using ICSharpCode.ILSpy.Properties; |
||||||
|
|
||||||
|
using ILSpy.TreeNodes; |
||||||
|
|
||||||
|
namespace ILSpy.Commands |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Right-click → "Copy fully qualified name". Visible whenever the selection is exactly
|
||||||
|
/// one tree node that wraps a TypeSystem entity (Type/Method/Field/Property/Event).
|
||||||
|
/// Copies the entity's <see cref="ICSharpCode.Decompiler.TypeSystem.IEntity.ReflectionName"/>
|
||||||
|
/// — the language-independent identifier used by FindNodeByPath etc. — to the clipboard.
|
||||||
|
/// </summary>
|
||||||
|
[ExportContextMenuEntry(Header = nameof(Resources.CopyName), Order = 9999)] |
||||||
|
[Shared] |
||||||
|
public sealed class CopyFullyQualifiedNameContextMenuEntry : IContextMenuEntry |
||||||
|
{ |
||||||
|
public bool IsVisible(TextViewContext context) => GetMemberNodeFromContext(context) != null; |
||||||
|
|
||||||
|
public bool IsEnabled(TextViewContext context) => true; |
||||||
|
|
||||||
|
public void Execute(TextViewContext context) |
||||||
|
{ |
||||||
|
var text = GetTextToCopy(context); |
||||||
|
if (text == null) |
||||||
|
return; |
||||||
|
// The clipboard is owned by the TopLevel that hosts the source visual; for the
|
||||||
|
// assembly tree that's the main window. SetTextAsync runs on the UI thread and we
|
||||||
|
// don't await — the menu item already returned by then.
|
||||||
|
var topLevel = context.OriginalSource is Visual src ? TopLevel.GetTopLevel(src) |
||||||
|
: context.TreeGrid is { } grid ? TopLevel.GetTopLevel(grid) |
||||||
|
: null; |
||||||
|
topLevel?.Clipboard?.SetTextAsync(text); |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary>Public for testing: returns the text the entry would write to the clipboard
|
||||||
|
/// for the given context, or <see langword="null"/> when the entry isn't applicable.</summary>
|
||||||
|
public string? GetTextToCopy(TextViewContext context) |
||||||
|
=> GetMemberNodeFromContext(context)?.Member?.ReflectionName; |
||||||
|
|
||||||
|
static IMemberTreeNode? GetMemberNodeFromContext(TextViewContext context) |
||||||
|
=> context.SelectedTreeNodes is { Length: 1 } nodes |
||||||
|
? nodes[0] as IMemberTreeNode |
||||||
|
: null; |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,34 @@ |
|||||||
|
// Copyright (c) 2026 AlphaSierraPapa for the SharpDevelop Team
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
||||||
|
// software and associated documentation files (the "Software"), to deal in the Software
|
||||||
|
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||||
|
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||||
|
// to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all copies or
|
||||||
|
// substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||||
|
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||||
|
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||||
|
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||||
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
// DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
using ICSharpCode.Decompiler.TypeSystem; |
||||||
|
|
||||||
|
namespace ILSpy.TreeNodes |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Contract implemented by every tree node that wraps a TypeSystem entity (types,
|
||||||
|
/// methods, fields, properties, events). Lets context-menu entries and other generic
|
||||||
|
/// tooling that wants the underlying <see cref="IEntity"/> reach it without knowing
|
||||||
|
/// which concrete tree-node class it has on hand.
|
||||||
|
/// </summary>
|
||||||
|
public interface IMemberTreeNode |
||||||
|
{ |
||||||
|
/// <summary>The underlying entity, or null when it cannot be resolved.</summary>
|
||||||
|
IEntity? Member { get; } |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue