Browse Source

Add namespace search strategy

pull/1736/head
Jan Kučera 6 years ago
parent
commit
44acb103bc
  1. 1
      ILSpy/ILSpy.csproj
  2. 2
      ILSpy/MainWindow.xaml.cs
  3. 63
      ILSpy/Search/NamespaceSearchStrategy.cs
  4. 9
      ILSpy/Search/SearchPane.cs
  5. 9
      ILSpy/Search/SearchResult.cs
  6. 18
      ILSpy/TreeNodes/AssemblyListTreeNode.cs

1
ILSpy/ILSpy.csproj

@ -173,6 +173,7 @@ @@ -173,6 +173,7 @@
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Search\AbstractEntitySearchStrategy.cs" />
<Compile Include="Search\NamespaceSearchStrategy.cs" />
<Compile Include="Search\AssemblySearchStrategy.cs" />
<Compile Include="Search\LiteralSearchStrategy.cs" />
<Compile Include="LoadedAssembly.cs" />

2
ILSpy/MainWindow.xaml.cs

@ -722,6 +722,8 @@ namespace ICSharpCode.ILSpy @@ -722,6 +722,8 @@ namespace ICSharpCode.ILSpy
return assemblyListTreeNode.FindPropertyNode(pd);
case IEvent ed:
return assemblyListTreeNode.FindEventNode(ed);
case INamespace nd:
return AssemblyListTreeNode.FindNamespaceNode(nd);
default:
return null;
}

63
ILSpy/Search/NamespaceSearchStrategy.cs

@ -0,0 +1,63 @@ @@ -0,0 +1,63 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata;
using System.Threading;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.Decompiler.TypeSystem;
using ICSharpCode.Decompiler.Util;
namespace ICSharpCode.ILSpy.Search
{
class NamespaceSearchStrategy : AbstractSearchStrategy
{
public NamespaceSearchStrategy(string term, IProducerConsumerCollection<SearchResult> resultQueue)
: this(resultQueue, new[] { term })
{
}
public NamespaceSearchStrategy(IProducerConsumerCollection<SearchResult> resultQueue, string[] terms)
: base(resultQueue, terms)
{
}
public override void Search(PEFile module, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var typeSystem = module.GetTypeSystemOrNull();
if (typeSystem == null) return;
var root = ((MetadataModule)typeSystem.MainModule).RootNamespace;
Search(module, root);
}
private void Search(PEFile module, INamespace ns)
{
if (ns.Types.Any()) {
if (IsMatch(ns.FullName.Length == 0 ? "-" : ns.FullName))
OnFoundResult(module, ns);
}
foreach (var child in ns.ChildNamespaces)
Search(module, child);
}
void OnFoundResult(PEFile module, INamespace ns)
{
var name = ns.FullName.Length == 0 ? "-" : ns.FullName;
var result = new NamespaceSearchResult {
Namespace = ns,
Name = name,
Fitness = 1.0f / name.Length,
Location = module.Name,
Assembly = module.FullName,
};
OnFoundResult(result);
}
}
}

9
ILSpy/Search/SearchPane.cs

@ -77,6 +77,7 @@ namespace ICSharpCode.ILSpy @@ -77,6 +77,7 @@ namespace ICSharpCode.ILSpy
searchModeComboBox.Items.Add(new { Image = Images.Library, Name = "Metadata Token" });
searchModeComboBox.Items.Add(new { Image = Images.Resource, Name = "Resource" });
searchModeComboBox.Items.Add(new { Image = Images.Assembly, Name = "Assembly" });
searchModeComboBox.Items.Add(new { Image = Images.Namespace, Name = "Namespace" });
ContextMenuProvider.Add(listBox);
MainWindow.Instance.CurrentAssemblyListChanged += MainWindow_Instance_CurrentAssemblyListChanged;
@ -355,6 +356,9 @@ namespace ICSharpCode.ILSpy @@ -355,6 +356,9 @@ namespace ICSharpCode.ILSpy
if (searchTerm[0].StartsWith("an:", StringComparison.Ordinal))
return new AssemblySearchStrategy(searchTerm[0].Substring(3), resultQueue, AssemblySearchKind.FullName);
if (searchTerm[0].StartsWith("n:", StringComparison.Ordinal))
return new NamespaceSearchStrategy(searchTerm[0].Substring(2), resultQueue);
}
switch (searchMode)
@ -381,6 +385,8 @@ namespace ICSharpCode.ILSpy @@ -381,6 +385,8 @@ namespace ICSharpCode.ILSpy
return new ResourceSearchStrategy(apiVisibility, resultQueue, searchTerm);
case SearchMode.Assembly:
return new AssemblySearchStrategy(resultQueue, searchTerm, AssemblySearchKind.NameOrFileName);
case SearchMode.Namespace:
return new NamespaceSearchStrategy(resultQueue, searchTerm);
}
return null;
@ -413,6 +419,7 @@ namespace ICSharpCode.ILSpy @@ -413,6 +419,7 @@ namespace ICSharpCode.ILSpy
Literal,
Token,
Resource,
Assembly
Assembly,
Namespace
}
}

9
ILSpy/Search/SearchResult.cs

@ -103,4 +103,13 @@ namespace ICSharpCode.ILSpy @@ -103,4 +103,13 @@ namespace ICSharpCode.ILSpy
public override ImageSource Image => Images.Assembly;
public override ImageSource LocationImage => Images.Library;
}
public class NamespaceSearchResult : SearchResult
{
public INamespace Namespace { get; set; }
public override object Reference => Namespace;
public override ImageSource Image => Images.Namespace;
public override ImageSource LocationImage => Images.Assembly;
}
}

18
ILSpy/TreeNodes/AssemblyListTreeNode.cs

@ -318,6 +318,24 @@ namespace ICSharpCode.ILSpy.TreeNodes @@ -318,6 +318,24 @@ namespace ICSharpCode.ILSpy.TreeNodes
typeNode.EnsureLazyChildren();
return typeNode.Children.OfType<EventTreeNode>().FirstOrDefault(m => m.EventDefinition.MetadataToken == def.MetadataToken && !m.IsHidden);
}
/// <summary>
/// Looks up the event node corresponding to the namespace definition.
/// Returns null if no matching node is found.
/// </summary>
public NamespaceTreeNode FindNamespaceNode(INamespace def)
{
var module = def.ContributingModules.FirstOrDefault();
if (module == null)
return null;
AssemblyTreeNode assemblyNode = FindAssemblyNode(module);
if (assemblyNode == null)
return null;
assemblyNode.EnsureLazyChildren();
return assemblyNode.Children.OfType<NamespaceTreeNode>().FirstOrDefault(n => def.FullName.Length == 0 || def.FullName.Equals(n.Text));
}
#endregion
}
}

Loading…
Cancel
Save