Exemple #1
0
        private static string GetCSharpSyntax(ISymbol symbol)
        {
            WrappingStringBuilder builder = new WrappingStringBuilder(MaximumLineLength);

            // Attributes
            foreach (SyntaxNode attributeListNode in symbol.GetAttributes()
                .Select(x => x.ApplicationSyntaxReference.GetSyntax().Parent.NormalizeWhitespace()))
            {
                builder.AppendLine(attributeListNode.ReplaceTrivia(attributeListNode.DescendantTrivia(), (x, y) => new SyntaxTrivia()).NormalizeWhitespace().ToString());
            }
            builder.NewLinePrefix = NewLinePrefix;

            // Accessors, etc.
            INamedTypeSymbol namedTypeSymbol = symbol as INamedTypeSymbol;
            if (namedTypeSymbol != null)
            {
                builder.Append(_cSharpAccessibilityStrings[namedTypeSymbol.DeclaredAccessibility]);
                if (namedTypeSymbol.TypeKind == TypeKind.Class)
                {
                    if (namedTypeSymbol.IsStatic)
                    {
                        builder.Append("static ");
                    }
                    else
                    {
                        if (namedTypeSymbol.IsAbstract)
                        {
                            builder.Append("abstract ");
                        }
                        else if (namedTypeSymbol.IsSealed)
                        {
                            builder.Append("sealed ");
                        }
                    }
                }
            }

            // Symbol
            string symbolDisplayString = symbol.ToDisplayString(_symbolDisplayFormat);
            int constraintsLocation = symbolDisplayString.IndexOf(" where", StringComparison.Ordinal);
            string genericConstraints = constraintsLocation == -1 ? string.Empty : symbolDisplayString.Substring(constraintsLocation + 1);
            builder.Append(constraintsLocation == -1 ? symbolDisplayString : symbolDisplayString.Substring(0, constraintsLocation));

            // Insert base types and interfaces if a named type	
            if (namedTypeSymbol != null && namedTypeSymbol.TypeKind != TypeKind.Enum)
            {
                // Base type (exclude object base)
                bool baseType = false;
                if (namedTypeSymbol.BaseType != null && namedTypeSymbol.BaseType.Name != "Object")
                {
                    builder.Append(" : ");
                    builder.Append(namedTypeSymbol.BaseType.ToDisplayString(_baseTypeDisplayFormat), true);
                    baseType = true;
                }

                // Interfaces
                if (namedTypeSymbol.AllInterfaces.Length > 0)
                {
                    builder.Append(baseType ? ", " : " : ");
                }
                bool first = true;
                foreach (INamedTypeSymbol interfaceSymbol in namedTypeSymbol.AllInterfaces)
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        builder.Append(", ");
                    }
                    builder.Append(interfaceSymbol.ToDisplayString(_baseTypeDisplayFormat), true);
                }
            }

            // Add generic constraints (wrap to new line if already wrapped another part of the signature)
            if (constraintsLocation != -1)
            {
                if (builder.ToString().Contains(Environment.NewLine + NewLinePrefix))
                {
                    builder.AppendLine();
                }
                else
                {
                    builder.Append(" ");
                }
                builder.Append(genericConstraints, true);
            }

            return builder.ToString();
        }
Exemple #2
0
        private static string GetCSharpSyntax(ISymbol symbol)
        {
            WrappingStringBuilder builder = new WrappingStringBuilder(MaximumLineLength);

            // Attributes
            foreach (SyntaxNode attributeListNode in symbol.GetAttributes()
                     .Select(x => x.ApplicationSyntaxReference.GetSyntax().Parent.NormalizeWhitespace()))
            {
                builder.AppendLine(attributeListNode.ReplaceTrivia(attributeListNode.DescendantTrivia(), (x, y) => new SyntaxTrivia()).NormalizeWhitespace().ToString());
            }
            builder.NewLinePrefix = NewLinePrefix;

            // Accessors, etc.
            INamedTypeSymbol namedTypeSymbol = symbol as INamedTypeSymbol;

            if (namedTypeSymbol != null)
            {
                builder.Append(_cSharpAccessibilityStrings[namedTypeSymbol.DeclaredAccessibility]);
                if (namedTypeSymbol.TypeKind == TypeKind.Class)
                {
                    if (namedTypeSymbol.IsStatic)
                    {
                        builder.Append("static ");
                    }
                    else
                    {
                        if (namedTypeSymbol.IsAbstract)
                        {
                            builder.Append("abstract ");
                        }
                        else if (namedTypeSymbol.IsSealed)
                        {
                            builder.Append("sealed ");
                        }
                    }
                }
            }

            // Symbol
            string symbolDisplayString = symbol.ToDisplayString(_symbolDisplayFormat);
            int    constraintsLocation = symbolDisplayString.IndexOf(" where", StringComparison.Ordinal);
            string genericConstraints  = constraintsLocation == -1 ? string.Empty : symbolDisplayString.Substring(constraintsLocation + 1);

            builder.Append(constraintsLocation == -1 ? symbolDisplayString : symbolDisplayString.Substring(0, constraintsLocation));

            // Insert base types and interfaces if a named type
            if (namedTypeSymbol != null && namedTypeSymbol.TypeKind != TypeKind.Enum)
            {
                // Base type (exclude object base)
                bool baseType = false;
                if (namedTypeSymbol.BaseType != null && namedTypeSymbol.BaseType.Name != "Object")
                {
                    builder.Append(" : ");
                    builder.Append(namedTypeSymbol.BaseType.ToDisplayString(_baseTypeDisplayFormat), true);
                    baseType = true;
                }

                // Interfaces
                if (namedTypeSymbol.AllInterfaces.Length > 0)
                {
                    builder.Append(baseType ? ", " : " : ");
                }
                bool first = true;
                foreach (INamedTypeSymbol interfaceSymbol in namedTypeSymbol.AllInterfaces)
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        builder.Append(", ");
                    }
                    builder.Append(interfaceSymbol.ToDisplayString(_baseTypeDisplayFormat), true);
                }
            }

            // Add generic constraints (wrap to new line if already wrapped another part of the signature)
            if (constraintsLocation != -1)
            {
                if (builder.ToString().Contains(Environment.NewLine + NewLinePrefix))
                {
                    builder.AppendLine();
                }
                else
                {
                    builder.Append(" ");
                }
                builder.Append(genericConstraints, true);
            }

            return(builder.ToString());
        }