Browse Source

Implemented basic rename feature.

pull/45/merge
Mike Krüger 12 years ago
parent
commit
444eb11153
  1. 70
      ICSharpCode.NRefactory.CSharp/Resolver/FindReferences.cs
  2. 41
      ICSharpCode.NRefactory.Tests/Analysis/SymbolCollectorTests.cs
  3. 97
      ICSharpCode.NRefactory.Tests/CSharp/Resolver/FindReferencesTest.cs
  4. 4
      ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj
  5. 6
      ICSharpCode.NRefactory/Analysis/SymbolCollector.cs
  6. 3
      ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj

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

@ -473,10 +473,76 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -473,10 +473,76 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
#endregion
#region RenameReferencesInFile
AstNode GetNodeToReplace(AstNode node)
{
if (node is ConstructorInitializer)
return null;
if (node is ObjectCreateExpression)
node = ((ObjectCreateExpression)node).Type;
if (node is InvocationExpression)
node = ((InvocationExpression)node).Target;
if (node is MemberReferenceExpression)
node = ((MemberReferenceExpression)node).MemberNameToken;
if (node is SimpleType)
node = ((SimpleType)node).IdentifierToken;
if (node is MemberType)
node = ((MemberType)node).MemberNameToken;
if (node is NamespaceDeclaration) {
// var nsd = ((NamespaceDeclaration)node);
// node = nsd.Identifiers.LastOrDefault (n => n.Name == memberName) ?? nsd.Identifiers.FirstOrDefault ();
// if (node == null)
return null;
}
if (node is TypeDeclaration)
node = ((TypeDeclaration)node).NameToken;
if (node is DelegateDeclaration)
node = ((DelegateDeclaration)node).NameToken;
if (node is EntityDeclaration)
node = ((EntityDeclaration)node).NameToken;
if (node is ParameterDeclaration)
node = ((ParameterDeclaration)node).NameToken;
if (node is ConstructorDeclaration)
node = ((ConstructorDeclaration)node).NameToken;
if (node is DestructorDeclaration)
node = ((DestructorDeclaration)node).NameToken;
if (node is NamedArgumentExpression)
node = ((NamedArgumentExpression)node).NameToken;
if (node is NamedExpression)
node = ((NamedExpression)node).NameToken;
if (node is VariableInitializer)
node = ((VariableInitializer)node).NameToken;
if (node is IdentifierExpression) {
node = ((IdentifierExpression)node).IdentifierToken;
}
return node;
}
public void RenameReferencesInFile(IList<IFindReferenceSearchScope> searchScopes, string newName, CSharpAstResolver resolver,
Action<RenameCallbackArguments> callback, Action<Error> errorCallback, CancellationToken cancellationToken)
Action<RenameCallbackArguments> callback, Action<Error> errorCallback, CancellationToken cancellationToken = default (CancellationToken))
{
throw new NotImplementedException();
WholeVirtualSlot = true;
FindReferencesInFile(
searchScopes,
resolver,
delegate(AstNode astNode, ResolveResult result) {
var nodeToReplace = GetNodeToReplace(astNode);
if (nodeToReplace == null) {
errorCallback (new Error (ErrorType.Error, "no node to replace found."));
return;
}
callback (new RenameCallbackArguments(nodeToReplace, Identifier.Create(newName)));
},
cancellationToken);
}
#endregion

41
ICSharpCode.NRefactory.Tests/Analysis/SymbolCollectorTests.cs

@ -0,0 +1,41 @@ @@ -0,0 +1,41 @@
//
// SymbolCollectorTests.cs
//
// Author:
// Mike Krüger <mkrueger@xamarin.com>
//
// Copyright (c) 2013 Xamarin Inc. (http://xamarin.com)
//
// 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;
using System.Linq;
using ICSharpCode.NRefactory.CSharp;
using ICSharpCode.NRefactory.TypeSystem;
using NUnit.Framework;
using ICSharpCode.NRefactory.CSharp.CodeCompletion;
namespace ICSharpCode.NRefactory.Analysis
{
[TestFixture]
public class SymbolCollectorTests
{
}
}

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

