Exemple #1
0
        internal string FormatTypeName(Type type, ObjectFormattingOptions options)
        {
            string result = GetPrimitiveTypeName(GetPrimitiveSpecialType(type));

            if (result != null)
            {
                return(result);
            }

            result = FormatGeneratedTypeName(type);
            if (result != null)
            {
                return(result);
            }

            if (type.IsArray)
            {
                return(FormatArrayTypeName(type, arrayOpt: null, options: options));
            }

            var typeInfo = type.GetTypeInfo();

            if (typeInfo.IsGenericType)
            {
                return(FormatGenericTypeName(typeInfo, options));
            }

            if (typeInfo.DeclaringType != null)
            {
                return(typeInfo.Name.Replace('+', '.'));
            }

            return(typeInfo.Name);
        }
        // TODO (tomat): Add ReferencePaths, SourcePaths

        internal CommandLineHostObject(TextWriter outputWriter, ObjectFormatter objectFormatter, CancellationToken cancellationToken)
        {
            CancellationToken = cancellationToken;

            _formattingOptions = new ObjectFormattingOptions(
                memberFormat: MemberDisplayFormat.Inline,
                quoteStrings: true,
                useHexadecimalNumbers: false,
                maxOutputLength: 1024,
                memberIndentation: "  ");

            _outputWriter = outputWriter;
            _objectFormatter = objectFormatter;
        }
        // TODO (tomat): Add ReferencePaths, SourcePaths

        internal CommandLineHostObject(TextWriter outputWriter, ObjectFormatter objectFormatter, CancellationToken cancellationToken)
        {
            CancellationToken = cancellationToken;

            _formattingOptions = new ObjectFormattingOptions(
                memberFormat: MemberDisplayFormat.Inline,
                quoteStrings: true,
                useHexadecimalNumbers: false,
                maxOutputLength: 1024,
                memberIndentation: "  ");

            _outputWriter    = outputWriter;
            _objectFormatter = objectFormatter;
        }
        public CommandLineScriptGlobals(TextWriter outputWriter, ObjectFormatter objectFormatter)
        {
            if (outputWriter == null)
            {
                throw new ArgumentNullException(nameof(outputWriter));
            }

            if (objectFormatter == null)
            {
                throw new ArgumentNullException(nameof(objectFormatter));
            }

            PrintOptions = ObjectFormattingOptions.Default;

            _outputWriter = outputWriter;
            _objectFormatter = objectFormatter;

            Args = new List<string>();
        }
        public CommandLineScriptGlobals(TextWriter outputWriter, ObjectFormatter objectFormatter)
        {
            if (outputWriter == null)
            {
                throw new ArgumentNullException(nameof(outputWriter));
            }

            if (objectFormatter == null)
            {
                throw new ArgumentNullException(nameof(objectFormatter));
            }

            PrintOptions = ObjectFormattingOptions.Default;

            _outputWriter    = outputWriter;
            _objectFormatter = objectFormatter;

            Args = new List <string>();
        }
Exemple #6
0
            public Builder(int lengthLimit, ObjectFormattingOptions options, bool insertEllipsis)
            {
                Debug.Assert(lengthLimit <= options.MaxOutputLength);

                int lineLengthLimit = options.MaxLineLength;

                if (insertEllipsis)
                {
                    lengthLimit     = Math.Max(0, lengthLimit - options.Ellipsis.Length - 1);
                    lineLengthLimit = Math.Max(0, lineLengthLimit - options.Ellipsis.Length - 1);
                }

                _lengthLimit     = lengthLimit;
                _lineLengthLimit = lineLengthLimit;
                _currentLimit    = Math.Min(lineLengthLimit, lengthLimit);
                _insertEllipsis  = insertEllipsis;

                _options = options;
                _sb      = new StringBuilder();
            }
