Browse Source

Type storage: Fixed handling of namespaces that have sub-namespaces but do not directly contain classes.

newNRvisualizers
Daniel Grunwald 15 years ago
parent
commit
1c51b3ab05
  1. 33
      ICSharpCode.NRefactory.Tests/CSharp/Resolver/NameLookupTests.cs
  2. 2
      ICSharpCode.NRefactory/CSharp/Resolver/ResolveVisitor.cs
  3. 72
      ICSharpCode.NRefactory/TypeSystem/Implementation/TypeStorage.cs

33
ICSharpCode.NRefactory.Tests/CSharp/Resolver/NameLookupTests.cs

@ -350,7 +350,7 @@ namespace Root.Child { @@ -350,7 +350,7 @@ namespace Root.Child {
Assert.AreEqual("Root.Alpha", result.Type.FullName);
}
[Test, Ignore("type references not implemented")]
[Test]
public void ImportAliasTest()
{
string program = @"using COL = System.Collections;
@ -377,7 +377,7 @@ class TestClass { @@ -377,7 +377,7 @@ class TestClass {
Assert.AreEqual("System.Collections.Generic", ns.NamespaceName, "COL.Generic");
}
[Test, Ignore("Cannot resolve type references")]
[Test]
public void ImportAliasClassResolveTest()
{
string program = @"using COL = System.Collections.ArrayList;
@ -394,6 +394,35 @@ class TestClass { @@ -394,6 +394,35 @@ class TestClass {
Assert.AreEqual("System.Collections.ArrayList", rr.Type.FullName, "a");
}
[Test]
public void ImportSubnamespaceWithAliasTest()
{
string program = @"namespace PC
{
// Define an alias for the nested namespace.
using Project = PC.MyCompany.Project;
class A
{
Project.MyClass M()
{
// Use the alias
$Project.MyClass$ mc = new Project.MyClass();
return mc;
}
}
namespace MyCompany
{
namespace Project
{
public class MyClass { }
}
}
}
";
var mrr = Resolve(program);
Assert.AreEqual("PC.MyCompany.Project.MyClass", mrr.Type.FullName);
}
[Test, Ignore("Parser position bug")]
public void ResolveNamespaceSD_863()
{

2
ICSharpCode.NRefactory/CSharp/Resolver/ResolveVisitor.cs

@ -1078,11 +1078,13 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -1078,11 +1078,13 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
#region Using Declaration
public override ResolveResult VisitUsingDeclaration(UsingDeclaration usingDeclaration, object data)
{
ScanChildren(usingDeclaration);
return null;
}
public override ResolveResult VisitUsingAliasDeclaration(UsingAliasDeclaration usingDeclaration, object data)
{
ScanChildren(usingDeclaration);
return null;
}
#endregion

72
ICSharpCode.NRefactory/TypeSystem/Implementation/TypeStorage.cs

@ -46,7 +46,7 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation @@ -46,7 +46,7 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
public bool Equals(FullNameAndTypeParameterCount x, FullNameAndTypeParameterCount y)
{
return x.TypeParameterCount == y.TypeParameterCount
return x.TypeParameterCount == y.TypeParameterCount
&& NameComparer.Equals(x.Name, y.Name)
&& NameComparer.Equals(x.Namespace, y.Namespace);
}
@ -110,11 +110,29 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation @@ -110,11 +110,29 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
#region Namespace Storage
class NamespaceEntry
{
/// <summary>
/// Full namespace name
/// </summary>
public readonly string Name;
/// <summary>
/// Parent namespace
/// </summary>
public readonly NamespaceEntry Parent;
/// <summary>
/// Number of classes in this namespace (not in sub-namespaces)
/// </summary>
public int ClassCount;
public NamespaceEntry(string name)
/// <summary>
/// Number of sub-namespaces.
/// </summary>
public int SubNamespaceCount;
public NamespaceEntry(NamespaceEntry parent, string name)
{
this.Parent = parent;
this.Name = name;
}
}
@ -145,9 +163,11 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation @@ -145,9 +163,11 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
}
// now create new dict
var newDict = _typeDicts[0].Values.GroupBy(c => c.Namespace, nameComparer)
.Select(g => new NamespaceEntry(g.Key) { ClassCount = g.Count() })
.ToDictionary(d => d.Name);
var newDict = new Dictionary<string, NamespaceEntry>(nameComparer);
foreach (ITypeDefinition type in _typeDicts[0].Values) {
NamespaceEntry ne = GetOrCreateNamespaceEntry(newDict, type.Namespace);
ne.ClassCount++;
}
// add the new dict to the array of dicts
var newNamespaceDicts = new Dictionary<string, NamespaceEntry>[namespaceDicts.Length + 1];
@ -157,6 +177,25 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation @@ -157,6 +177,25 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
return newDict;
}
}
NamespaceEntry GetOrCreateNamespaceEntry(Dictionary<string, NamespaceEntry> dict, string ns)
{
NamespaceEntry ne;
if (!dict.TryGetValue(ns, out ne)) {
NamespaceEntry parentEntry;
if (string.IsNullOrEmpty(ns)) {
parentEntry = null;
} else {
int pos = ns.LastIndexOf('.');
string parentNS = pos < 0 ? string.Empty : ns.Substring(0, pos);
parentEntry = GetOrCreateNamespaceEntry(dict, parentNS);
parentEntry.SubNamespaceCount++;
}
ne = new NamespaceEntry(parentEntry, ns);
dict.Add(ns, ne);
}
return ne;
}
#endregion
#region ITypeResolveContext implementation
@ -257,12 +296,23 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation @@ -257,12 +296,23 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
foreach (var dict in _namespaceDicts) {
NamespaceEntry ns;
if (dict.TryGetValue(typeDefinition.Namespace, out ns)) {
if (--ns.ClassCount == 0)
dict.Remove(typeDefinition.Namespace);
ns.ClassCount--;
RemoveNamespaceIfPossible(dict, ns);
}
}
}
}
void RemoveNamespaceIfPossible(Dictionary<string, NamespaceEntry> dict, NamespaceEntry ns)
{
while (ns.ClassCount == 0 && ns.SubNamespaceCount == 0) {
dict.Remove(ns.Name);
ns = ns.Parent;
if (ns == null)
break;
ns.SubNamespaceCount--;
}
}
#endregion
#region UpdateType
@ -281,12 +331,8 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation @@ -281,12 +331,8 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
}
if (isNew) {
foreach (var dict in _namespaceDicts) {
NamespaceEntry ns;
if (dict.TryGetValue(typeDefinition.Namespace, out ns)) {
++ns.ClassCount;
} else {
dict.Add(typeDefinition.Namespace, new NamespaceEntry(typeDefinition.Namespace) { ClassCount = 1 });
}
NamespaceEntry ns = GetOrCreateNamespaceEntry(dict, typeDefinition.Namespace);
++ns.ClassCount;
}
}
}

Loading…
Cancel
Save