Skip to content
Permalink
Browse files

Add namespace search strategy

  • Loading branch information...
miloush committed Oct 1, 2019
1 parent 0e84a03 commit 44acb103bc9d568bcfb09e3fbde9551f9af1cd01
@@ -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" />
@@ -722,6 +722,8 @@ public ILSpyTreeNode FindTreeNode(object reference)
return assemblyListTreeNode.FindPropertyNode(pd);
case IEvent ed:
return assemblyListTreeNode.FindEventNode(ed);
case INamespace nd:
return AssemblyListTreeNode.FindNamespaceNode(nd);
default:
return null;
}
@@ -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);
}
}
}
@@ -77,6 +77,7 @@ private SearchPane()
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 @@ AbstractSearchStrategy GetSearchStrategy()

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 @@ AbstractSearchStrategy GetSearchStrategy()
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 @@ public enum SearchMode
Literal,
Token,
Resource,
Assembly
Assembly,
Namespace
}
}
@@ -103,4 +103,13 @@ public class AssemblySearchResult : SearchResult
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;
}
}
@@ -318,6 +318,24 @@ public EventTreeNode FindEventNode(IEvent def)
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
}
}

0 comments on commit 44acb10

Please sign in to comment.
You can’t perform that action at this time.