Exemple #1
0
 static void RemoveConstraints(EntityDeclaration decl)
 {
     foreach (var child in decl.GetChildrenByRole(Roles.Constraint).ToArray())
     {
         child.Remove();
     }
 }
Exemple #2
0
        void WriteTypeDeclarationName(ITypeDefinition typeDef, TokenWriter writer, CSharpFormattingOptions formattingPolicy)
        {
            TypeSystemAstBuilder astBuilder = CreateAstBuilder();
            EntityDeclaration    node       = astBuilder.ConvertEntity(typeDef);

            if (typeDef.DeclaringTypeDefinition != null &&
                ((ConversionFlags & ConversionFlags.ShowDeclaringType) == ConversionFlags.ShowDeclaringType ||
                 (ConversionFlags & ConversionFlags.UseFullyQualifiedEntityNames) == ConversionFlags.UseFullyQualifiedEntityNames))
            {
                WriteTypeDeclarationName(typeDef.DeclaringTypeDefinition, writer, formattingPolicy);
                writer.WriteToken(Roles.Dot, ".");
            }
            else if ((ConversionFlags & ConversionFlags.UseFullyQualifiedEntityNames) == ConversionFlags.UseFullyQualifiedEntityNames)
            {
                if (!string.IsNullOrEmpty(typeDef.Namespace))
                {
                    WriteQualifiedName(typeDef.Namespace, writer, formattingPolicy);
                    writer.WriteToken(Roles.Dot, ".");
                }
            }
            writer.WriteIdentifier(node.NameToken);
            if ((ConversionFlags & ConversionFlags.ShowTypeParameterList) == ConversionFlags.ShowTypeParameterList)
            {
                var outputVisitor = new CSharpOutputVisitor(writer, formattingPolicy);
                outputVisitor.WriteTypeParameters(node.GetChildrenByRole(Roles.TypeParameter));
            }
        }
Exemple #3
0
        void WriteTypeDeclarationName(ITypeDefinition typeDef, TokenWriter writer, CSharpFormattingOptions formattingPolicy)
        {
            var astBuilder         = new TypeSystemAstBuilder();
            EntityDeclaration node = astBuilder.ConvertEntity(typeDef);

            if (typeDef.DeclaringTypeDefinition != null)
            {
                WriteTypeDeclarationName(typeDef.DeclaringTypeDefinition, writer, formattingPolicy);
                writer.WriteToken(Roles.Dot, ".");
            }
            writer.WriteIdentifier(node.NameToken);
            WriteTypeParameters(writer, node.GetChildrenByRole(Roles.TypeParameter));
        }
Exemple #4
0
            void Check(EntityDeclaration entity)
            {
                if (!entity.HasModifier(Modifiers.Override))
                {
                    return;
                }
                var rr = ctx.Resolve(entity) as MemberResolveResult;

                if (rr == null || rr.IsError)
                {
                    return;
                }
                var method = rr.Member as IParameterizedMember;

                if (method == null)
                {
                    return;
                }
                var baseMethod = InheritanceHelper.GetBaseMember(method) as IParameterizedMember;

                if (baseMethod == null)
                {
                    return;
                }

                for (int i = 0; i < Math.Min(method.Parameters.Count, baseMethod.Parameters.Count); i++)
                {
                    var arg     = method.Parameters[i];
                    var baseArg = baseMethod.Parameters[i];

                    if (arg.Name != baseArg.Name)
                    {
                        int _i         = i;
                        var parameters = entity.GetChildrenByRole(Roles.Parameter);
                        AddIssue(new CodeIssue(
                                     parameters.ElementAt(_i).NameToken,
                                     ctx.TranslateString("Parameter name differs in base method declaration"),
                                     string.Format(ctx.TranslateString("Rename to '{0}'"), baseArg.Name),
                                     s => {
                            s.Rename(arg, baseArg.Name);
                        }
                                     ));
                    }
                }
            }
