Ejemplo n.º 1
0
            void WriteTypeDeclarationName(ITypeDefinition typeDef, TextWriterTokenWriter formatter, CSharpFormattingOptions formattingPolicy)
            {
                TypeSystemAstBuilder astBuilder = CreateAstBuilder();

                if (typeDef.DeclaringTypeDefinition != null)
                {
                    WriteTypeDeclarationName(typeDef.DeclaringTypeDefinition, formatter, formattingPolicy);
                    formatter.WriteToken(Roles.Dot, ".");
                }
                else if ((ConversionFlags & ConversionFlags.UseFullyQualifiedTypeNames) == ConversionFlags.UseFullyQualifiedTypeNames)
                {
                    formatter.WriteIdentifier(Identifier.Create(typeDef.Namespace));
                    formatter.WriteToken(Roles.Dot, ".");
                }
                formatter.WriteIdentifier(Identifier.Create(typeDef.Name));
                if ((ConversionFlags & ConversionFlags.ShowTypeParameterList) == ConversionFlags.ShowTypeParameterList)
                {
                    var outputVisitor = new CSharpOutputVisitor(formatter, formattingPolicy);
                    outputVisitor.WriteTypeParameters(astBuilder.ConvertEntity(typeDef).GetChildrenByRole(Roles.TypeParameter));
                }
            }
Ejemplo n.º 2
0
        public string Generate(ISymbol symbol)
        {
            if (symbol == null)
            {
                throw new ArgumentNullException("symbol");
            }

            StringWriter writer = new StringWriter();

            _writer = new TextWriterTokenWriter(writer);
            _policy = FormattingOptionsFactory.CreateMono();

            TypeSystemAstBuilder astBuilder = CreateAstBuilder();

            astBuilder.AlwaysUseShortTypeNames = true;
            AstNode node = astBuilder.ConvertSymbol(symbol);

            if (symbol is ITypeDefinition)
            {
                WriteTypeDeclarationName((ITypeDefinition)symbol, _writer, _policy);
            }
            else if (symbol is IMember)
            {
                WriteMemberDeclarationName((IMember)symbol, _writer, _policy);
            }
            else
            {
                _writer.WriteIdentifier(Identifier.Create(symbol.Name));
            }


            if (HasParameters(symbol))
            {
                _writer.WriteToken(symbol.SymbolKind == SymbolKind.Indexer ? Roles.LBracket : Roles.LPar, symbol.SymbolKind == SymbolKind.Indexer ? "[" : "(");
                IEnumerable <ParameterDeclaration> parameters = new List <ParameterDeclaration>(node.GetChildrenByRole(Roles.Parameter));
                if (symbol is IMethod && ((IMethod)symbol).IsExtensionMethod)
                {
                    parameters = parameters.Skip(1);
                }

                WriteCommaSeparatedList(parameters);
                _writer.WriteToken(symbol.SymbolKind == SymbolKind.Indexer ? Roles.RBracket : Roles.RPar, symbol.SymbolKind == SymbolKind.Indexer ? "]" : ")");
            }
            if (_includePlaceholders)
            {
                _writer.WriteToken(Roles.Text, "$0");
            }
            return(writer.ToString());
        }
Ejemplo n.º 3
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));
                }
            }