Exemple #7
0
        private string FormatGenericTypeName(TypeInfo typeInfo, ObjectFormattingOptions options)
        {
            var pooledBuilder = PooledStringBuilder.GetInstance();
            var builder       = pooledBuilder.Builder;

            // consolidated generic arguments (includes arguments of all declaring types):
            Type[] genericArguments = typeInfo.GenericTypeArguments;

            if (typeInfo.DeclaringType != null)
            {
                var nestedTypes = ArrayBuilder <TypeInfo> .GetInstance();

                do
                {
                    nestedTypes.Add(typeInfo);
                    typeInfo = typeInfo.DeclaringType?.GetTypeInfo();
                }while (typeInfo != null);

                int typeArgumentIndex = 0;
                for (int i = nestedTypes.Count - 1; i >= 0; i--)
                {
                    AppendTypeInstantiation(builder, nestedTypes[i], genericArguments, ref typeArgumentIndex, options);
                    if (i > 0)
                    {
                        builder.Append('.');
                    }
                }

                nestedTypes.Free();
            }
            else
            {
                int typeArgumentIndex = 0;
                AppendTypeInstantiation(builder, typeInfo, genericArguments, ref typeArgumentIndex, options);
            }

            return(pooledBuilder.ToStringAndFree());
        }
        public InteractiveScriptGlobals(TextWriter outputWriter, ObjectFormatter objectFormatter)
        {
            if (outputWriter == null)
            {
                throw new ArgumentNullException(nameof(outputWriter));
            }

            if (objectFormatter == null)
            {
                throw new ArgumentNullException(nameof(objectFormatter));
            }

            Debug.Assert(outputWriter != null);
            Debug.Assert(objectFormatter != null);

            ReferencePaths = new SearchPaths();
            SourcePaths    = new SearchPaths();
            Args           = new List <string>();

            PrintOptions     = ObjectFormattingOptions.Default;
            _outputWriter    = outputWriter;
            _objectFormatter = objectFormatter;
        }
        public InteractiveScriptGlobals(TextWriter outputWriter, ObjectFormatter objectFormatter)
        {
            if (outputWriter == null)
            {
                throw new ArgumentNullException(nameof(outputWriter));
            }

            if (objectFormatter == null)
            {
                throw new ArgumentNullException(nameof(objectFormatter));
            }

            Debug.Assert(outputWriter != null);
            Debug.Assert(objectFormatter != null);

            ReferencePaths = new SearchPaths();
            SourcePaths = new SearchPaths();
            Args = new List<string>();

            PrintOptions = ObjectFormattingOptions.Default;
            _outputWriter = outputWriter;
            _objectFormatter = objectFormatter;
        }
Exemple #10
0
        private void AppendTypeInstantiation(StringBuilder builder, TypeInfo typeInfo, Type[] genericArguments, ref int genericArgIndex, ObjectFormattingOptions options)
        {
            // generic arguments of all the outer types and the current type;
            int currentArgCount = (typeInfo.IsGenericTypeDefinition ? typeInfo.GenericTypeParameters.Length : typeInfo.GenericTypeArguments.Length) - genericArgIndex;

            if (currentArgCount > 0)
            {
                string name = typeInfo.Name;

                int backtick = name.IndexOf('`');
                if (backtick > 0)
                {
                    builder.Append(name.Substring(0, backtick));
                }
                else
                {
                    builder.Append(name);
                }

                builder.Append(GenericParameterOpening);

                for (int i = 0; i < currentArgCount; i++)
                {
                    if (i > 0)
                    {
                        builder.Append(", ");
                    }
                    builder.Append(FormatTypeName(genericArguments[genericArgIndex++], options));
                }

                builder.Append(GenericParameterClosing);
            }
            else
            {
                builder.Append(typeInfo.Name);
            }
        }
        private void AppendTypeInstantiation(StringBuilder builder, TypeInfo typeInfo, Type[] genericArguments, ref int genericArgIndex, ObjectFormattingOptions options)
        {
            // generic arguments of all the outer types and the current type;
            int currentArgCount = (typeInfo.IsGenericTypeDefinition ? typeInfo.GenericTypeParameters.Length : typeInfo.GenericTypeArguments.Length) - genericArgIndex;

            if (currentArgCount > 0)
            {
                string name = typeInfo.Name;

                int backtick = name.IndexOf('`');
                if (backtick > 0)
                {
                    builder.Append(name.Substring(0, backtick));
                }
                else
                {
                    builder.Append(name);
                }

                builder.Append(GenericParameterOpening);

                for (int i = 0; i < currentArgCount; i++)
                {
                    if (i > 0)
                    {
                        builder.Append(", ");
                    }
                    builder.Append(FormatTypeName(genericArguments[genericArgIndex++], options));
                }

                builder.Append(GenericParameterClosing);
            }
            else
            {
                builder.Append(typeInfo.Name);
            }
        }