Exemple #5
0
        void WriteTypeParameters(EntityDeclaration node, TokenWriter writer, CSharpFormattingOptions formattingPolicy)
        {
            if ((ConversionFlags & ConversionFlags.ShowTypeParameterList) == ConversionFlags.ShowTypeParameterList)
            {
                var outputVisitor = new CSharpOutputVisitor(writer, formattingPolicy);
                IEnumerable <TypeParameterDeclaration> typeParameters = node.GetChildrenByRole(Roles.TypeParameter);
                if ((ConversionFlags & ConversionFlags.ShowTypeParameterVarianceModifier) == 0)
                {
                    typeParameters = typeParameters.Select(RemoveVarianceModifier);
                }
                outputVisitor.WriteTypeParameters(typeParameters);
            }

            TypeParameterDeclaration RemoveVarianceModifier(TypeParameterDeclaration decl)
            {
                decl.Variance = VarianceModifier.Invariant;
                return(decl);
            }
        }
            public void ConvertEntity(IEntity entity, IOutputFormatter formatter, CSharpFormattingOptions formattingPolicy)
            {
                if (entity == null)
                {
                    throw new ArgumentNullException("entity");
                }
                if (formatter == null)
                {
                    throw new ArgumentNullException("formatter");
                }
                if (formattingPolicy == null)
                {
                    throw new ArgumentNullException("options");
                }

                TypeSystemAstBuilder astBuilder = CreateAstBuilder();
                EntityDeclaration    node       = astBuilder.ConvertEntity(entity);

                PrintModifiers(node.Modifiers, formatter);

                if ((ConversionFlags & ConversionFlags.ShowDefinitionKeyword) == ConversionFlags.ShowDefinitionKeyword)
                {
                    if (node is TypeDeclaration)
                    {
                        switch (((TypeDeclaration)node).ClassType)
                        {
                        case ClassType.Class:
                            formatter.WriteKeyword("class");
                            break;

                        case ClassType.Struct:
                            formatter.WriteKeyword("struct");
                            break;

                        case ClassType.Interface:
                            formatter.WriteKeyword("interface");
                            break;

                        case ClassType.Enum:
                            formatter.WriteKeyword("enum");
                            break;

                        default:
                            throw new Exception("Invalid value for ClassType");
                        }
                        formatter.Space();
                    }
                    else if (node is DelegateDeclaration)
                    {
                        formatter.WriteKeyword("delegate");
                        formatter.Space();
                    }
                    else if (node is EventDeclaration)
                    {
                        formatter.WriteKeyword("event");
                        formatter.Space();
                    }
                }

                if ((ConversionFlags & ConversionFlags.ShowReturnType) == ConversionFlags.ShowReturnType)
                {
                    var rt = node.GetChildByRole(Roles.Type);
                    if (!rt.IsNull)
                    {
                        rt.AcceptVisitor(new CSharpOutputVisitor(formatter, formattingPolicy));
                        formatter.Space();
                    }
                }

                if (entity is ITypeDefinition)
                {
                    WriteTypeDeclarationName((ITypeDefinition)entity, formatter, formattingPolicy);
                }
                else
                {
                    WriteMemberDeclarationName((IMember)entity, formatter, formattingPolicy);
                }

                if ((ConversionFlags & ConversionFlags.ShowParameterList) == ConversionFlags.ShowParameterList && HasParameters(entity))
                {
                    formatter.WriteToken(entity.EntityType == EntityType.Indexer ? "[" : "(");
                    bool first = true;
                    foreach (var param in node.GetChildrenByRole(Roles.Parameter))
                    {
                        if (first)
                        {
                            first = false;
                        }
                        else
                        {
                            formatter.WriteToken(",");
                            formatter.Space();
                        }
                        param.AcceptVisitor(new CSharpOutputVisitor(formatter, formattingPolicy));
                    }
                    formatter.WriteToken(entity.EntityType == EntityType.Indexer ? "]" : ")");
                }

                if ((ConversionFlags & ConversionFlags.ShowBody) == ConversionFlags.ShowBody && !(node is TypeDeclaration))
                {
                    IProperty property = entity as IProperty;
                    if (property != null)
                    {
                        formatter.Space();
                        formatter.WriteToken("{");
                        formatter.Space();
                        if (property.CanGet)
                        {
                            formatter.WriteKeyword("get");
                            formatter.WriteToken(";");
                            formatter.Space();
                        }
                        if (property.CanSet)
                        {
                            formatter.WriteKeyword("set");
                            formatter.WriteToken(";");
                            formatter.Space();
                        }
                        formatter.WriteToken("}");
                    }
                    else
                    {
                        formatter.WriteToken(";");
                    }
                }
            }
