Browse Source

Worked on the way nrefactory contexct actions use the resolver.

newNRvisualizers
Mike Krüger 15 years ago
parent
commit
c744d2f523
  1. 13
      ICSharpCode.NRefactory/CSharp/Refactoring/ContextAction/ConvertForeachToFor.cs
  2. 4
      ICSharpCode.NRefactory/CSharp/Refactoring/ContextAction/CreateEventInvocator.cs
  3. 5
      ICSharpCode.NRefactory/CSharp/Refactoring/ContextAction/CreateField.cs
  4. 6
      ICSharpCode.NRefactory/CSharp/Refactoring/ContextAction/CreateLocalVariable.cs
  5. 2
      ICSharpCode.NRefactory/CSharp/Refactoring/ContextAction/CreateProperty.cs
  6. 14
      ICSharpCode.NRefactory/CSharp/Refactoring/ContextAction/GenerateSwitchLabels.cs
  7. 18
      ICSharpCode.NRefactory/CSharp/Refactoring/ContextAction/InsertAnonymousMethodSignature.cs
  8. 13
      ICSharpCode.NRefactory/CSharp/Refactoring/ContextAction/RemoveBackingStore.cs
  9. 2
      ICSharpCode.NRefactory/CSharp/Refactoring/ContextAction/SplitDeclarationAndAssignment.cs
  10. 4
      ICSharpCode.NRefactory/CSharp/Refactoring/ContextAction/UseExplicitType.cs
  11. 17
      ICSharpCode.NRefactory/CSharp/Refactoring/RefactoringContext.cs

13
ICSharpCode.NRefactory/CSharp/Refactoring/ContextAction/ConvertForeachToFor.cs

@ -25,6 +25,7 @@ @@ -25,6 +25,7 @@
// THE SOFTWARE.
using System;
using System.Linq;
using ICSharpCode.NRefactory.TypeSystem;
namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
@ -38,10 +39,10 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -38,10 +39,10 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
return GetForeachStatement (context) != null;
}
static string GetCountProperty (AstType type)
static string GetCountProperty (IType type)
{
if (type is ComposedType && ((ComposedType)type).ArraySpecifiers.Count > 0)
return "Length";
// if (!type.)
// return "Length";
return "Count";
}
@ -49,8 +50,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -49,8 +50,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
var foreachStatement = GetForeachStatement (context);
var result = context.ResolveType (foreachStatement.InExpression);
var countProperty = GetCountProperty (result);
var result = context.Resolve (foreachStatement.InExpression);
var countProperty = GetCountProperty (result.Type);
var initializer = new VariableDeclarationStatement (new PrimitiveType ("int"), "i", new PrimitiveExpression (0));
var id1 = new IdentifierExpression ("i");
@ -86,7 +87,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -86,7 +87,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
if (astNode == null)
return null;
var result = (astNode as ForeachStatement) ?? astNode.Parent as ForeachStatement;
if (result == null || context.ResolveType (result.InExpression) == null)
if (result == null || context.Resolve (result.InExpression) == null)
return null;
return result;
}

4
ICSharpCode.NRefactory/CSharp/Refactoring/ContextAction/CreateEventInvocator.cs

@ -46,10 +46,10 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -46,10 +46,10 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
VariableInitializer initializer;
var eventDeclaration = GetEventDeclaration (context, out initializer);
var type = context.GetDefinition (context.ResolveType (eventDeclaration.ReturnType));
var type = context.Resolve (eventDeclaration.ReturnType).Type;
if (type == null)
return;
var invokeMethod = type.Methods.Where (m => m.Name == "Invoke").FirstOrDefault ();
var invokeMethod = type.GetDelegateInvokeMethod ();
if (invokeMethod == null)
return;

5
ICSharpCode.NRefactory/CSharp/Refactoring/ContextAction/CreateField.cs

@ -27,6 +27,7 @@ @@ -27,6 +27,7 @@
using System;
using ICSharpCode.NRefactory.PatternMatching;
using System.Linq;
using ICSharpCode.NRefactory.TypeSystem;
namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
@ -37,7 +38,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -37,7 +38,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
var identifier = GetIdentifier (context);
if (identifier == null)
return false;
return context.ResolveType (identifier) == null && GuessType (context, identifier) != null;
return context.Resolve (identifier) == null && GuessType (context, identifier) != null;
}
public void Run (RefactoringContext context)
@ -62,7 +63,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -62,7 +63,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
if (identifier.Parent is AssignmentExpression) {
var assign = (AssignmentExpression)identifier.Parent;
var other = assign.Left == identifier ? assign.Right : assign.Left;
return context.ResolveType (other);
return context.Resolve (other).Type.ConvertToAstType ();
}
return null;
}

6
ICSharpCode.NRefactory/CSharp/Refactoring/ContextAction/CreateLocalVariable.cs

