static IEnumerable <CodeAction> GetActions(RefactoringContext context, AstNode node)
        {
            var resolveResult = context.Resolve(node) as UnknownIdentifierResolveResult;

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

            var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));

            if (service != null && !service.IsValidName(resolveResult.Identifier, AffectedEntity.Class))
            {
                yield break;
            }

            yield return(new CodeAction(context.TranslateString("Create class"), script => {
                script.CreateNewType(CreateType(context, service, node));
            }));

            yield return(new CodeAction(context.TranslateString("Create nested class"), script => {
                script.InsertWithCursor(
                    context.TranslateString("Create nested class"),
                    Script.InsertPosition.Before,
                    CreateType(context, service, node)
                    );
            }));
        }
예제 #2
0
        public static IEnumerable <AstNode> GenerateImplementation(RefactoringContext context, IEnumerable <Tuple <IMember, bool> > toImplement, bool generateRegion)
        {
            var service = (CodeGenerationService)context.GetService(typeof(CodeGenerationService));

            if (service == null)
            {
                yield break;
            }
            var nodes = new Dictionary <IType, List <EntityDeclaration> >();

            foreach (var member in toImplement)
            {
                if (!nodes.ContainsKey(member.Item1.DeclaringType))
                {
                    nodes [member.Item1.DeclaringType] = new List <EntityDeclaration>();
                }
                var decl = service.GenerateMemberImplementation(context, member.Item1, member.Item2);
                if (member.Item2 || member.Item1.DeclaringType.Kind != TypeKind.Interface)
                {
                    RemoveConstraints(decl);
                }

                nodes[member.Item1.DeclaringType].Add(decl);
            }

            foreach (var kv in nodes)
            {
                if (generateRegion)
                {
                    if (kv.Key.Kind == TypeKind.Interface)
                    {
                        yield return(new PreProcessorDirective(
                                         PreProcessorDirectiveType.Region,
                                         string.Format("{0} implementation", kv.Key.Name)));
                    }
                    else
                    {
                        yield return(new PreProcessorDirective(
                                         PreProcessorDirectiveType.Region,
                                         string.Format("implemented abstract members of {0}", kv.Key.Name)));
                    }
                }
                foreach (var member in kv.Value)
                {
                    yield return(member);
                }
                if (generateRegion)
                {
                    yield return(new PreProcessorDirective(
                                     PreProcessorDirectiveType.Endregion
                                     ));
                }
            }
        }
예제 #3
0
        static IEnumerable <CodeAction> GetActions(RefactoringContext context, AstNode node)
        {
            var resolveResult = context.Resolve(node) as UnknownIdentifierResolveResult;

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

            var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));

            if (service != null && !service.IsValidName(resolveResult.Identifier, AffectedEntity.Class))
            {
                yield break;
            }
            ClassType classType = GuessClassTypeByName(service, node);

            ModifyClassTypeBasedOnTypeGuessing(context, node, ref classType);

            string message;

            switch (classType)
            {
            case ClassType.Struct:
                message = context.TranslateString("Create struct");
                break;

            case ClassType.Interface:
                message = context.TranslateString("Create interface");
                break;

            default:
                message = context.TranslateString("Create class");
                break;
            }
            yield return(new CodeAction(message, script => {
                script.CreateNewType(CreateType(context, service, node, classType));
            }, node));

            if (node.Parent is TypeDeclaration || classType != ClassType.Class)
            {
                yield break;
            }
            yield return(new CodeAction(context.TranslateString("Create nested class"), script => {
                script.InsertWithCursor(
                    context.TranslateString("Create nested class"),
                    Script.InsertPosition.Before,
                    CreateType(context, service, node, classType)
                    );
            }, node));
        }