Exemple #7
0
        void WriteMemberDeclarationName(IMember member, TokenWriter writer, CSharpFormattingOptions formattingPolicy)
        {
            TypeSystemAstBuilder astBuilder = CreateAstBuilder();
            EntityDeclaration    node       = astBuilder.ConvertEntity(member);

            if ((ConversionFlags & ConversionFlags.ShowDeclaringType) == ConversionFlags.ShowDeclaringType && member.DeclaringType != null)
            {
                ConvertType(member.DeclaringType, writer, formattingPolicy);
                writer.WriteToken(Roles.Dot, ".");
            }
            switch (member.SymbolKind)
            {
            case SymbolKind.Indexer:
                writer.WriteKeyword(Roles.Identifier, "this");
                break;

            case SymbolKind.Constructor:
                WriteQualifiedName(member.DeclaringType.Name, writer, formattingPolicy);
                break;

            case SymbolKind.Destructor:
                writer.WriteToken(DestructorDeclaration.TildeRole, "~");
                WriteQualifiedName(member.DeclaringType.Name, writer, formattingPolicy);
                break;

            case SymbolKind.Operator:
                switch (member.Name)
                {
                case "op_Implicit":
                    writer.WriteKeyword(OperatorDeclaration.ImplicitRole, "implicit");
                    writer.Space();
                    writer.WriteKeyword(OperatorDeclaration.OperatorKeywordRole, "operator");
                    writer.Space();
                    ConvertType(member.ReturnType, writer, formattingPolicy);
                    break;

                case "op_Explicit":
                    writer.WriteKeyword(OperatorDeclaration.ExplicitRole, "explicit");
                    writer.Space();
                    writer.WriteKeyword(OperatorDeclaration.OperatorKeywordRole, "operator");
                    writer.Space();
                    ConvertType(member.ReturnType, writer, formattingPolicy);
                    break;

                default:
                    writer.WriteKeyword(OperatorDeclaration.OperatorKeywordRole, "operator");
                    writer.Space();
                    var operatorType = OperatorDeclaration.GetOperatorType(member.Name);
                    if (operatorType.HasValue)
                    {
                        writer.WriteToken(OperatorDeclaration.GetRole(operatorType.Value), OperatorDeclaration.GetToken(operatorType.Value));
                    }
                    else
                    {
                        writer.WriteIdentifier(node.NameToken);
                    }
                    break;
                }
                break;

            default:
                writer.WriteIdentifier(Identifier.Create(member.Name));
                break;
            }
            if ((ConversionFlags & ConversionFlags.ShowTypeParameterList) == ConversionFlags.ShowTypeParameterList && member.SymbolKind == SymbolKind.Method)
            {
                var outputVisitor = new CSharpOutputVisitor(writer, formattingPolicy);
                outputVisitor.WriteTypeParameters(node.GetChildrenByRole(Roles.TypeParameter));
            }
        }
Exemple #8
0
        void WriteMemberDeclarationName(IMember member, TokenWriter writer, CSharpFormattingOptions formattingPolicy)
        {
            TypeSystemAstBuilder astBuilder = CreateAstBuilder();
            EntityDeclaration    node       = astBuilder.ConvertEntity(member);

            switch (member.SymbolKind)
            {
            case SymbolKind.Indexer:
                writer.WriteKeyword(Roles.Identifier, "this");
                break;

            case SymbolKind.Constructor:
                WriteQualifiedName(member.DeclaringType.Name, writer, formattingPolicy);
                var typeNode = astBuilder.ConvertEntity(member.DeclaringTypeDefinition);
                WriteTypeParameters(writer, typeNode.GetChildrenByRole(Roles.TypeParameter));
                break;

            case SymbolKind.Destructor:
                writer.WriteToken(DestructorDeclaration.TildeRole, "~");
                WriteQualifiedName(member.DeclaringType.Name, writer, formattingPolicy);
                break;

            case SymbolKind.Operator:
                switch (member.Name)
                {
                case "op_Implicit":
                    writer.WriteKeyword(OperatorDeclaration.ImplicitRole, "implicit");
                    writer.Space();
                    writer.WriteKeyword(OperatorDeclaration.OperatorKeywordRole, "operator");
                    writer.Space();
                    ConvertType(member.ReturnType, writer, formattingPolicy);
                    break;

                case "op_Explicit":
                    writer.WriteKeyword(OperatorDeclaration.ExplicitRole, "explicit");
                    writer.Space();
                    writer.WriteKeyword(OperatorDeclaration.OperatorKeywordRole, "operator");
                    writer.Space();
                    ConvertType(member.ReturnType, writer, formattingPolicy);
                    break;

                default:
                    writer.WriteKeyword(OperatorDeclaration.OperatorKeywordRole, "operator");
                    writer.Space();
                    var operatorType = OperatorDeclaration.GetOperatorType(member.Name);
                    if (operatorType.HasValue)
                    {
                        writer.WriteToken(OperatorDeclaration.GetRole(operatorType.Value), OperatorDeclaration.GetToken(operatorType.Value));
                    }
                    else
                    {
                        writer.WriteIdentifier(node.NameToken);
                    }
                    break;
                }
                break;

            default:
                writer.WriteIdentifier(Identifier.Create(member.Name));
                IEnumerable <AstNode> typeArgs = node.GetChildrenByRole(Roles.TypeParameter);
                if (member is IMethod)
                {
                    var typeArguments = ((IMethod)member).TypeArguments;
                    if (typeArguments.Any() && typeArguments.First().Name == "TSource")
                    {
                        typeArgs = typeArgs.Skip(1);
                    }
                }
                WriteTypeParameters(writer, typeArgs);
                break;
            }
        }
		static void RemoveConstraints(EntityDeclaration decl)
		{
			foreach (var child in decl.GetChildrenByRole(Roles.Constraint).ToArray()) {
				child.Remove();
			}
		}