@ -24,6 +24,9 @@ using System.Threading; @@ -24,6 +24,9 @@ using System.Threading;
using ICSharpCode.NRefactory.CSharp.TypeSystem;
using ICSharpCode.NRefactory.TypeSystem;
using NUnit.Framework;
using ICSharpCode.NRefactory.Analysis;
using System.Text;
using ICSharpCode.NRefactory.Editor;
namespace ICSharpCode.NRefactory.CSharp.Resolver
{
@ -383,5 +386,99 @@ namespace Foo @@ -383,5 +386,99 @@ namespace Foo
Assert.IsTrue(actual.Any(r => r.StartLocation.Line == 12 && r is SimpleType));
}
#endregion
#region Rename
ISymbol GetSymbol (string reflectionName)
{
Stack<ITypeDefinition> typeStack = new Stack<ITypeDefinition>(compilation.MainAssembly.TopLevelTypeDefinitions);
while (typeStack.Count > 0) {
var cur = typeStack.Pop();
if (cur.ReflectionName == reflectionName)
return cur;
foreach (var member in cur.Members)
if (member.ReflectionName == reflectionName)
return member;
foreach (var nested in cur.NestedTypes) {
typeStack.Push(nested);
}
}
return null;
}
IList<AstNode> Rename(string fullyQualifiedName, string newName, bool includeOverloads)
{
var sym = GetSymbol(fullyQualifiedName);
Assert.NotNull(sym);
var graph = new TypeGraph(compilation.Assemblies);
var scopes = findReferences.GetSearchScopes(SymbolCollector.GetRelatedSymbols(graph, sym, includeOverloads));
List<AstNode> result = new List<AstNode>();
findReferences.RenameReferencesInFile(
scopes,
newName,
new CSharpAstResolver(compilation, syntaxTree, unresolvedFile),
delegate(RenameCallbackArguments obj) {
result.Add (obj.NodeToReplace);
},
delegate(Error obj) {
});
return result;
}
void TestRename(string code, string symbolName)
{
StringBuilder sb = new StringBuilder();
List<int> offsets = new List<int>();
foreach (var ch in code) {
if (ch == '$') {
offsets.Add(sb.Length);
continue;
}
sb.Append(ch);
}
Init(sb.ToString ());
var doc = new ReadOnlyDocument(sb.ToString ());
var result = Rename(symbolName, "x", false);
Assert.AreEqual(offsets.Count, result.Count);
result.Select(r => doc.GetOffset (r.StartLocation)).SequenceEqual(offsets);
}
[Test]
public void TestSimpleRename ()
{
TestRename (@"using System;
class $Test {
$Test test;
}", "Test");
}
[Test]
public void TestOverride ()
{
TestRename(@"using System;
class Test {
public virtual int $Foo { get; set; }
}
class Test2 : Test {
public override int $Foo { get; set; }
}
class Test3 : Test {
public override int $Foo { get; set; }
public FindReferencesTest ()
{
$Foo = 4;
}
}
", "Test.Foo");
}
#endregion
}
}

4
ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj

@ -417,6 +417,7 @@ @@ -417,6 +417,7 @@
<Compile Include="CSharp\CodeIssues\DuplicateIfInIfChainIssueTests.cs" />
<Compile Include="CSharp\CodeIssues\UseBlockInsteadColonIssueTests.cs" />
<Compile Include="CSharp\AstTests.cs" />
<Compile Include="Analysis\SymbolCollectorTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\cecil\Mono.Cecil.csproj">
@ -439,9 +440,6 @@ @@ -439,9 +440,6 @@
<Private>False</Private>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Analysis" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ProjectExtensions>
<MonoDevelop>

6
ICSharpCode.NRefactory/Analysis/MemberCollector.cs → ICSharpCode.NRefactory/Analysis/SymbolCollector.cs

@ -22,11 +22,11 @@ using ICSharpCode.NRefactory.TypeSystem; @@ -22,11 +22,11 @@ using ICSharpCode.NRefactory.TypeSystem;
namespace ICSharpCode.NRefactory.Analysis
{
public class MemberCollector
public class SymbolCollector
{
public static IEnumerable<IMember> GetRelatedMembers(TypeGraph g, IMember m, bool includeOverloads)
public static IEnumerable<ISymbol> GetRelatedSymbols(TypeGraph g, ISymbol m, bool includeOverloads)
{
throw new NotImplementedException();
yield return m;
}
}
}

3
ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj

@ -83,7 +83,6 @@ @@ -83,7 +83,6 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Analysis\AbiComparer.cs" />
<Compile Include="Analysis\MemberCollector.cs" />
<Compile Include="Analysis\TypeGraph.cs" />
<Compile Include="Analysis\TypeGraphNode.cs" />
<Compile Include="Documentation\DocumentationComment.cs" />
@ -279,10 +278,10 @@ @@ -279,10 +278,10 @@
<Compile Include="Completion\CompletionExtensionMethods.cs" />
<Compile Include="Refactoring\IssueMarker.cs" />
<Compile Include="Refactoring\Severity.cs" />
<Compile Include="Analysis\SymbolCollector.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Completion\" />
<Folder Include="Analysis" />
<Folder Include="TypeSystem\" />
<Folder Include="Utils\CompositeFormatStringParser\" />
<Folder Include="Refactoring\" />

Loading…
Cancel
Save