Beispiel #1
0
        public override IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var identifier = context.GetNode <IdentifierExpression>();

            if (identifier == null)
            {
                yield break;
            }
            if (CreateFieldAction.IsInvocationTarget(identifier))
            {
                yield break;
            }
            var statement = context.GetNode <Statement>();

            if (statement == null)
            {
                yield break;
            }

            if (!(context.Resolve(identifier).IsError))
            {
                yield break;
            }
            var guessedType = TypeGuessing.GuessAstType(context, identifier);

            if (guessedType == null)
            {
                yield break;
            }

            yield return(new CodeAction(context.TranslateString("Create local variable"), script => {
                var initializer = new VariableInitializer(null, identifier.Identifier);
                var decl = new VariableDeclarationStatement()
                {
                    Type = guessedType,
                    Variables = { initializer }
                };
                if (identifier.Parent is AssignmentExpression && ((AssignmentExpression)identifier.Parent).Left == identifier)
                {
                    initializer.Initializer = ((AssignmentExpression)identifier.Parent).Right.Clone();
                    if (!context.UseExplicitTypes)
                    {
                        decl.Type = new SimpleType("var");
                    }
                    script.Replace(statement, decl);
                }
                else
                {
                    script.InsertBefore(statement, decl);
                }
            }, identifier)
            {
                Severity = ICSharpCode.NRefactory.Refactoring.Severity.Error
            });
        }
        public override IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var identifier = CreateFieldAction.GetCreatePropertyOrFieldNode(context);

            if (identifier == null)
            {
                yield break;
            }
            if (CreateFieldAction.IsInvocationTarget(identifier))
            {
                yield break;
            }

            var propertyName = GetPropertyName(identifier);

            if (propertyName == null)
            {
                yield break;
            }

            var statement = context.GetNode <Statement>();

            if (statement == null)
            {
                yield break;
            }

            if (!(context.Resolve(identifier).IsError))
            {
                yield break;
            }

            var guessedType = TypeGuessing.GuessAstType(context, identifier);

            if (guessedType == null)
            {
                yield break;
            }
            var state = context.GetResolverStateBefore(identifier);

            if (state.CurrentTypeDefinition == null)
            {
                yield break;
            }

            bool          createInOtherType   = false;
            ResolveResult targetResolveResult = null;

            if (identifier is MemberReferenceExpression)
            {
                targetResolveResult = context.Resolve(((MemberReferenceExpression)identifier).Target);
                if (targetResolveResult.Type.GetDefinition() == null || targetResolveResult.Type.GetDefinition().Region.IsEmpty)
                {
                    yield break;
                }
                createInOtherType = !state.CurrentTypeDefinition.Equals(targetResolveResult.Type.GetDefinition());
            }

            bool isStatic = targetResolveResult is TypeResolveResult;

            if (createInOtherType)
            {
                if (isStatic && targetResolveResult.Type.Kind == TypeKind.Interface || targetResolveResult.Type.Kind == TypeKind.Enum)
                {
                    yield break;
                }
            }
            else
            {
                if (state.CurrentMember == null)
                {
                    yield break;
                }
                isStatic |= state.CurrentTypeDefinition.IsStatic;
                if (targetResolveResult == null)
                {
                    isStatic |= state.CurrentMember.IsStatic;
                }
            }
            isStatic &= !(identifier is NamedExpression);

            //			var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
//			if (service != null && !service.IsValidName(propertyName, AffectedEntity.Property, Modifiers.Private, isStatic)) {
//				yield break;
//			}

            yield return(new CodeAction(context.TranslateString("Create property"), script => {
                var decl = new PropertyDeclaration()
                {
                    ReturnType = guessedType,
                    Name = propertyName,
                    Getter = new Accessor(),
                    Setter = new Accessor()
                };
                if (isStatic)
                {
                    decl.Modifiers |= Modifiers.Static;
                }

                if (createInOtherType)
                {
                    if (targetResolveResult.Type.Kind == TypeKind.Interface)
                    {
                        decl.Modifiers = Modifiers.None;
                    }
                    else
                    {
                        decl.Modifiers |= Modifiers.Public;
                    }
                    script.InsertWithCursor(
                        context.TranslateString("Create property"),
                        targetResolveResult.Type.GetDefinition(),
                        (s, c) => decl);

                    return;
                }

                script.InsertWithCursor(context.TranslateString("Create property"), Script.InsertPosition.Before, decl);
            }, identifier.GetNodeAt(context.Location) ?? identifier)
            {
                Severity = ICSharpCode.NRefactory.Refactoring.Severity.Error
            });
        }