@ -51,7 +51,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -51,7 +51,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
if (identifier == null)
continue;
if (context.ResolveType (identifier) == null && GuessType (context, identifier) != null)
if (context.Resolve (identifier) == null && GuessType (context, identifier) != null)
expressions.Insert (0, identifier);
}
}
@ -67,7 +67,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -67,7 +67,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
return false;
if (context.GetNode<Statement> () == null)
return false;
return context.ResolveType (identifier) == null && GuessType (context, identifier) != null;
return context.Resolve (identifier) == null && GuessType (context, identifier) != null;
}
public void Run (RefactoringContext context)
@ -109,7 +109,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -109,7 +109,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
if (identifier != null && (identifier.Parent is InvocationExpression || identifier.Parent.Parent is InvocationExpression)) {
var invocation = (identifier.Parent as InvocationExpression) ?? (identifier.Parent.Parent as InvocationExpression);
var result = context.ResolveMember (invocation).FirstOrDefault () as IMethod;
var result = context.Resolve (invocation).Type.GetDelegateInvokeMethod ();
if (result == null)
return null;
int i = 0;

2
ICSharpCode.NRefactory/CSharp/Refactoring/ContextAction/CreateProperty.cs

@ -36,7 +36,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -36,7 +36,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
var identifier = CreateField.GetIdentifier (context);
if (identifier == null)
return false;
return context.ResolveType (identifier) == null && CreateField.GuessType (context, identifier) != null;
return context.Resolve (identifier) == null && CreateField.GuessType (context, identifier) != null;
}
public void Run (RefactoringContext context)

14
ICSharpCode.NRefactory/CSharp/Refactoring/ContextAction/GenerateSwitchLabels.cs