예제 #4
0
        public override IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var service = (CodeGenerationService)context.GetService(typeof(CodeGenerationService));

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

            var type = context.GetNode <AstType>();

            if (type == null || type.Role != Roles.BaseType)
            {
                yield break;
            }
            var state = context.GetResolverStateBefore(type);

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

            var resolveResult = context.Resolve(type);

            if (resolveResult.Type.Kind != TypeKind.Class || resolveResult.Type.GetDefinition() == null || !resolveResult.Type.GetDefinition().IsAbstract)
            {
                yield break;
            }

            var toImplement = CollectMembersToImplement(state.CurrentTypeDefinition, resolveResult.Type);

            if (toImplement.Count == 0)
            {
                yield break;
            }

            yield return(new CodeAction(
                             context.TranslateString("Implement abstract members"),
                             script => script.InsertWithCursor(
                                 context.TranslateString("Implement abstract members"),
                                 state.CurrentTypeDefinition, (s, c) => ImplementInterfaceAction.GenerateImplementation(c, toImplement.Select(m => Tuple.Create(m, false)), true)
                                 .Select(entity => {
                var decl = entity as EntityDeclaration;
                if (decl != null)
                {
                    decl.Modifiers |= Modifiers.Override;
                }
                return entity;
            }).ToList()), type));
        }
        public override IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var service = (CodeGenerationService)context.GetService(typeof(CodeGenerationService));

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

            var type = context.GetNode <AstType>();

            if (type == null || type.Role != Roles.BaseType)
            {
                yield break;
            }
            var state = context.GetResolverStateBefore(type);

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

            var resolveResult = context.Resolve(type);

            if (resolveResult.Type.Kind != TypeKind.Interface)
            {
                yield break;
            }

            bool interfaceMissing;
            var  toImplement = ImplementInterfaceAction.CollectMembersToImplement(
                state.CurrentTypeDefinition,
                resolveResult.Type,
                false,
                out interfaceMissing
                );

            if (toImplement.Count == 0)
            {
                yield break;
            }

            yield return(new CodeAction(context.TranslateString("Implement interface explicit"), script =>
                                        script.InsertWithCursor(
                                            context.TranslateString("Implement Interface"),
                                            state.CurrentTypeDefinition,
                                            (s, c) => ImplementInterfaceAction.GenerateImplementation(c, toImplement.Select(t => Tuple.Create(t.Item1, true)), interfaceMissing).ToList()
                                            )
                                        , type));
        }
        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));
        }
        public IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var pexpr = context.GetNode <PrimitiveExpression>();

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

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

            var resolveResult = context.Resolve(pexpr);

            yield return(new CodeAction(context.TranslateString("Create local constant"), script => {
                string name = CreateMethodDeclarationAction.CreateBaseName(pexpr, resolveResult.Type);
                var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
                if (service != null)
                {
                    name = service.CheckName(context, name, AffectedEntity.LocalConstant);
                }

                var initializer = new VariableInitializer(name, pexpr.Clone());
                var decl = new VariableDeclarationStatement()
                {
                    Type = context.CreateShortType(resolveResult.Type),
                    Modifiers = Modifiers.Const,
                    Variables = { initializer }
                };

                script.InsertBefore(statement, decl);
                var variableUsage = new IdentifierExpression(name);
                script.Replace(pexpr, variableUsage);
                script.Link(initializer.NameToken, variableUsage);
            }));

            yield return(new CodeAction(context.TranslateString("Create constant field"), script => {
                string name = CreateMethodDeclarationAction.CreateBaseName(pexpr, resolveResult.Type);
                var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
                if (service != null)
                {
                    name = service.CheckName(context, name, AffectedEntity.ConstantField);
                }

                var initializer = new VariableInitializer(name, pexpr.Clone());

                var decl = new FieldDeclaration()
                {
                    ReturnType = context.CreateShortType(resolveResult.Type),
                    Modifiers = Modifiers.Const,
                    Variables = { initializer }
                };

                var variableUsage = new IdentifierExpression(name);
                script.Replace(pexpr, variableUsage);
//				script.Link(initializer.NameToken, variableUsage);
                script.InsertWithCursor(context.TranslateString("Create constant"), decl, Script.InsertPosition.Before);
            }));
        }
