示例#1
0
        public override 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 = TypeGuessing.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);
            }, expr.GetNodeAt(context.Location) ?? expr)
            {
                Severity = ICSharpCode.NRefactory.Refactoring.Severity.Error
            });
        }