@ -35,24 +35,22 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -35,24 +35,22 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
var switchStatement = GetSwitchStatement (context);
if (switchStatement == null)
return false;
var result = context.ResolveType (switchStatement.Expression);
var result = context.Resolve (switchStatement.Expression);
if (result == null)
return false;
var type = context.GetDefinition (result);
return type != null && type.ClassType == ClassType.Enum;
return result.Type.IsEnum ();
}
public void Run (RefactoringContext context)
{
var switchStatement = GetSwitchStatement (context);
var result = context.ResolveType (switchStatement.Expression);
var type = context.GetDefinition (result);
var result = context.Resolve (switchStatement.Expression);
var type = result.Type;
var newSwitch = (SwitchStatement)switchStatement.Clone ();
var target = new TypeReferenceExpression (context.CreateShortType (result));
foreach (var field in type.Fields) {
var target = new TypeReferenceExpression (context.CreateShortType (result.Type.ConvertToAstType ()));
foreach (var field in type.GetFields (context.TypeResolveContext)) {
if (field.IsSynthetic || !field.IsConst)
continue;
newSwitch.SwitchSections.Add (new SwitchSection () {

18
ICSharpCode.NRefactory/CSharp/Refactoring/ContextAction/InsertAnonymousMethodSignature.cs

@ -34,16 +34,16 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -34,16 +34,16 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
public bool IsValid (RefactoringContext context)
{
ITypeDefinition type;
IType type;
return GetAnonymousMethodExpression (context, out type) != null;
}
public void Run (RefactoringContext context)
{
ITypeDefinition type;
IType type;
var anonymousMethodExpression = GetAnonymousMethodExpression (context, out type);
var delegateMethod = type.Methods.First ();
var delegateMethod = type.GetDelegateInvokeMethod ();
var sb = new StringBuilder ("(");
for (int k = 0; k < delegateMethod.Parameters.Count; k++) {
@ -63,7 +63,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -63,7 +63,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
}
}
static AnonymousMethodExpression GetAnonymousMethodExpression (RefactoringContext context, out ITypeDefinition delegateType)
static AnonymousMethodExpression GetAnonymousMethodExpression (RefactoringContext context, out IType delegateType)
{
delegateType = null;
@ -71,20 +71,20 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -71,20 +71,20 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
if (anonymousMethodExpression == null || !anonymousMethodExpression.DelegateToken.Contains (context.Location.Line, context.Location.Column) || anonymousMethodExpression.HasParameterList)
return null;
AstType resolvedType = null;
IType resolvedType = null;
var parent = anonymousMethodExpression.Parent;
if (parent is AssignmentExpression) {
resolvedType = context.ResolveType (((AssignmentExpression)parent).Left);
resolvedType = context.Resolve (((AssignmentExpression)parent).Left).Type;
} else if (parent is VariableDeclarationStatement) {
resolvedType = context.ResolveType (((VariableDeclarationStatement)parent).Type);
resolvedType = context.Resolve (((VariableDeclarationStatement)parent).Type).Type;
} else if (parent is InvocationExpression) {
// TODO: handle invocations
}
if (resolvedType == null)
return null;
delegateType = context.GetDefinition (resolvedType);
if (delegateType == null || delegateType.ClassType != ClassType.Delegate)
delegateType = resolvedType;
if (!delegateType.IsDelegate ())
return null;
return anonymousMethodExpression;

13
ICSharpCode.NRefactory/CSharp/Refactoring/ContextAction/RemoveBackingStore.cs

@ -26,6 +26,7 @@ @@ -26,6 +26,7 @@
using System;
using ICSharpCode.NRefactory.TypeSystem;
using System.Linq;
using ICSharpCode.NRefactory.CSharp.Resolver;
namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
@ -98,10 +99,10 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -98,10 +99,10 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
return null;
var returnStatement = propertyDeclaration.Getter.Body.Statements.First () as ReturnStatement;
var result = context.ResolveMember (returnStatement.Expression);
if (result == null)
var result = context.Resolve (returnStatement.Expression);
if (result == null || !(result is MemberResolveResult))
return null;
return result.FirstOrDefault () as IField;
return ((MemberResolveResult)result).Member as IField;
}
internal static IField ScanSetter (RefactoringContext context, PropertyDeclaration propertyDeclaration)
@ -112,10 +113,10 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -112,10 +113,10 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
var assignment = setAssignment != null ? setAssignment.Expression as AssignmentExpression : null;
if (assignment == null || assignment.Operator != AssignmentOperatorType.Assign)
return null;
var result = context.ResolveMember (assignment.Left);
if (result == null)
var result = context.Resolve (assignment.Left);
if (result == null || !(result is MemberResolveResult))
return null;
return result.FirstOrDefault () as IField;
return ((MemberResolveResult)result).Member as IField;
}
}
}

2
ICSharpCode.NRefactory/CSharp/Refactoring/ContextAction/SplitDeclarationAndAssignment.cs

@ -61,7 +61,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -61,7 +61,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
var result = context.GetNode<VariableDeclarationStatement> ();
if (result != null && result.Variables.Count == 1 && !result.Variables.First ().Initializer.IsNull && result.Variables.First ().NameToken.Contains (context.Location.Line, context.Location.Column)) {
resolvedType = context.ResolveType (result.Variables.First ().Initializer);
resolvedType = context.Resolve (result.Variables.First ().Initializer).Type.ConvertToAstType ();
if (resolvedType == null)
return null;
return result;

4
ICSharpCode.NRefactory/CSharp/Refactoring/ContextAction/UseExplicitType.cs

@ -41,7 +41,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -41,7 +41,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
var varDecl = GetVariableDeclarationStatement (context);
using (var script = context.StartScript ()) {
script.Replace (varDecl.Type, context.ResolveType (varDecl.Variables.First ().Initializer));
script.Replace (varDecl.Type, context.CreateShortType (context.Resolve (varDecl.Variables.First ().Initializer).Type.ConvertToAstType ()));
}
}
@ -49,7 +49,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -49,7 +49,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
var result = context.GetNode<VariableDeclarationStatement> ();
if (result != null && result.Variables.Count == 1 && !result.Variables.First ().Initializer.IsNull && result.Type.Contains (context.Location.Line, context.Location.Column) && result.Type.IsMatch (new SimpleType ("var"))) {
if (context.ResolveType (result.Variables.First ().Initializer) == null)
if (context.Resolve (result.Variables.First ().Initializer) == null)
return null;
return result;
}

17
ICSharpCode.NRefactory/CSharp/Refactoring/RefactoringContext.cs

@ -27,6 +27,7 @@ using System; @@ -27,6 +27,7 @@ using System;
using System.Linq;
using ICSharpCode.NRefactory.TypeSystem;
using System.Collections.Generic;
using ICSharpCode.NRefactory.CSharp.Resolver;
namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
@ -67,7 +68,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -67,7 +68,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
}
public abstract ITypeDefinition GetDefinition (AstType resolvedType);
// public abstract IType GetDefinition (AstType resolvedType);
public abstract void ReplaceReferences (IMember member, MemberDeclaration replaceWidth);
@ -100,8 +101,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -100,8 +101,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
#endregion
#region Resolving
public abstract AstType ResolveType (AstNode node);
public abstract IEnumerable<IMember> ResolveMember (Expression expression);
public abstract ResolveResult Resolve (AstNode expression);
#endregion
public string GetNameProposal (string name, bool camelCase = true)
@ -125,5 +125,16 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -125,5 +125,16 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
return baseName + (number > 0 ? (number + 1).ToString () : "");
}
}
public static class ExtensionMethods
{
#region ConvertTypes
public static ICSharpCode.NRefactory.CSharp.AstType ConvertToAstType (this IType type)
{
var builder = new TypeSystemAstBuilder ();
return builder.ConvertType (type);
}
#endregion
}
}

Loading…
Cancel
Save