Browse Source

Added find namespace references & rename namespace function in Script.

pull/32/merge
Mike Krüger 13 years ago
parent
commit
a1db11e233
  1. 6
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/InconsistentNamingIssue.cs
  2. 13
      ICSharpCode.NRefactory.CSharp/Refactoring/Script.cs
  3. 66
      ICSharpCode.NRefactory.CSharp/Resolver/FindReferences.cs
  4. 58
      ICSharpCode.NRefactory.Tests/CSharp/Resolver/FindReferencesTest.cs

6
ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/InconsistentNamingIssue.cs

@ -153,12 +153,14 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -153,12 +153,14 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
}
} else if (resolveResult is LocalResolveResult) {
script.Rename(((LocalResolveResult)resolveResult).Variable, n);
} else if (resolveResult is NamespaceResolveResult) {
script.Rename(((NamespaceResolveResult)resolveResult).Namespace, n);
} else {
script.Replace(identifier, Identifier.Create(n));
}
}, identifier)));
if (entity != AffectedEntity.Namespace && entity != AffectedEntity.Label) {
if (entity != AffectedEntity.Label) {
actions.Add(new CodeAction(string.Format(ctx.TranslateString("Rename '{0}'..."), identifier.Name), (Script script) => {
if (resolveResult == null)
resolveResult = ctx.Resolve (node);
@ -173,6 +175,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -173,6 +175,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
}
} else if (resolveResult is LocalResolveResult) {
script.Rename(((LocalResolveResult)resolveResult).Variable);
} else if (resolveResult is NamespaceResolveResult) {
script.Rename(((NamespaceResolveResult)resolveResult).Namespace);
}
}, identifier));
}

13
ICSharpCode.NRefactory.CSharp/Refactoring/Script.cs

@ -383,7 +383,18 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -383,7 +383,18 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
public virtual void Rename(IVariable variable, string name = null)
{
}
/// <summary>
/// Renames the specified namespace.
/// </summary>
/// <param name="ns">The namespace</param>
/// <param name='name'>
/// The new name, if null the user is prompted for a new name.
/// </param>
public virtual void Rename(INamespace ns, string name = null)
{
}
public virtual void Dispose()
{
FormatText (nodesToFormat);

66
ICSharpCode.NRefactory.CSharp/Resolver/FindReferences.cs

@ -286,6 +286,14 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -286,6 +286,14 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
return new[] { scope };
}
}
public IList<IFindReferenceSearchScope> GetSearchScopes(INamespace ns)
{
if (ns == null)
throw new ArgumentNullException("ns");
return new[] { GetSearchScopeForNamespace(ns) };
}
#endregion
#region GetInterestingFileNames
@ -1312,5 +1320,63 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -1312,5 +1320,63 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
}
}
#endregion
#region Find Namespace References
SearchScope GetSearchScopeForNamespace(INamespace ns)
{
var scope = new SearchScope (
delegate (ICompilation compilation) {
return new FindNamespaceNavigator (ns);
}
);
return scope;
}
sealed class FindNamespaceNavigator : FindReferenceNavigator
{
readonly INamespace ns;
public FindNamespaceNavigator (INamespace ns)
{
this.ns = ns;
}
internal override bool CanMatch(AstNode node)
{
var nsd = node as NamespaceDeclaration;
if (nsd != null && nsd.FullName == ns.FullName)
return true;
var ud = node as UsingDeclaration;
if (ud != null && ud.Namespace == ns.FullName)
return true;
var st = node as SimpleType;
if (st != null && st.Identifier == ns.Name)
return true;
var mt = node as MemberType;
if (mt != null && mt.MemberName == ns.Name)
return true;
var identifer = node as IdentifierExpression;
if (identifer != null && identifer.Identifier == ns.Name)
return true;
var mrr = node as MemberReferenceExpression;
if (mrr != null && mrr.MemberName == ns.Name)
return true;
return false;
}
internal override bool IsMatch(ResolveResult rr)
{
var nsrr = rr as NamespaceResolveResult;
return nsrr != null && nsrr.NamespaceName == ns.FullName;
}
}
#endregion
}
}

58
ICSharpCode.NRefactory.Tests/CSharp/Resolver/FindReferencesTest.cs

@ -51,7 +51,16 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -51,7 +51,16 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
(node, rr) => result.Add(node), CancellationToken.None);
return result.OrderBy(n => n.StartLocation).ToArray();
}
AstNode[] FindReferences(INamespace ns)
{
var result = new List<AstNode>();
var searchScopes = findReferences.GetSearchScopes(ns);
findReferences.FindReferencesInFile(searchScopes, unresolvedFile, syntaxTree, compilation,
(node, rr) => result.Add(node), CancellationToken.None);
return result.OrderBy(n => n.StartLocation).ToArray();
}
#region Method Group
[Test]
public void FindMethodGroupReference()
@ -300,5 +309,52 @@ public class C { @@ -300,5 +309,52 @@ public class C {
#endif // NET_4_5
#endregion
#region Namespaces
[Test]
public void FindNamespaceTest()
{
Init(@"using System;
using Foo.Bar;
namespace Foo.Bar {
class MyTest { }
}
namespace Other.Bar {
class OtherTest {}
}
namespace Foo
{
class Test
{
static void T()
{
Bar.MyTest test;
Other.Bar.OtherTest test2;
}
}
}
namespace B
{
using f = Foo.Bar;
class Test2
{
Foo.Bar.MyTest a;
}
}
");
var test = compilation.MainAssembly.RootNamespace.GetChildNamespace("Foo").GetChildNamespace ("Bar");
var actual = FindReferences(test).ToList();
Assert.AreEqual(5, actual.Count);
Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 2 && r is MemberType));
Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 4 && r is NamespaceDeclaration));
Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 18 && r is SimpleType));
Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 26 && r is MemberType));
Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 29 && r is MemberType));
}
#endregion
}
}

Loading…
Cancel
Save