Exemple #10
0
        void WriteMemberDeclarationName(IMember member, TokenWriter writer, CSharpFormattingOptions formattingPolicy)
        {
            TypeSystemAstBuilder astBuilder = CreateAstBuilder();
            EntityDeclaration    node       = astBuilder.ConvertEntity(member);

            switch (member.SymbolKind)
            {
            case SymbolKind.Indexer:
                writer.WriteKeyword(Roles.Identifier, "this");
                break;

            case SymbolKind.Constructor:
                WriteQualifiedName(member.DeclaringType.Name, writer, formattingPolicy);
                var typeNode = astBuilder.ConvertEntity(member.DeclaringTypeDefinition);
                WriteTypeParameters(writer, typeNode.GetChildrenByRole(Roles.TypeParameter));
                break;

            case SymbolKind.Destructor:
                writer.WriteToken(DestructorDeclaration.TildeRole, "~");
                WriteQualifiedName(member.DeclaringType.Name, writer, formattingPolicy);
                break;

            case SymbolKind.Operator:
                switch (member.Name)
                {
                case "op_Implicit":
                    writer.WriteKeyword(OperatorDeclaration.ImplicitRole, "implicit");
                    writer.Space();
                    writer.WriteKeyword(OperatorDeclaration.OperatorKeywordRole, "operator");
                    writer.Space();
                    ConvertType(member.ReturnType, writer, formattingPolicy);
                    break;

                case "op_Explicit":
                    writer.WriteKeyword(OperatorDeclaration.ExplicitRole, "explicit");
                    writer.Space();
                    writer.WriteKeyword(OperatorDeclaration.OperatorKeywordRole, "operator");
                    writer.Space();
                    ConvertType(member.ReturnType, writer, formattingPolicy);
                    break;

                default:
                    writer.WriteKeyword(OperatorDeclaration.OperatorKeywordRole, "operator");
                    writer.Space();
                    var operatorType = OperatorDeclaration.GetOperatorType(member.Name);
                    if (operatorType.HasValue)
                    {
                        writer.WriteToken(OperatorDeclaration.GetRole(operatorType.Value), OperatorDeclaration.GetToken(operatorType.Value));
                    }
                    else
                    {
                        writer.WriteIdentifier(node.NameToken);
                    }
                    break;
                }
                break;

            default:
                writer.WriteIdentifier(Identifier.Create(member.Name));
                if (member is IMethod)
                {
                    var method = ((IMethod)member);

                    var methodParameterTypeArguments =
                        from p in method.Parameters
                        from type in ExpandIntersections(p.Type)
                        from typeArgument in type.TypeArguments
                        select typeArgument.Name;

                    IEnumerable <AstNode> typeArgs = node.GetChildrenByRole(Roles.TypeParameter).Where(arg => !methodParameterTypeArguments.Contains(arg.Name));
                    WriteTypeParameters(writer, typeArgs);
                }
                break;
            }
        }