public 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 = CreateFieldAction.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(), decl); })); }
public IEnumerable <CodeAction> GetActions(RefactoringContext context) { var expr = GetCreatePropertyOrFieldNode(context); if (expr == null) { yield break; } if (expr is MemberReferenceExpression && !(((MemberReferenceExpression)expr).Target is ThisReferenceExpression)) { 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 = CreateFieldAction.GuessAstType(context, expr); if (guessedType == null) { yield break; } var state = context.GetResolverStateBefore(expr); if (state.CurrentMember == null || state.CurrentTypeDefinition == null) { yield break; } bool isStatic = !(expr is NamedExpression) && (state.CurrentMember.IsStatic | state.CurrentTypeDefinition.IsStatic); // var service = (NamingConventionService)context.GetService(typeof(NamingConventionService)); // if (service != null && !service.IsValidName(identifier.Identifier, AffectedEntity.Field, Modifiers.Private, isStatic)) { // yield break; // } yield return(new CodeAction(context.TranslateString("Create field"), script => { var decl = new FieldDeclaration() { ReturnType = guessedType, Variables = { new VariableInitializer(propertyName) } }; if (isStatic) { decl.Modifiers |= Modifiers.Static; } script.InsertWithCursor(context.TranslateString("Create field"), Script.InsertPosition.Before, decl); })); }