Exemple #12
0
 public string FormatObject(object obj, ObjectFormattingOptions options = null)
 {
     return new Formatter(this, options).FormatObject(obj);
 }
        internal string FormatTypeName(Type type, ObjectFormattingOptions options)
        {
            string result = GetPrimitiveTypeName(GetPrimitiveSpecialType(type));
            if (result != null)
            {
                return result;
            }

            result = FormatGeneratedTypeName(type);
            if (result != null)
            {
                return result;
            }

            if (type.IsArray)
            {
                return FormatArrayTypeName(type, arrayOpt: null, options: options);
            }

            var typeInfo = type.GetTypeInfo();
            if (typeInfo.IsGenericType)
            {
                return FormatGenericTypeName(typeInfo, options);
            }

            if (typeInfo.DeclaringType != null)
            {
                return typeInfo.Name.Replace('+', '.');
            }

            return typeInfo.Name;
        }
        private string FormatGenericTypeName(TypeInfo typeInfo, ObjectFormattingOptions options)
        {
            var pooledBuilder = PooledStringBuilder.GetInstance();
            var builder = pooledBuilder.Builder;

            // consolidated generic arguments (includes arguments of all declaring types):
            Type[] genericArguments = typeInfo.GenericTypeArguments;

            if (typeInfo.DeclaringType != null)
            {
                var nestedTypes = ArrayBuilder<TypeInfo>.GetInstance();
                do
                {
                    nestedTypes.Add(typeInfo);
                    typeInfo = typeInfo.DeclaringType?.GetTypeInfo();
                }
                while (typeInfo != null);

                int typeArgumentIndex = 0;
                for (int i = nestedTypes.Count - 1; i >= 0; i--)
                {
                    AppendTypeInstantiation(builder, nestedTypes[i], genericArguments, ref typeArgumentIndex, options);
                    if (i > 0)
                    {
                        builder.Append('.');
                    }
                }

                nestedTypes.Free();
            }
            else
            {
                int typeArgumentIndex = 0;
                AppendTypeInstantiation(builder, typeInfo, genericArguments, ref typeArgumentIndex, options);
            }

            return pooledBuilder.ToStringAndFree();
        }
            public Builder(int lengthLimit, ObjectFormattingOptions options, bool insertEllipsis)
            {
                Debug.Assert(lengthLimit <= options.MaxOutputLength);

                int lineLengthLimit = options.MaxLineLength;
                if (insertEllipsis)
                {
                    lengthLimit = Math.Max(0, lengthLimit - options.Ellipsis.Length - 1);
                    lineLengthLimit = Math.Max(0, lineLengthLimit - options.Ellipsis.Length - 1);
                }

                _lengthLimit = lengthLimit;
                _lineLengthLimit = lineLengthLimit;
                _currentLimit = Math.Min(lineLengthLimit, lengthLimit);
                _insertEllipsis = insertEllipsis;

                _options = options;
                _sb = new StringBuilder();
            }
 /// <summary>
 /// Formats an array type name (vector or multidimensional).
 /// </summary>
 internal abstract string FormatArrayTypeName(Type arrayType, Array arrayOpt, ObjectFormattingOptions options);
 public Formatter(ObjectFormatter language, ObjectFormattingOptions options)
 {
     _options = options ?? ObjectFormattingOptions.Default;
     _language = language;
 }
 internal string FormatObject(object obj, ObjectFormattingOptions options)
 {
     return new Formatter(this, options).FormatObject(obj);
 }
        public void LongMembers()
        {
            object obj = new LongMembers();

            var options = new ObjectFormattingOptions(maxLineLength: 20, memberFormat: MemberDisplayFormat.Inline);
            //str = ObjectFormatter.Instance.FormatObject(obj, options);
            //Assert.Equal("LongMembers { Lo ...", str);

            options = new ObjectFormattingOptions(maxLineLength: 20, memberFormat: MemberDisplayFormat.List);
            var str = CSharpObjectFormatter.Instance.FormatObject(obj, options);
            Assert.Equal("LongMembers {\r\n  LongName012345 ...\r\n  LongValue: \"01 ...\r\n}\r\n", str);
        }
