static TypeDeclaration CreateClassFromType(RefactoringContext context, ClassType classType, SimpleType simpleType) { TypeDeclaration result; string className = simpleType.Identifier; if (simpleType.Parent is Attribute && classType == ClassType.Class) { if (!className.EndsWith("Attribute", System.StringComparison.Ordinal)) { className += "Attribute"; } } result = new TypeDeclaration { Name = className, ClassType = classType }; var entity = simpleType.GetParent <EntityDeclaration>(); if (entity != null) { result.Modifiers |= entity.Modifiers & Modifiers.Public; } var guessedType = TypeGuessing.GuessType(context, simpleType); if (guessedType.Kind == TypeKind.TypeParameter) { ImplementConstraints(context, result, (ITypeParameter)guessedType); } return(result); }
public override IEnumerable <CodeAction> GetActions(RefactoringContext context) { var expr = GetCreatePropertyOrFieldNode(context); if (expr == null) { yield break; } if (!(expr is MemberReferenceExpression)) { yield break; } var propertyName = CreatePropertyAction.GetPropertyName(expr); if (propertyName == null) { yield break; } if (IsInvocationTarget(expr)) { yield break; } var statement = expr.GetParent <Statement>(); if (statement == null) { yield break; } if (!(context.Resolve(expr).IsError)) { yield break; } var guessedType = TypeGuessing.GuessType(context, expr); if (guessedType == null || guessedType.Kind != TypeKind.Enum) { yield break; } var state = context.GetResolverStateBefore(expr); if (state.CurrentMember == null || state.CurrentTypeDefinition == null) { yield break; } yield return(new CodeAction(context.TranslateString("Create enum value"), script => { var decl = new EnumMemberDeclaration { Name = propertyName }; script.InsertWithCursor(context.TranslateString("Create enum value"), guessedType.GetDefinition(), (s, c) => decl); }, expr) { Severity = ICSharpCode.NRefactory.Refactoring.Severity.Error }); }
IEnumerable <CodeAction> GetActionsFromIdentifier(RefactoringContext context, IdentifierExpression identifier) { if (!(context.Resolve(identifier).IsError)) { yield break; } var methodName = identifier.Identifier; var guessedType = TypeGuessing.GuessType(context, identifier); if (guessedType.Kind != TypeKind.Delegate) { yield break; } var invocationMethod = guessedType.GetDelegateInvokeMethod(); if (invocationMethod == null) { yield break; } var state = context.GetResolverStateBefore(identifier); if (state.CurrentMember == null || state.CurrentTypeDefinition == null) { yield break; } bool isStatic = state.CurrentMember.IsStatic || state.CurrentTypeDefinition.IsStatic; var service = (NamingConventionService)context.GetService(typeof(NamingConventionService)); if (service != null && !service.IsValidName(methodName, AffectedEntity.Method, Modifiers.Private, isStatic)) { yield break; } yield return(CreateAction( context, identifier, methodName, context.CreateShortType(invocationMethod.ReturnType), invocationMethod.Parameters.Select(parameter => new ParameterDeclaration(context.CreateShortType(parameter.Type), parameter.Name) { ParameterModifier = GetModifiers(parameter) }), false, isStatic, null)); }
static void ModifyClassTypeBasedOnTypeGuessing(RefactoringContext context, AstNode node, ref ClassType classType) { var guessedType = TypeGuessing.GuessType(context, node); if (guessedType.Kind == TypeKind.TypeParameter) { var tp = (ITypeParameter)guessedType; if (tp.HasValueTypeConstraint) { classType = ClassType.Struct; } if (tp.HasReferenceTypeConstraint) { classType = ClassType.Class; } } }
static TypeDeclaration CreateClassFromObjectCreation(RefactoringContext context, ObjectCreateExpression createExpression) { TypeDeclaration result; string className = createExpression.Type.ToString(); if (!createExpression.Arguments.Any()) { result = new TypeDeclaration { Name = className }; } else { var decl = new ConstructorDeclaration { Name = className, Modifiers = Modifiers.Public, Body = new BlockStatement { new ThrowStatement(new ObjectCreateExpression(context.CreateShortType("System", "NotImplementedException"))) } }; result = new TypeDeclaration { Name = className, Members = { decl } }; decl.Parameters.AddRange(CreateMethodDeclarationAction.GenerateParameters(context, createExpression.Arguments)); } var guessedType = TypeGuessing.GuessType(context, createExpression); if (guessedType.Kind == TypeKind.Interface || guessedType.Kind == TypeKind.Class && guessedType.GetDefinition().IsAbstract) { result.BaseTypes.Add(context.CreateShortType(guessedType)); AddImplementation(context, result, guessedType); } return(result); }
protected override CodeAction GetAction(RefactoringContext context, AnonymousMethodExpression node) { if (context.Location < node.DelegateToken.StartLocation || context.Location >= node.Body.StartLocation) { return(null); } Expression convertExpression = null; var stmt = node.Body.Statements.FirstOrDefault(); if (stmt == null) { return(null); } if (stmt.GetNextSibling(s => s.Role == BlockStatement.StatementRole) == null) { var exprStmt = stmt as ExpressionStatement; if (exprStmt != null) { convertExpression = exprStmt.Expression; } } IType guessedType = null; if (!node.HasParameterList) { guessedType = TypeGuessing.GuessType(context, node); if (guessedType.Kind != TypeKind.Delegate) { return(null); } } return(new CodeAction(context.TranslateString("Convert to lambda"), script => { var parent = node.Parent; while (!(parent is Statement)) { parent = parent.Parent; } bool explicitLambda = parent is VariableDeclarationStatement && ((VariableDeclarationStatement)parent).Type.IsVar(); var lambda = new LambdaExpression(); if (convertExpression != null) { lambda.Body = convertExpression.Clone(); } else { lambda.Body = node.Body.Clone(); } if (node.HasParameterList) { foreach (var parameter in node.Parameters) { if (explicitLambda) { lambda.Parameters.Add(new ParameterDeclaration { Type = parameter.Type.Clone(), Name = parameter.Name }); } else { lambda.Parameters.Add(new ParameterDeclaration { Name = parameter.Name }); } } } else { var method = guessedType.GetDelegateInvokeMethod(); foreach (var parameter in method.Parameters) { lambda.Parameters.Add(new ParameterDeclaration { Name = parameter.Name }); } } script.Replace(node, lambda); }, node)); }
IEnumerable <CodeAction> GetActionsFromMemberReferenceExpression(RefactoringContext context, MemberReferenceExpression invocation) { if (!(context.Resolve(invocation).IsError)) { yield break; } var methodName = invocation.MemberName; var guessedType = TypeGuessing.GuessType(context, invocation); if (guessedType.Kind != TypeKind.Delegate) { yield break; } var invocationMethod = guessedType.GetDelegateInvokeMethod(); var state = context.GetResolverStateBefore(invocation); if (state.CurrentTypeDefinition == null) { yield break; } ResolveResult targetResolveResult = context.Resolve(invocation.Target); bool createInOtherType = !state.CurrentTypeDefinition.Equals(targetResolveResult.Type.GetDefinition()); bool isStatic; if (createInOtherType) { if (targetResolveResult.Type.GetDefinition() == null || targetResolveResult.Type.GetDefinition().Region.IsEmpty) { yield break; } isStatic = targetResolveResult is TypeResolveResult; if (isStatic && targetResolveResult.Type.Kind == TypeKind.Interface || targetResolveResult.Type.Kind == TypeKind.Enum) { yield break; } } else { if (state.CurrentMember == null) { yield break; } isStatic = state.CurrentMember.IsStatic || state.CurrentTypeDefinition.IsStatic; } // var service = (NamingConventionService)context.GetService(typeof(NamingConventionService)); // if (service != null && !service.IsValidName(methodName, AffectedEntity.Method, Modifiers.Private, isStatic)) { // yield break; // } yield return(CreateAction( context, invocation, methodName, context.CreateShortType(invocationMethod.ReturnType), invocationMethod.Parameters.Select(parameter => new ParameterDeclaration(context.CreateShortType(parameter.Type), parameter.Name) { ParameterModifier = GetModifiers(parameter) }), createInOtherType, isStatic, targetResolveResult)); }