Browse Source

Use ISymbol in Script.Rename()

pull/45/merge
Daniel Grunwald 12 years ago
parent
commit
bbcde1ea2e
  1. 12
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/InconsistentNamingIssue.cs
  2. 8
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/TypeParameterNotUsedIssue.cs
  3. 45
      ICSharpCode.NRefactory.CSharp/Refactoring/Script.cs
  4. 3
      ICSharpCode.NRefactory.CSharp/Resolver/FindReferences.cs
  5. 20
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/TestRefactoringContext.cs
  6. 1
      ICSharpCode.NRefactory.Tests/CSharp/Resolver/FindReferencesTest.cs

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

@ -147,11 +147,11 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
if (resolveResult is MemberResolveResult) { if (resolveResult is MemberResolveResult) {
script.Rename(((MemberResolveResult)resolveResult).Member, n); script.Rename(((MemberResolveResult)resolveResult).Member, n);
} else if (resolveResult is TypeResolveResult) { } else if (resolveResult is TypeResolveResult) {
var def = ((TypeResolveResult)resolveResult).Type.GetDefinition(); var def = resolveResult.Type.GetDefinition();
if (def != null) { if (def != null) {
script.Rename(def, n); script.Rename(def, n);
} else { } else if (resolveResult.Type.Kind == TypeKind.TypeParameter) {
script.RenameTypeParameter(((TypeResolveResult)resolveResult).Type, n); script.Rename((ITypeParameter)resolveResult.Type, n);
} }
} else if (resolveResult is LocalResolveResult) { } else if (resolveResult is LocalResolveResult) {
script.Rename(((LocalResolveResult)resolveResult).Variable, n); script.Rename(((LocalResolveResult)resolveResult).Variable, n);
@ -169,11 +169,11 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
if (resolveResult is MemberResolveResult) { if (resolveResult is MemberResolveResult) {
script.Rename(((MemberResolveResult)resolveResult).Member); script.Rename(((MemberResolveResult)resolveResult).Member);
} else if (resolveResult is TypeResolveResult) { } else if (resolveResult is TypeResolveResult) {
var def = ((TypeResolveResult)resolveResult).Type.GetDefinition(); var def = resolveResult.Type.GetDefinition();
if (def != null) { if (def != null) {
script.Rename(def); script.Rename(def);
} else { } else if (resolveResult.Type.Kind == TypeKind.TypeParameter) {
script.RenameTypeParameter(((TypeResolveResult)resolveResult).Type); script.Rename((ITypeParameter)resolveResult.Type);
} }
} else if (resolveResult is LocalResolveResult) { } else if (resolveResult is LocalResolveResult) {
script.Rename(((LocalResolveResult)resolveResult).Variable); script.Rename(((LocalResolveResult)resolveResult).Variable);

8
ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/TypeParameterNotUsedIssue.cs

@ -52,13 +52,15 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
} }
protected static bool FindUsage (BaseRefactoringContext context, SyntaxTree unit, protected static bool FindUsage (BaseRefactoringContext context, SyntaxTree unit,
ITypeParameter typaParameter, AstNode declaration) ITypeParameter typeParameter, AstNode declaration)
{ {
var found = false; var found = false;
refFinder.FindTypeParameterReferences (typaParameter, context.UnresolvedFile, unit, context.Compilation, var searchScopes = refFinder.GetSearchScopes(typeParameter);
refFinder.FindReferencesInFile(searchScopes, context.Resolver,
(node, resolveResult) => (node, resolveResult) =>
{ {
found = found || node != declaration; if (node != declaration)
found = true;
}, context.CancellationToken); }, context.CancellationToken);
return found; return found;
} }

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

@ -346,55 +346,18 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
} }
/// <summary> /// <summary>
/// Renames the specified entity. /// Renames the specified symbol.
/// </summary> /// </summary>
/// <param name='entity'> /// <param name='symbol'>
/// The Entity to rename /// The symbol to rename
/// </param> /// </param>
/// <param name='name'> /// <param name='name'>
/// The new name, if null the user is prompted for a new name. /// The new name, if null the user is prompted for a new name.
/// </param> /// </param>
public virtual void Rename(IEntity entity, string name = null) public virtual void Rename(ISymbol symbol, string name = null)
{ {
} }
/// <summary>
/// Renames the specified entity.
/// </summary>
/// <param name='type'>
/// The Entity to rename
/// </param>
/// <param name='name'>
/// The new name, if null the user is prompted for a new name.
/// </param>
public virtual void RenameTypeParameter (IType type, string name = null)
{
}
/// <summary>
/// Renames the specified variable.
/// </summary>
/// <param name='variable'>
/// The Variable to rename
/// </param>
/// <param name='name'>
/// The new name, if null the user is prompted for a new name.
/// </param>
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 DoGlobalOperationOn(IEntity entity, Action<RefactoringContext, Script, AstNode> callback, string operationDescripton = null) public virtual void DoGlobalOperationOn(IEntity entity, Action<RefactoringContext, Script, AstNode> callback, string operationDescripton = null)
{ {
} }

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

