public string ConvertSymbol(ISymbol symbol)
        {
            var stringWriter = new StringWriter();
            var astBuilder   = new TypeSystemAstBuilder();

            astBuilder.AlwaysUseShortTypeNames = true;
            AstNode node   = astBuilder.ConvertSymbol(symbol);
            var     writer = new TextWriterTokenWriter(stringWriter);
            var     rt     = node.GetChildByRole(Roles.Type);

            if (!rt.IsNull)
            {
                rt.AcceptVisitor(new CSharpOutputVisitor(stringWriter, FormattingOptionsFactory.CreateMono()));
            }

            IProperty property = symbol as IProperty;

            if (property != null)
            {
                writer.Space();
                writer.WriteToken(Roles.LBrace, "{");
                writer.Space();
                if (property.CanGet)
                {
                    writer.WriteKeyword(PropertyDeclaration.GetKeywordRole, "get");
                    writer.WriteToken(Roles.Semicolon, ";");
                    writer.Space();
                }
                if (property.CanSet)
                {
                    writer.WriteKeyword(PropertyDeclaration.SetKeywordRole, "set");
                    writer.WriteToken(Roles.Semicolon, ";");
                    writer.Space();
                }
                writer.WriteToken(Roles.RBrace, "}");
            }
            return(stringWriter.ToString());
        }
Exemple #2
0
            void WriteMemberDeclarationName(IMember member, TextWriterTokenWriter formatter, CSharpFormattingOptions formattingPolicy)
            {
                TypeSystemAstBuilder astBuilder = CreateAstBuilder();

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

                case SymbolKind.Constructor:
                    formatter.WriteIdentifier(Identifier.Create(member.DeclaringType.Name));
                    break;

                case SymbolKind.Destructor:
                    formatter.WriteToken(DestructorDeclaration.TildeRole, "~");
                    formatter.WriteIdentifier(Identifier.Create(member.DeclaringType.Name));
                    break;

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

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

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

                default:
                    formatter.WriteIdentifier(Identifier.Create(member.Name));
                    break;
                }
                if ((ConversionFlags & ConversionFlags.ShowTypeParameterList) == ConversionFlags.ShowTypeParameterList && member.SymbolKind == SymbolKind.Method)
                {
                    var outputVisitor = new CSharpOutputVisitor(formatter, formattingPolicy);
                    outputVisitor.WriteTypeParameters(astBuilder.ConvertEntity(member).GetChildrenByRole(Roles.TypeParameter));
                }
            }
Exemple #3
0
            public void ConvertEntity(IEntity entity, TextWriterTokenWriter 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(Roles.ClassKeyword, "class");
                            break;

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

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

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

                        default:
                            throw new Exception("Invalid value for ClassType");
                        }
                        formatter.Space();
                    }
                    else if (node is DelegateDeclaration)
                    {
                        formatter.WriteKeyword(Roles.DelegateKeyword, "delegate");
                        formatter.Space();
                    }
                    else if (node is EventDeclaration)
                    {
                        formatter.WriteKeyword(EventDeclaration.EventKeywordRole, "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))
                {
                    if (entity.SymbolKind == SymbolKind.Indexer)
                    {
                        formatter.WriteToken(Roles.LBracket, "[");
                    }
                    else
                    {
                        formatter.WriteToken(Roles.LBrace, "(");
                    }
                    bool first = true;
                    foreach (var param in node.GetChildrenByRole(Roles.Parameter))
                    {
                        if (first)
                        {
                            first = false;
                        }
                        else
                        {
                            formatter.WriteToken(Roles.Comma, ",");
                            formatter.Space();
                        }
                        param.AcceptVisitor(new CSharpOutputVisitor(formatter, formattingPolicy));
                    }
                    if (entity.SymbolKind == SymbolKind.Indexer)
                    {
                        formatter.WriteToken(Roles.RBracket, "]");
                    }
                    else
                    {
                        formatter.WriteToken(Roles.RBrace, ")");
                    }
                }

                if ((ConversionFlags & ConversionFlags.ShowBody) == ConversionFlags.ShowBody && !(node is TypeDeclaration))
                {
                    IProperty property = entity as IProperty;
                    if (property != null)
                    {
                        formatter.Space();
                        formatter.WriteToken(Roles.LBrace, "{");
                        formatter.Space();
                        if (property.CanGet)
                        {
                            formatter.WriteKeyword(PropertyDeclaration.GetKeywordRole, "get");
                            formatter.WriteToken(Roles.Semicolon, ";");
                            formatter.Space();
                        }
                        if (property.CanSet)
                        {
                            formatter.WriteKeyword(PropertyDeclaration.SetKeywordRole, "set");
                            formatter.WriteToken(Roles.Semicolon, ";");
                            formatter.Space();
                        }
                        formatter.WriteToken(Roles.RBrace, "}");
                    }
                    else
                    {
                        formatter.WriteToken(Roles.Semicolon, ";");
                    }
                }
            }