public static IMethodDeclaration GetMonoBehaviourMethod([NotNull] IClassDeclaration classDeclaration, [NotNull] string name) { classDeclaration.GetPsiServices().Locks.AssertReadAccessAllowed(); return(classDeclaration.MethodDeclarations.FirstOrDefault(t => t.NameIdentifier.Name.Equals(name))); }
public static void MoveToMethodWithFieldIntroduction([NotNull] IClassDeclaration classDeclaration, [NotNull] ICSharpExpression expression, [NotNull] string methodName, string fieldName = null) { classDeclaration.GetPsiServices().Locks.AssertReadAccessAllowed(); var methodDeclaration = GetOrCreateMethod(classDeclaration, methodName); MoveToMethodWithFieldIntroduction(classDeclaration, methodDeclaration, expression, fieldName); }
public static void MoveToMethodWithFieldIntroduction([NotNull] IClassDeclaration classDeclaration, [NotNull] IMethodDeclaration methodDeclaration, [NotNull] ICSharpExpression expression, string fieldName = null) { classDeclaration.GetPsiServices().Locks.AssertReadAccessAllowed(); var result = GetDeclaredElementFromParentDeclaration(expression); var factory = CSharpElementFactory.GetInstance(classDeclaration); var type = expression.Type(new ResolveContext(classDeclaration.GetPsiModule())); if (type.IsUnknown) { type = TypeFactory.CreateTypeByCLRName("System.Object", classDeclaration.GetPsiModule()); } var isVoid = type.IsVoid(); if (!isVoid) { var baseName = fieldName ?? CreateBaseName(expression, result); var name = NamingUtil.GetUniqueName(expression, baseName, NamedElementKinds.PrivateInstanceFields, baseName == null ? collection => { collection.Add(expression.Type(), new EntryOptions()); } : (Action <INamesCollection>)null, de => !de.Equals(result)); var field = factory.CreateFieldDeclaration(type, name); field.SetAccessRights(AccessRights.PRIVATE); classDeclaration.AddClassMemberDeclaration(field); var initialization = factory.CreateStatement("$0 = $1;", name, expression.CopyWithResolve()); var body = methodDeclaration.EnsureStatementMemberBody(); body.AddStatementAfter(initialization, null); RenameOldUsages(expression, result, name, factory); } else { var initialization = factory.CreateStatement("$0;", expression.CopyWithResolve()); var body = methodDeclaration.EnsureStatementMemberBody(); body.AddStatementAfter(initialization, null); ExpressionStatementNavigator.GetByExpression(expression).NotNull("statement != null").RemoveOrReplaceByEmptyStatement(); } }
public static IMethodDeclaration GetOrCreateMethod([NotNull] IClassDeclaration classDeclaration, [NotNull] string methodName) { classDeclaration.GetPsiServices().Locks.AssertReadAccessAllowed(); var result = GetMonoBehaviourMethod(classDeclaration, methodName); if (result == null) { var factory = CSharpElementFactory.GetInstance(classDeclaration); var declaration = (IMethodDeclaration)factory.CreateTypeMemberDeclaration("void $0(){}", methodName); result = classDeclaration.AddClassMemberDeclarationBefore(declaration, classDeclaration.MethodDeclarations.FirstOrDefault()); } return(result); }