@ -396,7 +396,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
/// <summary> /// <summary>
/// Finds all references in the given file. /// Finds all references in the given file.
/// </summary> /// </summary>
/// <param name="searchScope">The search scopes for which to look.</param> /// <param name="searchScopes">The search scopes for which to look.</param>
/// <param name="resolver">AST resolver for the file to search in.</param> /// <param name="resolver">AST resolver for the file to search in.</param>
/// <param name="callback">Callback used to report the references that were found.</param> /// <param name="callback">Callback used to report the references that were found.</param>
/// <param name="cancellationToken">CancellationToken that may be used to cancel the operation.</param> /// <param name="cancellationToken">CancellationToken that may be used to cancel the operation.</param>
@ -530,7 +530,6 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
public void RenameReferencesInFile(IList<IFindReferenceSearchScope> searchScopes, string newName, CSharpAstResolver resolver, public void RenameReferencesInFile(IList<IFindReferenceSearchScope> searchScopes, string newName, CSharpAstResolver resolver,
Action<RenameCallbackArguments> callback, Action<Error> errorCallback, CancellationToken cancellationToken = default (CancellationToken)) Action<RenameCallbackArguments> callback, Action<Error> errorCallback, CancellationToken cancellationToken = default (CancellationToken))
{ {
WholeVirtualSlot = true;
FindReferencesInFile( FindReferencesInFile(
searchScopes, searchScopes,
resolver, resolver,

20
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/TestRefactoringContext.cs

@ -165,17 +165,21 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
Replace (node, new IdentifierExpression (newName)); Replace (node, new IdentifierExpression (newName));
} }
public override void Rename (IEntity entity, string name) public override void Rename (ISymbol symbol, string name)
{ {
if (symbol.SymbolKind == SymbolKind.Variable || symbol.SymbolKind == SymbolKind.Parameter) {
Rename(symbol as IVariable, name);
return;
}
FindReferences refFinder = new FindReferences (); FindReferences refFinder = new FindReferences ();
refFinder.FindReferencesInFile (refFinder.GetSearchScopes (entity), refFinder.FindReferencesInFile (refFinder.GetSearchScopes (symbol),
context.UnresolvedFile, context.UnresolvedFile,
context.RootNode as SyntaxTree, context.RootNode as SyntaxTree,
context.Compilation, (n, r) => Rename (n, name), context.Compilation, (n, r) => Rename (n, name),
context.CancellationToken); context.CancellationToken);
} }
public override void Rename (IVariable variable, string name) void Rename (IVariable variable, string name)
{ {
FindReferences refFinder = new FindReferences (); FindReferences refFinder = new FindReferences ();
refFinder.FindLocalReferences (variable, refFinder.FindLocalReferences (variable,
@ -185,16 +189,6 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
context.CancellationToken); context.CancellationToken);
} }
public override void RenameTypeParameter (IType type, string name = null)
{
FindReferences refFinder = new FindReferences ();
refFinder.FindTypeParameterReferences (type,
context.UnresolvedFile,
context.RootNode as SyntaxTree,
context.Compilation, (n, r) => Rename (n, name),
context.CancellationToken);
}
public override void CreateNewType (AstNode newType, NewTypeContext context) public override void CreateNewType (AstNode newType, NewTypeContext context)
{ {
var output = OutputNode (0, newType, true); var output = OutputNode (0, newType, true);

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

@ -442,6 +442,7 @@ namespace Foo
sb.Append(ch); sb.Append(ch);
} }
Init(sb.ToString ()); Init(sb.ToString ());
findReferences.WholeVirtualSlot = true;
var doc = new ReadOnlyDocument(sb.ToString ()); var doc = new ReadOnlyDocument(sb.ToString ());
var result = Rename(symbolName, "x", false); var result = Rename(symbolName, "x", false);
Assert.AreEqual(offsets.Count, result.Count); Assert.AreEqual(offsets.Count, result.Count);

Loading…
Cancel
Save