Exemple #20
0
 public Formatter(ObjectFormatter language, ObjectFormattingOptions options)
 {
     _options  = options ?? ObjectFormattingOptions.Default;
     _language = language;
 }
        internal override string FormatArrayTypeName(Type arrayType, Array arrayOpt, ObjectFormattingOptions options)
        {
            StringBuilder sb = new StringBuilder();

            // print the inner-most element type first:
            Type elementType = arrayType.GetElementType();
            while (elementType.IsArray)
            {
                elementType = elementType.GetElementType();
            }

            sb.Append(FormatTypeName(elementType, options));

            // print all components of a jagged array:
            Type type = arrayType;
            do
            {
                if (arrayOpt != null)
                {
                    sb.Append('[');

                    int rank = type.GetArrayRank();

                    bool anyNonzeroLowerBound = false;
                    for (int i = 0; i < rank; i++)
                    {
                        if (arrayOpt.GetLowerBound(i) > 0)
                        {
                            anyNonzeroLowerBound = true;
                            break;
                        }
                    }

                    for (int i = 0; i < rank; i++)
                    {
                        int lowerBound = arrayOpt.GetLowerBound(i);
                        int length = arrayOpt.GetLength(i);

                        if (i > 0)
                        {
                            sb.Append(", ");
                        }

                        if (anyNonzeroLowerBound)
                        {
                            AppendArrayBound(sb, lowerBound, options.UseHexadecimalNumbers);
                            sb.Append("..");
                            AppendArrayBound(sb, length + lowerBound, options.UseHexadecimalNumbers);
                        }
                        else
                        {
                            AppendArrayBound(sb, length, options.UseHexadecimalNumbers);
                        }
                    }

                    sb.Append(']');
                    arrayOpt = null;
                }
                else
                {
                    AppendArrayRank(sb, type);
                }

                type = type.GetElementType();
            }
            while (type.IsArray);

            return sb.ToString();
        }
Exemple #22
0
 public string FormatObject(object obj, ObjectFormattingOptions options = null)
 {
     return(new Formatter(this, options).FormatObject(obj));
 }
Exemple #23
0
 /// <summary>
 /// Formats an array type name (vector or multidimensional).
 /// </summary>
 internal abstract string FormatArrayTypeName(Type arrayType, Array arrayOpt, ObjectFormattingOptions options);
        public void LargeGraph()
        {
            var list = new LinkedList<object>();
            object obj = list;
            for (int i = 0; i < 10000; i++)
            {
                var node = list.AddFirst(i);
                var newList = new LinkedList<object>();
                list.AddAfter(node, newList);
                list = newList;
            }

            string output = "LinkedList<object>(2) { 0, LinkedList<object>(2) { 1, LinkedList<object>(2) { 2, LinkedList<object>(2) {";

            for (int i = 100; i > 4; i--)
            {
                var options = new ObjectFormattingOptions(maxOutputLength: i, memberFormat: MemberDisplayFormat.Inline);
                var str = CSharpObjectFormatter.Instance.FormatObject(obj, options);

                var expected = output.Substring(0, i - " ...".Length);
                if (!expected.EndsWith(" ", StringComparison.Ordinal))
                {
                    expected += " ";
                }

                expected += "...";

                Assert.Equal(expected, str);
            }
        }
Exemple #25
0
 internal string FormatObject(object obj, ObjectFormattingOptions options)
 {
     return(new Formatter(this, options).FormatObject(obj));
 }