예제 #8
0
        public override IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            var pexpr = context.GetNode <PrimitiveExpression>();

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

            var visitor = new DeclareLocalVariableAction.SearchNodeVisitior(pexpr);

            if (pexpr.Parent is VariableInitializer)
            {
                var varDec = pexpr.Parent.Parent as VariableDeclarationStatement;
                if (varDec != null && (varDec.Modifiers & Modifiers.Const) != 0)
                {
                    yield break;
                }
                var fieldDecl = pexpr.Parent.Parent as FieldDeclaration;
                if (fieldDecl != null && (fieldDecl.Modifiers & Modifiers.Const) != 0)
                {
                    yield break;
                }
            }

            var node = context.GetNode <BlockStatement>();

            if (node != null)
            {
                node.AcceptVisitor(visitor);
            }

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

            if (statement != null)
            {
                yield return(new CodeAction(context.TranslateString("Create local constant"), script => {
                    string name = CreateMethodDeclarationAction.CreateBaseName(pexpr, resolveResult.Type);
                    var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
                    if (service != null)
                    {
                        name = service.CheckName(context, name, AffectedEntity.LocalConstant);
                    }

                    var initializer = new VariableInitializer(name, pexpr.Clone());
                    var decl = new VariableDeclarationStatement()
                    {
                        Type = context.CreateShortType(resolveResult.Type),
                        Modifiers = Modifiers.Const,
                        Variables = { initializer }
                    };

                    script.InsertBefore(statement, decl);
                    var variableUsage = new IdentifierExpression(name);
                    script.Replace(pexpr, variableUsage);
                    script.Link(initializer.NameToken, variableUsage);
                }, pexpr));
            }

            yield return(new CodeAction(context.TranslateString("Create constant field"), script => {
                string name = CreateMethodDeclarationAction.CreateBaseName(pexpr, resolveResult.Type);
                var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
                if (service != null)
                {
                    name = service.CheckName(context, name, AffectedEntity.ConstantField);
                }

                var initializer = new VariableInitializer(name, pexpr.Clone());

                var decl = new FieldDeclaration()
                {
                    ReturnType = context.CreateShortType(resolveResult.Type),
                    Modifiers = Modifiers.Const,
                    Variables = { initializer }
                };

                var variableUsage = new IdentifierExpression(name);
                script.Replace(pexpr, variableUsage);
                //				script.Link(initializer.NameToken, variableUsage);
                script.InsertWithCursor(context.TranslateString("Create constant"), Script.InsertPosition.Before, decl);
            }, pexpr));

            if (visitor.Matches.Count > 1)
            {
                yield return(new CodeAction(string.Format(context.TranslateString("Create local constant (replace '{0}' occurrences)"), visitor.Matches.Count), script => {
                    string name = CreateMethodDeclarationAction.CreateBaseName(pexpr, resolveResult.Type);
                    var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
                    if (service != null)
                    {
                        name = service.CheckName(context, name, AffectedEntity.LocalConstant);
                    }

                    var initializer = new VariableInitializer(name, pexpr.Clone());
                    var decl = new VariableDeclarationStatement()
                    {
                        Type = context.CreateShortType(resolveResult.Type),
                        Modifiers = Modifiers.Const,
                        Variables = { initializer }
                    };

                    script.InsertBefore(statement, decl);

                    var linkedNodes = new List <AstNode>();
                    linkedNodes.Add(initializer.NameToken);
                    for (int i = 0; i < visitor.Matches.Count; i++)
                    {
                        var identifierExpression = new IdentifierExpression(name);
                        linkedNodes.Add(identifierExpression);
                        script.Replace(visitor.Matches [i], identifierExpression);
                    }
                    script.Link(linkedNodes.ToArray());
                }, pexpr));

                yield return(new CodeAction(string.Format(context.TranslateString("Create constant field (replace '{0}' occurrences)"), visitor.Matches.Count), script => {
                    string name = CreateMethodDeclarationAction.CreateBaseName(pexpr, resolveResult.Type);
                    var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
                    if (service != null)
                    {
                        name = service.CheckName(context, name, AffectedEntity.ConstantField);
                    }

                    var initializer = new VariableInitializer(name, pexpr.Clone());

                    var decl = new FieldDeclaration()
                    {
                        ReturnType = context.CreateShortType(resolveResult.Type),
                        Modifiers = Modifiers.Const,
                        Variables = { initializer }
                    };

                    var linkedNodes = new List <AstNode>();
                    linkedNodes.Add(initializer.NameToken);
                    for (int i = 0; i < visitor.Matches.Count; i++)
                    {
                        var identifierExpression = new IdentifierExpression(name);
                        linkedNodes.Add(identifierExpression);
                        script.Replace(visitor.Matches [i], identifierExpression);
                    }
                    script.InsertWithCursor(context.TranslateString("Create constant"), Script.InsertPosition.Before, decl);
                }, pexpr));
            }
        }